Eclipse keep saying "No tests found with test runner JUnit 5"

Leem picture Leem · May 1, 2018 · Viewed 20.4k times · Source

I am using Eclipse Oxygen.3 Release (4.7.3). The following is my JUnit test class:

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

class MyMathTest {
    MyMath myMath = new MyMath();

    @Before
    public void before() {
        System.out.println("Before");
    }

    @After
    public void after() {
        System.out.println("After");
    }

    @Test
    public void testSum_with3numbers() {
        System.out.println("Test1");
        int result = myMath.sum(new int[] {1,2,3});
        int expected = 6;
        assertEquals(expected, result);
    }

    @Test
    public void testSum_with1numbers() {
        System.out.println("Test2");
        int result = myMath.sum(new int[] {3});
        int expected = 3;
        assertEquals(expected, result);
    }

    @BeforeClass
    public static void beforeClass() {
        System.out.println("Before class");
    }

    @AfterClass
    public static void afterClass() {
        System.out.println("After class");
    }

}

When I run this Junit test, eclipse keeps popping up dialog telling "No tests found with test runner 'JUnit 5'". Why?

enter image description here

Answer

Sam Brannen picture Sam Brannen · May 2, 2018

Your test class is currently based on JUnit 4 since it uses annotations from the org.junit package.

Thus, to get it to run as a JUnit 4 test class in Eclipse, you need to select the JUnit 4 Runner in the "Run Configuration". Click on the tiny arrow (pointing down) next to the green circle with a white arrow in it (at the top of the Eclipse IDE). There you should see your test class, and by selecting that you can switch between the JUnit 4 and JUnit 5 runners.

On the other hand, if your goal is to write a test using JUnit Jupiter (i.e., the programming model for JUnit 5), you'll need to switch from annotations in org.junit to org.junit.jupiter.api.