1.

Do You Need To Write A Main () Method Compulsorily In A Junit Test Case Class?

Answer»

No. But still developers write the MAIN() method in a JUnit test case class to call a JUnit test runner to run all tests defined in this class like:

PUBLIC static void main(String[] args) {
junit.textui.TestRunner.run(Calculator.class);
}

Since you can call a JUnit runner to run a test case class as a system command, explicit main() for a Junit test case is not recommended. junit.textui.TestRunner.run() method takes the test class name as its argument. This method automatically finds all class methods whose name STARTS with test. Thus it will RESULT in below MENTIONED findings:

  1. testCreateLogFile()
  2. testExists()
  3. testGetChildList()

It will execute each of the 3 methods in unpredictable sequence (hence test case methods should be independent of each other) and give the result in console.

No. But still developers write the main() method in a JUnit test case class to call a JUnit test runner to run all tests defined in this class like:

public static void main(String[] args) {
junit.textui.TestRunner.run(Calculator.class);
}

Since you can call a JUnit runner to run a test case class as a system command, explicit main() for a Junit test case is not recommended. junit.textui.TestRunner.run() method takes the test class name as its argument. This method automatically finds all class methods whose name starts with test. Thus it will result in below mentioned findings:

It will execute each of the 3 methods in unpredictable sequence (hence test case methods should be independent of each other) and give the result in console.



Discussion

No Comment Found