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.
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());
// ...
}
Firebase Cloud Functions: PubSub, "res.on is not a function"
TypeError: Cannot read properties of undefined (reading 'createMessageComponentCollector')
My server keeps running into a deadlock, and I'm not sure how to solve it
I am using IntelliJ and I would like to package a library I found into a jar for my projectSaid library was found online and only contains java files without a main class
This question already has an answer here: