SpringJunit4Runner no tests found

336 views Asked by At

I have written a framework using springboot and junit4 but the tests are not found with the below error. Console logs show the below error:

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.61 sec <<< FAILURE!
initializationError(com.mastercard.mastercom.microservices.tests.testbase.TestBase)  Time elapsed: 0.016 sec  <<< ERROR!
java.lang.Exception: No runnable methods
    at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)

my base class is

SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class, initializers = {
        ConfigFileApplicationContextInitializer.class })
public class TestBase {
//code
}

POM is

   <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <version>2.2.5.RELEASE</version>
                <scope>test</scope>
            <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions> 
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
            
    <dependency>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-junit</artifactId>
                <version>2.0.0.0</version>
                <scope>test</scope>
            </dependency>
    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <includes>
                    <include>**/*Test*.java</include>
                </includes>
            </configuration>
        </plugin>
 

    

No Junit tests are found(Junit4)

2

There are 2 answers

2
Jeff Bennett On

One (or both) of two problems.

1.) You need to have a method with the @Test annotation. Maybe you have it, but it's not shown here.

2.) For JUnit 4, you need the vintage test engine, but you've excluded it.

1
Rajesh Sharma On

In spring boot junit test, the error “java.lang.Exception: No runnable methods” occurs because the junit framework does not find any test method with the @Test annotation in the test class.

In the older version of the junit framework, the test method is identified with a method signature. The junit identifies the methods that begin with the word “test.” It runs all the methods starting with the word “test” in the test class to perform the unit test of the source code.

In the recent version of the junit framework, the test method is identified by an annotation @Test. The junit identifies all the methods that have the @Test annotation in the test class, performs a unit test of the source code.

How to reproduce this issue

In the spring boot application, in the test environment, create a test class with test methods that do not have a @Test annotation. The junit framework searches methods with a @Test annotation. Since the test class does not contain a method with a @Test annotation, this error “java.lang.Exception: No runnable methods” will be thrown.

Example

@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentTestController{
@Autowired
private WebApplicationContext webApplicationContext;

private MockMvc mockMvc;

@Before
public void setup() {
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}

@Test
public void testStudent() throws Exception {
    mockMvc.perform(get("/student")).andExpect(status().isOk())
            .andExpect(content().contentType("application/json"))
            .andExpect(jsonPath("$.studentId").value("1"))
            .andExpect(jsonPath("$.name").value("student1"))
            .andExpect(jsonPath("$.age").value(30));
}

}