0

我已经使用 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
}

聚甲醛是

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

4

2 回答 2

0

两个问题之一(或两个)。

1.) 你需要有一个带有@Test 注解的方法。也许你有它,但这里没有显示。

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

于 2020-09-25T20:57:13.657 回答
0

在spring boot junit test中,出现“java.lang.Exception: No runnable methods”的错误是因为junit框架在测试类中没有找到任何带有@Test注解的测试方法。

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

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

如何重现此问题

在 Spring Boot 应用程序中,在测试环境中,使用没有 @Test 注解的测试方法创建一个测试类。junit 框架使用 @Test 注释搜索方法。由于测试类不包含带有@Test 注解的方法,因此将抛出此错误“java.lang.Exception: No runnable methods”。

例子

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

}

于 2020-09-26T04:17:56.563 回答