简体   繁体   中英

How to pass command line arguments to Junit Test

I have a main class that accepts command line arguments and I am passing this parameter to another class. Now i have to test the myClass with parameters. I have JUnit to test it but I want to know how do pass this in the test

public class JsonFileTest {
    public static void main(String[] fileNames) {
        myClass class = new myClass(fileNames);
    }
}

I am a beginner in JUnit and learning it so if any guidance would be highly appreciated. I need to pass filenames to a class method.

It seems counter-intuitive that you want to parameterize your unit tests in this way. I would suggest that you think of a few sets of arguments and hardcode them in your unit tests. This way they are repeatable, and, hopefully, offer good coverage.

Several useful points for the beginner:

  1. You can't name your object "class" in Java.

  2. It's recommended to follow Java naming convention, so class name should start with capital letter.

Now, about command line arguments. You have an answer in your code:

public static void main(String[] fileNames) {

String[] fileNames - is array of command line arguments.

So to provide some arguments to your method, initialize it with parameter you need.

Something like:

@Test
public void testMainMethod(){
   JsonFileTest.main(new String[]{"file1", "file2"});
}

But in the real life it's not a good idea to test a main method. It returns nothing, so you're only able to create some test to check that it throws/doesn't throw some exception. The idea is to test the entire logic. In this sample case, object construction:

@Test
public void testConstructor(){
   myClass class = new myClass(new String[]{"file1", "file2"});
   /* Your assertions here */

}

You don't need to pass command line parameters to your JUnit execution. Instead your test methods should build/prepare everything and call your new myClass(...) constructor with the parameters your original program would do when using command line parameters. The code might look like this:

@Test
public void checkForWhatever() {
    // prepare everything here like create a temp file
    File x = ...;
    String filename = x.getName(); // or maybe even x.getAbsolutePath();

    String[] arguments = new String[1];
    arguments[0] = filename;

    // now call your constructor
    myClass obj = new myClass(arguments);

    // do any checks here now
    Assertions.assertTrue(obj.getWhatever());
    // ...
}

I want to pass arguments to an @BeforeAll method to create missing directories.

Multiple projects rely on these directories, but the projects are located in different locations:

  1. "C:/Workspace/Project1"
  2. "C:/Workspace/Project2"
  3. "C:/Users/Test/Project3"4
  4. etc

I'd like to pass the file path as variable to the JUnit methods so that it can create Log, Temp, Input, etc. inside for each project. How can I do this using cli? I've tried:

  java -jar junit-platform-console-standalone-1.5.2.jar --class-path out --scan-class-path "C:\Workspace\Project1"

Also, I have a constructor that takes a parameter:

    public class ConnectIntegrationTest {

        private String workspace;


        public ConnectIntegrationTest(String localWorkspace) {
            this.workspace = localWorkspace;
        }

@BeforeAll
    void setupMissingDirectories() throws Exception {

I must be missing something here...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM