0

I run JUnit5 tests but receive an initializationError (No runnable methods). However, I have the @Test annotation on at least one method (failingTest). Why is failingTest not identified as a runnable method (and the corresponding test case executed)?

File StandardTests.java ...

// See https://junit.org/junit5/docs/current/user-guide/#writing-tests-classes-and-methods
import static org.junit.jupiter.api.Assertions.fail ;
import org.junit.jupiter.api.Test ;
public class StandardTests
  {
  @Test
  public void failingTest()
    { fail ( "a failing test" ) ; }
  } // end StandardTests

And in file TestRunner.java ...

import org.junit.runner.JUnitCore ;
import org.junit.runner.Result ;
import org.junit.runner.notification.Failure ;
public class TestRunner
  {
  public static void main ( String [] args )
    {
    Result result = JUnitCore.runClasses ( StandardTests.class ) ;
    for ( Failure failure : result.getFailures() )
      System.out.println ( "failure: " + failure.toString() ) ;
    System.out.println ( "successful: " + result.wasSuccessful() ) ;
    } // end main
  } // end TestRunner

I expect that method failingTest of class StandardTests would be identified as a JUnit5 test case and executed (that is, that failingTest would be identified as a runnable method and executed as a JUnit5 test case). However, the output when I run the TestRunner is:

C:\JUnit\Minimal>C:\Progra~1\Java\JDK-12~1.1\bin\java -cp ".;junit-platform-console-standalone-1.4.2.jar" TestRunner
failure: initializationError(StandardTests): No runnable methods
successful: false

How can I get this JUnit5 test case (failingTest) to be identified as a test case and executed?

user3134725
  • 1,003
  • 6
  • 12
  • 3
    I notice that you're using JUnit 5 for your tests. I haven't dealt with manually running tests in a while, but the imports in your `TestRunner` look like JUnit 4. – chrylis -cautiouslyoptimistic- Jun 27 '19 at 21:42
  • 1
    @chrylis I would assume that's the reason. It will look for methods annotated with the `@Test` annoation, but for Junit4 (different fully-qualified name), not Junit5, thus it can't find any. – Tom Jun 27 '19 at 21:47

1 Answers1

2

It seems you have JUnit4/5 mixture: the annotations are from JUnit5 (org.junit.jupiter.api.Test), but the runner is JUnit4 (org.junit.runner.JUnitCore). The last one should be smth with jupiter.

Also see this answer for more details: https://stackoverflow.com/a/52373592/907576

radistao
  • 14,889
  • 11
  • 66
  • 92
  • I also know that I've got the similar error when I execute test with JUnit5, while `@Test` annotations are from JUnit 4 package. – Dmitriy Popov Jun 27 '19 at 23:04
  • Seems the JUnit developers made major design flaw when developed the new engine with such drawbacks: i met already tens of people who faced this issue. – radistao Jun 28 '19 at 07:00
  • Thanks to everyone for your help. I changed my `import` statements within file `StandardTests.java` to the following: `import org.junit.Assert ;` and `import org.junit.Test ;` . – user3134725 Jun 28 '19 at 18:26
  • but in this case you are running and verifying as JUnit4 using Junit5 engine (so called _vintage_ mode). Didn't you try to figure out the proper JUnit5 runner class? – radistao Jun 28 '19 at 21:36