JUnit testing got initializationError with java.lang.Exception: No tests found matching

Ron*_*Ron 6 junit spring unit-testing

When running JUnit testing , it gave an initializationError: No tests found matching. Like this:

prodapi-main-junit
initializationError(org.junit.runner.manipulation.Filter)
java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=testCreateSite], {ExactMatcher:fDisplayName=testCreateSite(com.company.product.api.web.rest.HostControllerTest)], {LeadingIdentifierMatcher:fClassName=com.company.product.api.web.rest.HostControllerTest,fLeadingIdentifier=testCreateSite]] from org.junit.internal.requests.ClassRequest@3c0f93f1
    at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest(JUnit4TestLoader.java:77)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:68)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Run Code Online (Sandbox Code Playgroud)

While the testing code is as below:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ProductApplication.class)
@WebAppConfiguration
public class HostControllerTest{
    @Test
    public void testCreateSite() throws Exception {
    ......
}
}
Run Code Online (Sandbox Code Playgroud)

It should be fine to load the class, running well. There're other modules similar with this one, they're fine to run.

I have checked the possible causes:

  1. someone said that missing "Test" annotation result in this error. While the code did have the annotation as you can see.
  2. some said the build path should be configured to do the build under the testing source folder, getting the testing class, and export. And that configuration I double-checked worked as well.
  3. maybe testing classes are not generated in the compile time, while I can see those testing classes under the destination folder.

I don't know whether there're any other possible things can get Junit testing error like this. Maybe I should check the class loader?

Ron*_*Ron 6

谷歌搜索后的一些答案。我发现有一个关于这种情况的问题,如下所示:https : //github.com/junit-team/junit4/issues/1277(FilterRequest可能会隐藏测试的真正失败原因(异常))这是我的步骤尝试过:
1.不要单独选择测试功能,而是在“测试”选项卡上选择“运行所选项目中的所有测试”选项,然后单击“运行”->“运行(调试)配置”后选择Junit项目名称。
2.您可以按以下方式获取错误的详细信息:

initializationError(com.company.product.api.web.rest.HostControllerTest)
java.lang.Exception: Method testSetDBConfiguration should have no parameters
    at org.junit.runners.model.FrameworkMethod.validatePublicVoidNoArg(FrameworkMethod.java:76)
    at org.junit.runners.ParentRunner.validatePublicVoidNoArgMethods(ParentRunner.java:155)
Run Code Online (Sandbox Code Playgroud)

3.根据上面的eclipse给出的细节,我删除了该函数的参数,initializedError消失了。

因此,由于新添加的测试功能具有不必要的输入参数,因此出现了此问题。错误的代码:

@Test
public void testSetDBConfiguration(String name) throws Exception {
Run Code Online (Sandbox Code Playgroud)

变成

@Test
public void testSetDBConfiguration() throws Exception {
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,我没有一直运行单个JUnit Test,而是尝试运行整个测试类。然后错误表明我的@AfterClass方法不是静态的。在看到您的答案之前,我已经浪费了30分钟,所以谢谢您。 (2认同)

Bra*_*y D 6

PowerMock 有同样的问题,@RunWith(PowerMockRunner.class)然后发现我@Test的实现是错误的。正在使用 import org.junit.jupiter.api.Test

我切换到import org.junit.Test;并解决了我的问题。