java测试时找不到类,春季-JUnit测试使用java.lang.Exception引发了InitializationError:找不到匹配的测试...

当运行JUnit测试时遇到初始化错误,找不到匹配的测试,可能是由于多种原因导致的。这个问题在一个具体的测试类中被发现,尽管代码包含了正确的测试注释和配置。错误详细信息显示了一个带有参数的方法导致了初始化错误。通过删除该测试方法的参数,初始化错误得以解决,表明不正确的测试方法签名可能是触发此问题的原因。
摘要由CSDN通过智能技术生成

运行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 {

......

}

}

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

我检查了可能的原因:

>有人说缺少“测试”注释会导致此错误.

如您所见,代码确实具有注释.

>有人说构建路径应该配置为在测试源文件夹下进行构建,获取测试类并导出.

我仔细检查过的配置也很好用.

>也许测试类不是在编译时生成的,而我可以在目标文件夹下看到这些测试类.

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

解决方法:

谷歌搜索后的一些答案.我发现有一个讨论此案的问题,如下所示:

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.根据上面的eclipse给出的细节,我删除了该函数的参数,initializedError消失了.

因此,由于新添加的测试功能具有不必要的输入参数,因此出现了此问题.

错误的代码:

@Test

public void testSetDBConfiguration(String name) throws Exception {

变成

@Test

public void testSetDBConfiguration() throws Exception {

标签:unit-testing,junit,spring

来源: https://codeday.me/bug/20191110/2014449.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值