Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8508339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:08:07+00:00 2026-06-11T03:08:07+00:00

My process simply add some content to the system variable PATH . Actually I’m

  • 0

My process simply add some content to the system variable PATH. Actually I’m doing this with a Process that use the setx.exe:

public void changePath(String newPath ) {
  String path = System.getenv("PATH") + ";";
  String[] cmd = new String[]{"C:\\Windows\\System32\\setx.exe", "PATH", 
      path+newPath, "-m"};
  ProcessBuilder builder = new ProcessBuilder(cmd);
  ...
}

So I tried to write a test case to it.

Class UpdatePathTest {

  @Test
  public void testUpdatePath() {
    //call the method that update the path
    changePath("C:\\somebin");
    assertTrue(System.getenv("PATH").contains("C:\\somebin")); //fails
    // ProcessBuilder with command String[]{"cmd", "/C", "echo", "%PATH%"}; will fail too.
    //and the above in a new Thread will fail too.
  }

}

So, is there any way to get the new PATH value? Writing the new path is the only option, because I’m developing a jar that will install a desktop application.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:08:09+00:00Added an answer on June 11, 2026 at 3:08 am

    I’m not sure changing the path is a good idea in a unit test. What if the test fails? You will have to make sure you do all the relevant tidy up.

    Consider inverting your dependencies and use dependency injection.

    This article explains it quite well I think.

    So instead of having a method that does:

    public void method() {
        String path = System.getenv("PATH") + ";";
        //do stuff on path
    }
    

    consider doing:

    public void method(String path) {
        //do stuff on path
    }
    

    which allows you to stub the path. If you cannot change the signature of the method then consider using the factory pattern and using a test factory to get the path.

    EDIT: after update to question

    What you have to think about here is what you are testing. When you call C:\Windows\System32\setx.exe you have read the API docs and are calling it with the correct parameters. This is much like calling another method on a java API. For example if you are manipulating a list you “know” it is zero based. You do not need to test this you just read the API and the community backs you up on this. For testing changePath I think you probably what to test what is going into ProcessBuilder. Again you have read the API docs and you have to assume that you are passing in the correct variables. (See //1 at bottom) And again you have to assume that ProcessBuilder works properly and that the Oracle (or most likely Sun) guys have implemented it to the API documents.

    So what you want to do is check that you are passing variables to ProcessBuilder that match the specification as you understand it. For this you can mock ProcessBuilder and then verify that you are passing the correct parameters and calling the correct method on this class.

    In general it is a hard one to test because you don’t want to test the windows functions but want to test java’s interaction with them.

    //1 The main problem I have had with calling this external commands is understanding the API documents correctly or setting up the command. Usually you have to get the command line out and check that you are using methods correctly (esp cmd functions). This can mean that you work out how to use the cmd function, code it into ProcessBuilder and then write a test (or vice versa on the ProcessBuilder/test) Not the ideal way but sometimes documents are hard to understand.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On my system I can't run a simple Java application that start a process.
I'm in the process of making some improvements to a live Drupal site that's
I'm starting a new application that must use an existing database that use some
Is this a simple process? I'm only writing a quick hacky UI for an
I have some mechanics to measure method execution time. Simply at start of method
I'm new to this, so please bear with me. I simply want to get
There are already some pretty good threads on this topic on Stack Overflow, but
I need to create content that only outputs what I entered, no layout, no
Ok, Just some background... I have a fully functioning game that uses the canvas
I want to add a simple process bar to my code with asy task.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.