27

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?

3
  • 1
    Have you checked the imports? Maybe you mixed @Test from testng instead of JUnit ? Commented Dec 8, 2017 at 9:14
  • @mrkernelpanic thanks for your comments. I am not sure what you mean. But I have found the cause that another new added testing function has redundant parameters make the validation of test class failed. Commented Dec 11, 2017 at 9:02
  • ok :) I meant your Java Imports. Commented Dec 11, 2017 at 9:04

13 Answers 13

26

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 {
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, instead of running a single JUnit Test all the time, I tried to run the whole test class. Then the error showed that my @AfterClass method was not static. I have wasted like 30 mins before seeing your answer, so thank you.
If you ran the test before with Junit 4 the eclipse run configuration doesn't update to Junit5 when you change your imports later. You need the adapt/delete+recreate the run config
Thanks, running all the tests instead of a single test solved it for me
11

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.

3 Comments

I do the opposite and it works fine for me too
@FabienSalles What exactly do you mean by 'do the opposite'?
I switched from import org.junit.Test to import org.junit.jupiter.api.Test in order to fix the same issue
2

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

Comments

1

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.

Comments

0

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

  }
}

Comments

0

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)

Comments

0

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

Comments

0

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

Comments

0

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

Comments

0

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

Comments

0

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

Comments

0

In addition to all the other good suggestions above, a test method name must begin with test. The name test... is not a convention or suggestion.

@Test
public void testX() {

Comments

0

Make sure that the test class doesn't accidentaly have the @Ignore annotation.

While not applicable to the originaly stated case, same exception occurs as well if somebody for some reason uses this annotation on the class, and you try to run a test without noticing the class-wide ignore.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.