Spring Boot 2.3.1, JUnit 5, Maven 3.6.3 - Maven lifecycle “test” does not run test suite

可紊 提交于 2021-01-29 10:08:45

问题


pom.xml:

...
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-runner</artifactId>
    <version>1.7.0-M1</version>
    <scope>test</scope>
</dependency>
...

In <build> only is used:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

Not in use in <build> are the plugins maven-surefire-plugin and maven-compiler-plugin

There are a few test classes (MyTest1Test, MyTest2Test, etc.), testing REST controllers. Some test methods are tagged with "myTag", to be included in a test suite, like:

@SpringBootTest
@AutoConfigureMockMvc
public class MyTest1Test {
...
    @Test
    @WithMockUser
    public void test1() {...}

    @Test
    @WithMockUser
    @Tag("myTag")
    public void test2() {...}
...
}

Also there is a test suite, defined like:

@RunWith(JUnitPlatform.class)
@SelectClasses({MyTest1Test.class, MyTest3Test.class})
@IncludeTags({"myTag"})
public class MyTestSuiteTest {
...
}

When I select Maven > test, all test classes are run, only the test suite is not run. Why not? What kind of configuration has to be done?

When I run the test suite itself by right-click > Run 'MyTestSuiteTest' within the test class, it is run correctly.

Trying replacing @RunWith(JUnitPlatform.class with @ExtendWith(SpringExtension.class) doesn't solve it. Maven > "test" doesn't run the test suite, and worth, trying to run the test suite with IDE (right-click on the class > Run 'MyTestSuiteTest") shows and error:

org.junit.runners.model.InvalidTestClassError: Invalid test class '...MyTestSuiteTest':
  1. No runnable methods

    at org.junit.runners.ParentRunner.validate(ParentRunner.java:525)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:102)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:84)
    at org.junit.runners.JUnit4.<init>(JUnit4.java:23)
    at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:37)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70)
    at org.junit.internal.requests.ClassRequest.createRunner(ClassRequest.java:28)
    at org.junit.internal.requests.MemoizingRequest.getRunner(MemoizingRequest.java:19)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

The IDE tries to run the test suite with JUnit 4, but why? Because of @SelectClasses, @IncludeTags? I have to include the dependency

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-runner</artifactId>
    <version>1.7.0-M1</version>
    <scope>test</scope>
</dependency>

to be able to use @SelectClasses and @IncludeTags, but also I excluded the vintage engine. Btw, do I have to remove the exclusion of vintage engine, if running a test suite with @RunWith?

Observation: Including vintage engine: Using @RunWith, Maven > "test", shows the following in the console output:

org.junit.vintage.engine.discovery.DefensiveAllDefaultPossibilitiesBuilder$DefensiveAnnotatedBuilder buildRunner
    WARNING: Ignoring test class using JUnitPlatform runner: ....MyTestSuiteTest

Running the test suite directly with right-click > Run 'MyTestSuiteTest" via IDE shows an additional entry in the unit test view:

MyTestSuiteTest
   JUnit Jupiter
      MyTest1Test
         test2
      MyTest3Test
         test1
   JUnit Vintage
      <no name>

Although there are just two annotated methods to run with the test suite (one from MyTest1Test class, and one from MyTest3Test class), it says 3 test were run successfully. JUnit Vintage didn't show up before when vintage engine was exluded.

Using @ExtendWith with vintage engine included: Maven > "test" doesn't run the test suite, no warning or similar in the console output. "right-click > Run 'MyTestSuiteTest" gives the reported error.

Excluding vintage engine: @RunWith behaves as described initially in the question. @ExtendWith Maven > "test" doesn't run the test suite, no warning or similar in the console output. "right-click > Run 'MyTestSuiteTest" gives the reported error.

来源:https://stackoverflow.com/questions/62514473/spring-boot-2-3-1-junit-5-maven-3-6-3-maven-lifecycle-test-does-not-run-te

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!