spring JUnit 测试得到了带有 java.lang.Exception 的初始化错误:没有找到匹配的测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47710837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:32:47  来源:igfitidea点击:

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

springunit-testingjunit

提问by Ron

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

运行 JUnit testing 时,它给出了一个初始化错误:没有找到匹配的测试。像这样:

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)

While the testing code is as below:

而测试代码如下:

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

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.
  1. 有人说缺少“测试”注释会导致此错误。虽然代码确实有注释,如您所见。
  2. 有人说应该将构建路径配置为在测试源文件夹下进行构建,获取测试类并导出。我仔细检查过的配置也有效。
  3. 也许在编译时没有生成测试类,而我可以在目标文件夹下看到那些测试类。

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?

我不知道是否还有其他可能的事情会导致像这样的 Junit 测试错误。也许我应该检查类加载器?

回答by Ron

After googled some answers. I found there's an issue talked about this case as below: https://github.com/junit-team/junit4/issues/1277(FilterRequest may hide the real failure cause(exception) of a test) Here're the steps I tried:
1. don't select the testing function alone, while select "Run all the tests in the selected project" option on the Test Tab, when select the Junit project name after click on Run->"Run(Debug) Configuration"
2. You can get the details of the error as follows:

在谷歌搜索了一些答案之后。我发现有一个关于这个案例的问题如下: 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)

3.according to the details given by eclipse above, I removed the argument of that function, the initializedError just disappeared.

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

So this issue rises due to the new added testing function has unnecessary input argument. The incorrect code :

因此,由于新添加的测试函数具有不必要的输入参数,因此出现此问题。不正确的代码:

@Test
public void testSetDBConfiguration(String name) throws Exception {

Changed to

变成

@Test
public void testSetDBConfiguration() throws Exception {

回答by Bradley D

Had the same issue with PowerMock @RunWith(PowerMockRunner.class)then discovered that my @Testwas the wrong implementation. Was using import org.junit.jupiter.api.Test;

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

I switched to import org.junit.Test;and that fixed the problem for me.

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

回答by Alisson Gomes

I had to add the hamcrest-all-1.3.jarinto classpath to run unit test.

我必须添加hamcrest-all-1.3.jar到类路径来运行单元测试。

junit 4.12

junit 4.12

java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=myTest], {ExactMatcher:fDisplayName=scannerTest(example.JavaTest)], {LeadingIdentifierMatcher:fClassName=example.JavaTest,fLeadingIdentifier=myTest]] from org.junit.internal.requests.ClassRequest@38af3868
    at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40)

回答by Prashant Rajput

I had the similar problem and later realized that my main spring boot application configuration was not scanning through the packages that had my test classes in

我遇到了类似的问题,后来意识到我的主要 Spring Boot 应用程序配置没有扫描包含我的测试类的包

Main class was scanning packages - {"com.mycmp.prj.pkg1", "com.mycmp.prj.pkg2", "com.mycmp.dependentprj.pkg5"}

主要类是扫描包 - {"com.mycmp.prj.pkg1", "com.mycmp.prj.pkg2", "com.mycmp.dependentprj.pkg5"}

Test class was in package - com.mycmp.prj.pkg3

测试类在包中 - com.mycmp.prj.pkg3

Problem got fixed by fixing our base packages to scan all packages from current project and only scan limited needed packages from dependent libraries

通过修复我们的基本包以扫描当前项目中的所有包并仅扫描依赖库中的有限需要的包来解决问题



Main java class

主Java类

@SpringBootApplication(scanBasePackages = {"com.mycmp.prj.pkg1", "com.mycmp.prj.pkg2", "com.mycmp.dependentprj.pkg5"})
public class MyApplication extends SpringBootServletInitializer {

    public static void main(final String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
        return application.sources(MyApplication.class);
    }

    @Bean
    public FilterRegistrationBean<Filter> customFilters() {
      final FilterRegistrationBean<Filter> registration = new 
      FilterRegistrationBean<>();
      final Filter myFilter = new ServicesFilter();
      registration.setFilter(myFilter);
      registration.addUrlPatterns("/myurl1/*", "/myurl2/*");
      return registration;
    }

    @PostConstruct
    public void started() {
      //
    }
}

My Test Class

我的测试班

**package com.mycmp.prj.pkg3;**

import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.mongodb.MongoClient;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApplication.class)
public class MongoConfigStoreTest {

  @Test
  public void testConnection() throws Exception {

    final MongoClient client = new MongoClient("localhost", 127027);
    assertNotNull(client);
    assertNotNull(client.getDatabase("localhost"));

  }
}