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