一、异常浮现
1. 代码
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.SpringBootDB</groupId>
<artifactId>SpringBootDB</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringBootDB</name>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Maven src\test\java: com.db.SpringBootDBApplicationTest
package com.db;
import java.sql.Connection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootDBApplicationTest {
@Autowired
private ApplicationContext ioc;
// 运行 junit 测试时, 发生异常
@Test
public void main() throws Exception {
System.out.println("--> ioc=" + ioc);
}
}
2. 异常信息
java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=contextLoads], {ExactMatcher:fDisplayName=contextLoads(com.db.SpringBootDBApplicationTest)], {LeadingIdentifierMatcher:fClassName=com.db.SpringBootDBApplicationTest,fLeadingIdentifier=contextLoads]] from org.junit.internal.requests.ClassRequest@5fcd892a
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest(JUnit4TestLoader.java:77)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:68)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
二、解决
因为没有指定主程序类所以导致异常,
主入口类[使用 @SpringBootApplication(org.springframework.boot.autoconfigure.SpringBootApplication) 注解的 Class]
@SpringBootApplication 来标注一个主程序类, 说明这是一个Spring Boot应用;
所以, 添上主程序即可解决异常:
Maven src/main/java: com.db.SpringBootDBApplication
package com.db;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootDBApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDBApplication.class, args);
}
}
本文分析了SpringBoot项目中单元测试出现的异常原因,并给出了具体的解决方案,强调了正确配置主程序类的重要性。
1763

被折叠的 条评论
为什么被折叠?



