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?
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
.
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>
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.
You are using JUnit 4 annotations with JUnit 5 dependencies.
If you want to use JUnit 5 you should replace:
'@Before' with '@BeforeEach'
'@AfterEach' with '@AfterEach'
'@BeforeAll' with '@BeforeAll'
'@AfterAll' with '@AfterAll'
Here is more about JUnit 5 annotations https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations/
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.
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.
the test class is not public, make it public and it should work
right click and run as -> config-> make this change and run it will works cheers!!.