Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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)

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.

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?

like image 572
Ron Avatar asked Dec 08 '17 09:12

Ron


2 Answers

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:

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.

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 {
like image 61
Ron Avatar answered Sep 19 '22 06:09

Ron


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

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

like image 33
Bradley D Avatar answered Sep 20 '22 06:09

Bradley D