How to pass command line arguments to Junit Test

Amit :

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.

Progman :

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());
    // ...
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=328466&siteId=1