问题
While upgrading to JUnit 5 (version 5.5.2), I have made a strange discovery with the suite functionality: my suites can find and run tests that end with the word "Test" but fail to find tests that don't end in "Test" (in my case, they end in "Base").
In JUnit 4, we used the @Suite.SuiteClasses()
annotation to find these tests, but the JUnit 5 @SelectClasses
annotation seems to miss these test classes entirely. Even using @IncludeClassNamePatterns({"^Com.*Base.*?$"})
fails to detect the tests, which I found strange (the tests I want to run start with "Com"). After this, I tried the @Tag()
annotation, which didn't work either.
I assumed this was because Maven Surefire (version 2.22.2) only detects test classes that start with Test, or end with Test, Tests, or TestCase. So, I tried to include my Base test case:
<includes>
<include>**/*Base.java</include>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
<include>**/*TestCase.java</include>
</includes>
Maven was able to run these Base tests when I built this project, but the test suites still failed to find them.
My code will look similar to the following:
@RunWith(JUnitPlatform.class)
@SelectClasses({
Com_TestOne_Base.class,
Com_TestTwo_Base.class,
Com_TestThree_Base.class,
Com_TestFour_Base.class,
Com_TestFive_Base.class,
Com_TestSix_Base.class,
})
public class Com_Base_Suite {
}
The result of running this suite is a success, but no tests actually run. All these tests have been updated to JUnit 5 and run successfully on their own.
回答1:
The problem you're running into results from mixing up JUnit 4 and 5. Maven Surefire is able to run JUnit 5 (aka JUnit platform) tests out of the box - given you have the right dependencies in your pom. See e.g. https://github.com/junit-team/junit5-samples/tree/master/junit5-jupiter-starter-maven for a minimal pom.xml.
JUnitPlatform
, SelectClasses
et al. allow you to run JUnit platform tests through JUnit 4. You'd probably only want to do that if your build tool or IDE do not support the JUnit platform themselves. JUnit 5 does currently not have any explicit support for test suites similar to JUnit 4's @Suite
annotation.
I recommend you get rid of Com_Base_Suite
alltogether and go with a naming convention that can be configured through Maven's <includes>
section.
来源:https://stackoverflow.com/questions/58981160/why-do-junit-5-suite-annotations-selectclasses-and-includeclassnamepatterns-fa