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

13

25

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?

Edme answered 8/12, 2017 at 9:1 Comment(3)
Have you checked the imports? Maybe you mixed @Test from testng instead of JUnit ?Leader
@Leader 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.Edme
ok :) I meant your Java Imports.Leader
E
25

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 {
Edme answered 8/12, 2017 at 9:1 Comment(3)
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.Moderato
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 configFilamentous
Thanks, running all the tests instead of a single test solved it for meCatha
G
9

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.

Giamo answered 19/12, 2019 at 15:28 Comment(0)
T
2

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

Tetrameter answered 20/10, 2020 at 12:49 Comment(0)
M
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.

Masefield answered 26/9, 2020 at 19:50 Comment(0)
G
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"));

  }
}
Grimm answered 19/7, 2019 at 20:52 Comment(0)
V
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)
Vapor answered 9/4, 2020 at 1:7 Comment(0)
R
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

Rame answered 12/6, 2020 at 5:48 Comment(0)
A
0

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

Adaline answered 9/10, 2020 at 9:9 Comment(0)
T
0

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

Township answered 20/8, 2021 at 14:31 Comment(0)
K
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

Kosse answered 2/1, 2022 at 14:11 Comment(0)
P
0

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

Paolapaolina answered 25/4, 2022 at 18:59 Comment(0)
P
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() {
Potentiate answered 8/8, 2023 at 6:24 Comment(0)
F
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.

Fannie answered 31/1 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.