12

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

VaL
  • 1,128
  • 15
  • 29
Leem
  • 17,220
  • 36
  • 109
  • 159
  • Possible duplicate of [Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError](https://stackoverflow.com/questions/46717693/eclipse-no-tests-found-using-junit-5-caused-by-noclassdeffounderror) – Eduardo Meneses May 01 '18 at 21:19
  • `@Test`should be at class level I think. – LMC May 02 '18 at 01:10
  • You seem to be using JUnit 4 annotations from the org.junit package instead of JUnit Jupiter annotations from the org.junit.jupiter.api package. – Marc Philipp May 02 '18 at 06:25

14 Answers14

19

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.

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
  • It's a good point, however, this part seems to have got buggy somehow in eclipse; I tried with both annotations and both frameworks and the message keeps appearing; and I tried with several solutions in this thread and it keeps appearing. I also realised, it this mattered, that the project was not added in the source tab, in the run configuration, but adding it there didn't help. It's kinda frustrating because you wanna be a good boy and write your tests, and you struggle for a while with this and yet not get it going. – Gabriel Xunqueira Aug 04 '20 at 18:09
  • I have just realised that there is a comment on the first question stating a possible duplicate; have a looke if you didin't realise. The problem here is if you run a custom version of eclipse, e.g. sts or codeready, you'll be lagging behind (maybe). Still, after realising about this I need to install a new eclipse ... sigh – Gabriel Xunqueira Aug 04 '20 at 18:18
7

I had the same error, after trying everything, I have realized that the JUnit library wasn't added. So after adding it, tests worked as intended.

4b0
  • 21,981
  • 30
  • 95
  • 142
S. Stupar
  • 71
  • 1
  • 3
5

This happened to me because my test method was declared as private and JUnit could not detect it. After I made it public it worked as expected, of course with @Test annotation.

4

In Eclipse 2019-09 (4.13) is a bug that can cause the "No tests found with test runner 'JUnit 5'" error: https://bugs.eclipse.org/bugs/show_bug.cgi?id=551298

I added the following dependency to my project and it worked for me:

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-commons</artifactId>
    <version>1.5.2</version><!--$NO-MVN-MAN-VER$-->
    <scope>test</scope>
</dependency>
Markus Hettich
  • 544
  • 6
  • 12
3

the test class is not public, make it public and it should work

2

You are using JUnit 4 annotations with JUnit 5 dependencies.
If you want to use JUnit 5 you should replace:

@Before with @BeforeEach
@After with @AfterEach
@BeforeClass with @BeforeAll
@AfterClass with @AfterAll
@Ignore with @Disabled

Here is more about JUnit 5 annotations https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations/

salerokada
  • 334
  • 3
  • 7
2

Take a look back to your imports.

You imports import org.junit.Test; (Used for run test cases with JUnit 4)

You need to import import org.junit.jupiter.api.Test; to run tast case with JUnit5.

2

Right click the java file and choose:

Build Path -> Configure Build Path -> Java Build Path.

Choose the tab Libraries in Java Build Path, On the right side tab choose Add Library.

Select JUnit and click Next.

Choose JUnit 5 in JUnit Library version dropdown and click finish.

Now run the Test with Junit Test, it will work.

This procedure worked for me.

Ajith
  • 33
  • 5
0

right click and run as -> config-> make this change and run it will works cheers!!. enter image description here

0

My 2 cents:

Right click on project -> Configure -> Add module-info -> Give some random name. It should automatically add there a requires in the JUnit package, if not, then manually add this:

requires org.junit.jupiter.api;

And it will magically work!

* tested on Eclipse 2018-12

Catalin Pirvu
  • 175
  • 2
  • 8
0

My issue was that, Run works fine, debug throws this error.

I was getting this merely because of low memory in the system. Ideally, eclipse couldn't launch the debug session due to low memory in the system. Error message thrown is confusing.

Hope it helps someone!

user3499836
  • 87
  • 1
  • 9
0

In my current case, I encountered this error when testing the pact verification on the Service Provider side.The error was thrown because the Pact Broker doesn't have any pact to test against the Service Provider.

Visit the pact broker and ensure that there is at least one pact to test with.

Iwan Satria
  • 1,903
  • 1
  • 19
  • 22
0

I had the same problem with an Eclipse (2020-09) non-Maven project. None of the proposed solutions worked, but changing the compiler from 9.0.4 to 15.0.1 did.

I did that by selecting Preferences > Java > Installed JREsand checking the box next to jdk-15.01. You may need to install a recent JDK if none are shown.

Ellen Spertus
  • 6,576
  • 9
  • 50
  • 101
0

Adding the JUnit vintage dependency fixed the issue for me.

https://junit.org/junit5/docs/current/user-guide/#migrating-from-junit4-running

    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <scope>test</scope>
    </dependency>   
Rob Breidecker
  • 604
  • 1
  • 7
  • 12