【问题标题】:JUnit testing got initializationError with java.lang.Exception: No tests found matchingJUnit 测试得到了initializationError with java.lang.Exception:没有找到匹配的测试
【发布时间】:2018-05-22 11:47:57
【问题描述】:

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

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)

而测试代码如下:

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

加载类应该没问题,运行良好。还有其他与此类似的模块,它们可以运行。

我已经检查了可能的原因:

  1. 有人说缺少“测试”注释会导致此错误。 如您所见,代码确实具有注释。
  2. 有人说构建路径应该配置为在测试源文件夹下构建,获取测试类,然后导出。 我仔细检查的那个配置也很有效。
  3. 可能在编译时没有生成测试类,而我可以在目标文件夹下看到那些测试类。

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

【问题讨论】:

  • 你检查过进口吗?也许你混合了来自 testng 的 @Test 而不是 JUnit
  • @mrkernelpanic 感谢您的 cmets。我不确定你的意思。但是我发现另外一个新添加的测试函数有冗余参数导致测试类的验证失败。
  • ok :) 我的意思是你的 Java 导入。

标签: spring unit-testing junit


【解决方案1】:

在搜索了一些答案之后。我发现有一个关于这个案例的问题如下: https://github.com/junit-team/junit4/issues/1277 (FilterRequest 可能隐藏测试的真正失败原因(异常)) 以下是我尝试的步骤:
1.不要单独选择测试功能,在选择Junit项目名称时,在Test选项卡上选择“Run all the tests in the selected project”选项 点击运行->“运行(调试)配置”
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.根据上面eclipse给出的细节,我去掉了那个函数的参数,initializedError就消失了。

所以这个问题是由于新添加的测试功能有不必要的输入参数。 错误代码:

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

改为

@Test
public void testSetDBConfiguration() throws Exception {

【讨论】:

  • 谢谢,我没有一直运行一个 JUnit 测试,而是尝试运行整个测试类。然后错误表明我的@AfterClass 方法不是静态的。在看到你的答案之前我已经浪费了大约 30 分钟,所以谢谢。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    我找到了以下对我有用的解决方案。

    你的应用程序 @SpringBootApplication 包名 和测试包名称应该相同。

    看看有没有帮助。 编码愉快。

    【讨论】:

      【解决方案4】:

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

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

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

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


      主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() {
            //
          }
      }
      

      我的测试课

      **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"));
      
        }
      }
      

      【讨论】:

        【解决方案5】:

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

        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)
        

        【讨论】:

          【解决方案6】:

          检查以下情况:

          1. 你用过org.junit.jupiter.api.Test吗?而不是 org.junit.Test; 用于运行 Junit 4 测试用例?
          2. 您是否将参数传递给 测试方法如下图:

          public void test(String arg){ assertTrue(true); }

          1. 重建项目也可以:

            mvn 全新安装

          【讨论】:

            【解决方案7】:

            如果它是一个 Maven 项目运行 eclipse:eclipse 作为一个 Maven 构建。这解决了我的问题。

            【讨论】:

              【解决方案8】:

              首先能够确保你的方法注释 @test 以及测试类是公开的。

              【讨论】:

                【解决方案9】:

                当您使用gradle 时(并且您使用codium 作为IDE) 您可能需要手动重建它,例如./gradlew build

                【讨论】:

                  【解决方案10】:

                  是的,在 VSCode 测试视图中,我经常在我的基于 Java JUnit 的 JPA 测试中遇到可怕的红色“InitializationError”,并且永远无法弄清楚。根据 Mohit Basak 的评论,我改变了

                  @SpringBootTest(classes = {MyApp.class})
                  public class MyTest {
                  ...
                  }
                  

                  @SpringBootTest(classes = {com.xyz.MyApp.class})
                  public class MyTest {
                  ...
                  }
                  

                  到目前为止,我认为它现在工作得更好。测试总是运行绿色/完整,甚至在命令行中运行良好gradle build

                  【讨论】:

                    猜你喜欢
                    • 2016-03-07
                    • 1970-01-01
                    • 2019-07-09
                    • 2017-04-13
                    • 1970-01-01
                    • 2014-04-23
                    • 2015-09-04
                    • 2018-08-22
                    • 1970-01-01
                    相关资源
                    最近更新 更多