Java 未找到任何测试
我已经使用springboot和junit4编写了一个框架,但是没有发现测试中存在以下错误。 控制台日志显示以下错误:Java 未找到任何测试,java,spring-boot,junit,junit4,springjunit4classrunner,Java,Spring Boot,Junit,Junit4,Springjunit4classrunner,我已经使用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 elaps
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)
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>
org.springframework.boot
弹簧起动试验
2.2.5.1发布
测试
org.junit.vintage
朱尼特老式发动机
朱尼特
朱尼特
4.12
测试
org.hamcrest
汉克雷斯特朱松
2.0.0.0
测试
org.apache.maven.plugins
maven surefire插件
**/*Test*.java
未发现Junit测试(Junit4)两个问题中的一个(或两个)
1.)您需要一个带有@Test注释的方法。也许你有,但这里没有显示
2.)对于JUnit4,您需要vintage测试引擎,但已将其排除在外。在spring boot JUnit测试中,出现错误“java.lang.Exception:No runnable methods”,因为JUnit框架在测试类中找不到任何带有@test注释的测试方法 在junit框架的旧版本中,测试方法由方法签名标识。junit识别以单词“test”开头的方法。它运行测试类中以单词“test”开头的所有方法来执行源代码的单元测试 在junit框架的最新版本中,测试方法由注释@test标识。junit识别测试类中具有@Test注释的所有方法,执行源代码的单元测试 如何重现此问题 在SpringBoot应用程序的测试环境中,使用不具有@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));
}
}我也找不到测试。我从spring中删除了vintage排除。我的测试在另一个扩展TestBase类的类中,但仍然没有测试Foudn错误。我尝试添加an@Test到testbase,它确定了该测试,而不是来自另一个类的测试JUnit4和JUnit5具有不同的@Test,由包来区分。可能你只需要使用另一个“@Test”。我的测试在另一个类中
@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));
}