3

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.

Mohan
  • 7,302
  • 5
  • 32
  • 55
  • 3
    JUnit 4 uses the annotation `@Test`. JUnit 3 extends `TestCase` and runs all `testXXX` methods. – dejvuth Sep 18 '16 at 13:25
  • The relevant part is that you read about the differences between Junit 4 and 3. And that you then get rid of anything that has the Junit3 smell on it ( for example by reworking the extends relationship with @Test annotations). – GhostCat Sep 18 '16 at 13:46

1 Answers1

3

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.

dejvuth
  • 6,986
  • 3
  • 33
  • 36
  • But how does JUnit 3 **find** those classes that extend `junit.framework.TestCase`. Reflection can't do it, because you need to know the name of the `package` that contains the test cases. How does JUnit4 **find** those methods? Reflection can't do it, because you need to know the name of the `package` that contains the classes that contain the method that contain the `@Test` annotation. – Ian Boyd Jul 22 '22 at 04:37
  • 1
    @IanBoyd it doesn’t. It’s up to the environment in which you embed JUnit, to provide that information. – Holger Jul 22 '22 at 12:17