Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit5 tests work fine with maven but not when run through Eclipse, "No tests found with test runner 'JUnit 5'."

I'm getting a pop-up window with title "Could not run test" and message "No tests found with test runner 'JUnit 5'." when I try to run JUnit 5 tests with Eclipse via Run As > JUnit Test.

I have two test-related files. One is a test suite:

...

import org.junit.jupiter.api.BeforeAll;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
@SelectPackages("com.foo.stufftotest")

public class TestSuite {
    @BeforeAll
    public static void setup() {
        ...

The other contains the "actual" tests:

package com.foo.stufftotest;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import com.foo.TestSuite;
import com.foo.business.mechanics.LogicStuff1;

public class BusinessTest {
    @Test
    public void testLogic1() {
        ...
    }

    @Test
    public void testLogic2() {
        ...
    }

    ...

All the testLogicN() methods depend on setup stuff done in TestSuite.setup(). If setup() doesn't run, there are a lot of null values, and it's no surprise that things fail. When I try to run JUnit from the project's context menu, all the tests are triggered and they all fail; the suite doesn't seem to be recognized. When I try to run JUnit specifically from TestSuite.java's context menu, I end up with the error I mentioned at the top of the question.

However, when I run maven test on the project, the suite is triggered properly, and all the tests pass. Therefore, the code itself doesn't seem to be the problem.

I don't remember having this issue with JUnit 4, although I never used JUnit 4 with this particular project.

Am I using Eclipse wrong, or am I using JUnit5 wrong? What's the fix here?

like image 982
SOLO Avatar asked Jan 22 '18 19:01

SOLO


2 Answers

I encountered the same issue in Eclipse Oxygen 4.7.1 despite the fact that my unit tests were properly annotated with org.junit.jupiter.

I used the Maven > Update Project and selected Update project configuration from pom.xml but that did nothing even though I thought it would pick up the fact that I had JUnit5 dependencies in the pom.

My solution:

  1. Open Java Build Path, select Libraries tab and Add Library.
  2. Select JUnit.
  3. Select JUnit5 for JUnit library version.

Once added I was able to manually execute tests from Eclipse. However I'm still not certain why this was not added automatically.

like image 117
RiverCityCoder Avatar answered Oct 19 '22 06:10

RiverCityCoder


I'm getting a pop-up window with title "Could not run test" and message "No tests found with test runner 'JUnit 5'." when I try to run JUnit 5 tests with Eclipse via Run As > JUnit Test.

That's because TestSuite is in fact not a JUnit 5 test class. Since it is annotated with @RunWith (from JUnit 4) it is a JUnit 4 test class. To avoid the pop-up window in Eclipse, simply click on "Run Configurations" and select JUnit 4 instead of JUnit 5 for running the test class.

The other issue you have is that @BeforeAll is an annotation from JUnit Jupiter. Thus, it is simply not supported in a class annotated with @RunWith(JUnitPlatform.class) (unless that class also happens to contain @Test methods for JUnit Jupiter). Thus you will have to find an alternative approach to executing your "set up" code.

like image 3
Sam Brannen Avatar answered Oct 19 '22 05:10

Sam Brannen