Mohan Mohan - 4 months ago 36
Java Question

How does JUnit find tests?

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

) by looking for methods annotated with
. However, I've included a test from 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
is run despite not being annotated with
. In case it's relevant, I'm invoking all of this via
gradle test


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.