SpringBoo編寫測試用例出現異常:java.lang.Exception: No tests found matching
異常可能的原因:
- 沒加
@Test
注解; - 可能是spring-test版本和Junit4不兼容;
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
- 測試用例不能用
static
靜態修飾; - 測試用例需無參無返回值;
- 測試類所在的包名,此
src/test/java
下的包名需要與src/main/java
里邊定義的包名一致; - 方法前需加
public
關鍵字;ps:我使用的junit4 (eclipse自帶的)測試方法必須要加上public作用域(之前沒有加)
完整版:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Test
public void contextLoads() {
System.out.println("測試用例");
}
}