Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To find the number of test methods in a JUnit TestCase

Tags:

java

junit

Is there a way to know the number of test methods in a test case?

What I want to do is have a test case which tests several scenarios and for all these i would be doing the data setUp() only once. Similarly I would like to do the cleanup (tearDown()) once at the end of all the test methods.

The current approach i am using is to maintain a counter for the number of test methods that are present in the file and decrement them in the tearDown method and do the cleanup when the count reaches 0. But this counter needs to be taken care of whenever new test methods are added.

like image 506
user18727 Avatar asked Feb 11 '09 08:02

user18727


People also ask

How do I check JUnit test cases?

We create an object of the JunitTestCaseExample. java class, and by using its object, we will test all its methods. We use the assertEquals() method to check the actual result with the expected output.

How do you call a method in JUnit test case?

Create Test Runner Class It imports the JUnitCore class and uses the runClasses() method that takes the test class name as its parameter. Compile the Test case and Test Runner classes using javac. Now run the Test Runner, which will run the test case defined in the provided Test Case class. Verify the output.


1 Answers

Instead of using setup/teardown you should probably use methods annotated with @BeforeClass and @AfterClass instead.

like image 178
krosenvold Avatar answered Sep 30 '22 21:09

krosenvold