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

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?



Solution 1:[1]

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 {

Solution 2:[2]

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.

Solution 3:[3]

I have found the below solution which worked for me.

your Application @SpringBootApplication package name and Test package name should be same .

see if it helps. Happy coding.

Solution 4:[4]

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

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

Test class was in package - 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

@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"));

  }
}

Solution 5:[5]

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

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)

Solution 6:[6]

Check for below conditions:

  1. Have you used org.junit.jupiter.api.Test; instead of org.junit.Test; for running Junit 4 test cases?
  2. Have you passed an argument to the test method as shown below:

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

  1. Re-building a project will also work:

    mvn clean install

Solution 7:[7]

If it is a maven project run eclipse:eclipse as a Maven build.That resolved my problem.

Solution 8:[8]

First able Make sure that your methods annotated @test as well as the test class are public.

Solution 9:[9]

When you use gradle (and you use codium as IDE) you may need to rebuild it manually, e.g. with ./gradlew build.

Solution 10:[10]

Yup, in VSCode Testing view, I often get the horrible red "InitializationError" for my Java JUnit-based JPA tests and could never figure it out. Based on Mohit Basak comment, I changed

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

to

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

and so far I think it's working better now. The tests always ran green/complete and even runs fine from command line with gradle build

Solution 11:[11]

I had a similar error when my test method was private. It needs to be public.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Bradley D
Solution 3 Mohit Basak
Solution 4 K.D
Solution 5 Alisson Gomes
Solution 6
Solution 7 Darko
Solution 8 Shadyar
Solution 9 dr0i
Solution 10 robm99x
Solution 11 Srikanth Chadalavada