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

后端 未结 8 1492
别那么骄傲
别那么骄傲 2021-01-01 18:59

When running JUnit testing , it gave an initializationError: No tests found matching. Like this:

prodapi-main-junit
initializationError(org.junit.runner.man         


        
相关标签:
8条回答
  • 2021-01-01 19:36

    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"));
    
      }
    }
    
    0 讨论(0)
  • 2021-01-01 19:38

    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.

    0 讨论(0)
  • 2021-01-01 19:39

    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

    0 讨论(0)
  • 2021-01-01 19:42

    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)
    
    0 讨论(0)
  • 2021-01-01 19:47

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

    0 讨论(0)
  • 2021-01-01 19:48

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

    0 讨论(0)
提交回复
热议问题