Mohan Mohan - 2 months ago 27
Java Question

How does JUnit find tests?

I had assumed that JUnit found tests (inside classes derived from

junit.framework.TestCase
) by looking for methods annotated with
@Test
. However, I've included a test from http://256stuff.com/sources/jenkins_hash_java/JenkinsHashTest.java in a project. The structure of the code is as follows:

import junit.framework.TestCase;

public class JenkinsHashTest extends TestCase {
public void testHashes() {
...//this code is run
}
}


I have confirmed that the test method
testHashes()
is run despite not being annotated with
@Test
. In case it's relevant, I'm invoking all of this via
gradle test
.

Answer

You can refer to this Junit 3 vs Junit 4 Comparison:

Basically, in JUnit 3 you need to extend junit.framework.TestCase. All test cases need to follow the testXXX pattern.

JUnit 4 removes that restrictions. Any public classes with a zero-argument public constructor can act as a test class. Any methods which need consider as a test method should be annotated with the @Test annotation.