本文主要介绍Spring Boot如何完成各种不同类型的单元测试
Spring Boot入门系列
- Spring Boot七分钟快速实践
- Spring Boot & MyBatis
- Spring Boot & Redis
- Spring Boot & Swagger
- Spring Boot & 单元测试
- Spring Boot & Actuator
- Spring Boot Admin
Spring基本单元测试
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
- 测试代码
@RunWith(SpringRunner.class)
//启动Spring
@SpringBootTest
public class HelloControllerTest {
@Autowired
private HelloController helloController;
@Test
public void getHello() throws Exception {
final String greeting = helloController.index();
Assert.assertEquals("Hello World!", greeting);
}
}
Mock的方式进行Spring MVC单元测试
@RunWith(SpringRunner.class)
@SpringBootTest
//配置mock
@AutoConfigureMockMvc
public class HelloControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void getHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Hello World!")));
}
}
容器的方式启动进行Spring MVC单元测试
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<scope>test</scope>
</dependency>
- 配置服务器与客户端
@RunWith(SpringRunner.class)
//配置本地随机端口,服务器会选择一个空闲的端口使用,避免端口冲突
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloControllerServerTest {
@Autowired
private WebTestClient webClient;
@Test
public void exampleTest() {
this.webClient.get().uri("/").exchange().expectStatus().isOk()
.expectBody(String.class).isEqualTo("Hello World!");
}
}
使用Spock完成单元测试
pom.xml
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-spring</artifactId>
<version>1.1-groovy-2.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.16</version>
</dependency>
<!--下面是要放到build/plugins下面的-->
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.6.1</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
- 测试代码
import static org.hamcrest.Matchers.equalTo
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
//注意这里不能使用@RunWith(SpringRunner.class)了
@SpringBootTest
@AutoConfigureMockMvc
class HelloControllerTest extends Specification {
@Autowired
private MockMvc mvc
def "mvc"() {
expect:
mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Hello World!")));
}
}
常见错误
Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
@SpringBootTest
搜索@SpringBootApplication
注解的类,是按照test所在类的package往父层级逐级搜索。 所以解决办法一:@SpringBootTest(classes = Application.class)
,解决方案二:修改test所在类的package,放到@SpringBootApplication
子package
网友评论