SpringBoot设置测试临时属性
- properties:设置properties临时属性
- args:模拟命令行临时参数
- 优先级: args > properties > 配置文件
package com.vmware;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(properties = {"server.port=8090","server.host=192.169.1.2"})
@SpringBootTest(args = {"--server.port=8888","--server.host=192.168.1.3"})
public class TestCase {
@Value("${server.port}")
private Integer port;
@Value("${server.host}")
private String host;
@Test
void testProperties(){
System.out.println(port);
System.out.println(host);
}
}
加载测试专用配置
在测试包下构建测试配置类
package com.vmware.config;
import com.vmware.pojo.Service;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfig {
@Bean
public Service service(){
Service service = new Service();
service.setPort(1999);
service.setHost("localhost");
return service;
}
}
注入测试bean
package com.vmware;
import com.vmware.pojo.Service;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class ConfigTest {
@Autowired
private Service service;
@Test
void testService(){
System.out.println(service);
}
}
- @Configuration注解内包含@Component注解,会将配置类注册到容器中,所以可以自动注入获取到bean
- 如果不使用@Configuration注解,需要在测试类上使用@Import注解将测试类导入进来
SpringBoot开启测试web环境
-
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
-
NONE:不启用web环境(默认)
-
DEFINED_PORT:使用默认端口
-
RANDOM_PORT:使用随机端口
-
MOCK:模拟web环境,不会启动tomcat
模拟http请求
-
@AutoConfigureMockMvc:开启模拟mvc配置
-
注入请求发送对象MockMvc
-
MockMvcRequestBuilders:构建请求
-
MockMvcResultMatchers:构建结果匹配器
package com.vmware;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.result.StatusResultMatchers;
@SpringBootTest(webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class ControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
void testHelloController() throws Exception{
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/hello");
ResultActions resultActions = mockMvc.perform(builder);
StatusResultMatchers status = MockMvcResultMatchers.status();
ResultMatcher matcher = status.isOk();
resultActions.andExpect(matcher); //自动断言
}
}
- 进行文本匹配:MockMvcResultMatchers.content().string()
@Test
void testBody() throws Exception{
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/hello");
ResultActions perform = mockMvc.perform(builder);
ContentResultMatchers contentResultMatchers = MockMvcResultMatchers.content();
ResultMatcher matcher = contentResultMatchers.string("Hello!");
perform.andExpect(matcher);
}
- 进行Json匹配:MockMvcResultMatchers.content().json()
@Test
void testJson() throws Exception{
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/book");
ResultActions resultActions = mockMvc.perform(builder);
ContentResultMatchers matchers = MockMvcResultMatchers.content();
ResultMatcher resultMatcher = matchers.json("{\"name\":\"Java虚拟机\",\"price\":54}");
resultActions.andExpect(resultMatcher);
}
- 进行请求头匹配:MockMvcResultMatchers.header()
@Test
void testContentType() throws Exception{
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/hello");
ResultActions actions = mvc.perform(builder);
HeaderResultMatchers header = MockMvcResultMatchers.header();
ResultMatcher text = header.string("Content-Type", "text/plain;charset=UTF-8");
actions.andExpect(text);
}
测试数据事务回滚
- @Transactional:该注解与@SpringBootTest注解同时使用时会默认执行事务回滚
- @Rollback:是否进行事务回滚,默认值为true,需要与@Transactional注解结合使用
生成测试随机数据
在yml配置文件中设置配置属性:
-
${random.value}:随机一个字符串
-
${random.uuid}:随机生产一个uuid
-
${random.int(0,100)}:随机一个int类型数据,可以指定最小值与最大值的范围
-
${random.long}:随机一个long类型数据,可以指定最小值与最大值的范围
-
注入数据:使用@ConfigurationProperties(value = “xxx”)读取属性