SpringJunit4Runner未找到测试

问题描述

我已经使用springboot和junit4编写了一个框架,但是没有发现测试并出现以下错误。 控制台日志显示以下错误

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)

我的基类是

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

POM是

   <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>
 

    

未找到Junit测试(Junit4)

解决方法

两个问题中的一个(或两个)。

1。)您需要具有带有@Test批注的方法。也许您有,但是这里没有显示。

2。)对于JUnit 4,您需要老式测试引擎,但已将其排除在外。

,

在spring boot junit测试中,由于junit框架在测试类中找不到带有@Test批注的任何测试方法,因此发生错误“ java.lang.Exception:没有可运行的方法”。

在旧版本的junit框架中,测试方法使用方法签名来标识。 junit标识以单词“ test”开头的方法。它运行测试类中所有以“ test”开头的方法,以执行源代码的单元测试。

在最新版本的junit框架中,测试方法由@Test注释标识。 junit标识测试类中所有具有@Test批注的方法,并对源代码执行单元测试。

如何重现此问题

在spring boot应用程序的测试环境中,使用没有@Test批注的测试方法创建一个测试类。 junit框架使用@Test注释搜索方法。由于测试类不包含带有@Test批注的方法,因此将引发此错误“ java.lang.Exception:没有可运行的方法”。

示例

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

}