以下是使用 @SpringBootTest
进行集成测试的一些基本步骤:
1. 添加 spring-boot-starter-test
依赖项到 pom.xml 文件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2. 测试类
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.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApplication.class)
class YourTestClassName {
@Autowired
private SomeService someService;
@Test
void testSomeService() {
// 测试某个方法...
}
}