All,
I am currently using JUnit 4 for writing test cases. I am fairly new to JUnit and finding it difficult to test my main class which takes arguments. I have specified the arguments to my JUnit test class by:
1 > Right click JUnit test class
2 > Goto Run As -> Run Configurations
3 > Select the Arguments tab and specify a value (I have entered an invalid argument i.e. the main class expects the command line argument to be converted to an int
and I am passing a String value that cannot be converted to int
)
However, the main
class that I am testing, if the command line argument cannot be converted to a int
, than I throw IllegalArgumentException
. However, the JUnit does not show the testMain() method as Error
or Failure
. I don't think my setup is right for the JUnit class. Can anyone please guide me where I am going wrong
Firstly the arguments should be in the program arguments section. Normally the launching point of the application that's the main method doesn't need to be tested if you design the app to be testable.
Refactor the class
You can now easily test the nullOrEmpty method using junit like
I think this is a better approach
Change the main() method to something like this:
Move the code that was in main() to the new method go(). Now, your test method can do this:
To test your class main method simply write something like: