Leem
Leem

Reputation: 18308

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

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

Upvotes: 12

Views: 39418

Answers (15)

zb226
zb226

Reputation: 10500

This also happens if a class of the exact same name exists in both the main and test parts of the source code and the former does not contain test code, e.g. if you have

  1. src/main/some/package/structure/SomeTest.java (no test code)
  2. src/test/some/package/structure/SomeTest.java (actual test code)

Eclipse will fail to run the second class with the JUnit 5 test runner and show the error

No tests found with test runner JUnit 5

(Eclipse 4.27 / 2023-07)

Upvotes: 0

Rob Breidecker
Rob Breidecker

Reputation: 604

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>   

Upvotes: 0

Ellen Spertus
Ellen Spertus

Reputation: 6805

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.

Upvotes: 0

Ajith
Ajith

Reputation: 33

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.

Upvotes: 2

Iwan Satria
Iwan Satria

Reputation: 2143

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.

Upvotes: 0

user3499836
user3499836

Reputation: 117

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!

Upvotes: 0

salerokada
salerokada

Reputation: 383

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/

Upvotes: 2

Catalin Pirvu
Catalin Pirvu

Reputation: 175

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

Upvotes: 0

Arun Kumar T
Arun Kumar T

Reputation: 66

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

Upvotes: 0

JustAnotherProgrammer
JustAnotherProgrammer

Reputation: 146

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

Upvotes: 3

S. Stupar
S. Stupar

Reputation: 71

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.

Upvotes: 7

Cosmin Mavrichi
Cosmin Mavrichi

Reputation: 53

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.

Upvotes: 5

Markus Hettich
Markus Hettich

Reputation: 574

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>

Upvotes: 4

Ravirajsinh Vaghela
Ravirajsinh Vaghela

Reputation: 177

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.

Upvotes: 2

Sam Brannen
Sam Brannen

Reputation: 31177

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.

Upvotes: 19

Related Questions