SpringBoot-测试

测试:

(1)加入临时属性:

给测试添加临时的属性 : 临时属性可以覆盖掉配置文件中的属性值

优势 : 作用范围小 , 仅仅影响测试代码的范围

@SpringBootTest(properties = {"test.prop = test1",""}) //设置临时属性 , 这里要加一个{}
public class PropertiesAndArgsTest {

    @Value("${test.prop}") //可以获取配置文件中的属性值 , 也可以获取临时属性的值
    private String msg;

    @Test
    void testProperties(){
        System.out.println(msg); //输出 test1
    }
}

也可以使用args进行设置值

@SpringBootTest(args = {"--test.prop = test1",""}) //设置临时属性 , 这里要加一个{}
public class PropertiesAndArgsTest {

    @Value("${test.prop}") //可以获取配置文件中的属性值 , 也可以获取临时属性的值
    private String msg;

    @Test
    void testProperties(){
        System.out.println(msg); //输出 test1
    }
}

(2)临时加载一个bean

@SpringBootTest
@Import({com.sichen.config.MsgConfig.class}) 
//加载资源进测试环境 , 可以为当前的测试类导入专用的配置 ,
//注意导入的资源必须要是一个bean
public class MsgConfig {

    @Autowired  //使用自动装配
    private String msg;
    
}

(3)表现层进行测试

1.测试类中添加web环境

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class WebTest {
    @Test
    void Test(){
    }
}
属性名 含义 默认值
MOCK 虚拟的 false
NONE 不启动web环境 false
DEFINED_PORT 使用默认的端口(就是你默认是使用哪个端口) true
RANDOM_PORT 使用的是随机的端口,(测试你的代码端口是不是写死的) true

2.发送虚拟请求 :

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@AutoConfigureMockMvc //开启虚拟mvc调用
public class WebTest {
    @Test
    void TestWeb(@Autowired MockMvc mvc) throws Exception {
        //创建模拟请求 , 当前访问 /books
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
        //执行对应的请求
        mvc.perform(builder);
    }
}

3.匹配响应执行状态 :

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@AutoConfigureMockMvc //开启虚拟mvc调用
public class WebTest {

    @Test
    void TestWeb(@Autowired MockMvc mvc) throws Exception {
        //创建模拟请求 , 当前访问 /books
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
        //执行对应的请求
        ResultActions actions = mvc.perform(builder);
        //设置预期值 , 与真实值进行比较 , 成功测试通过 , 失败测试失败
        //定义本次调用的预期值
        StatusResultMatchers status = MockMvcResultMatchers.status();
        //预计本次调用时成功的:状态 200
        ResultMatcher ok = status.isOk();
        actions.andExpect(ok);
    }
}

4.匹配响应体:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@AutoConfigureMockMvc //开启虚拟mvc调用
public class WebTest {
    
@Test
    void TestBody(@Autowired MockMvc mvc) throws Exception {
        //创建模拟请求 , 当前访问 /books
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
        //执行对应的请求
        ResultActions actions = mvc.perform(builder);

        //设置预期值 , 与真实值进行比较 , 成功测试通过 , 失败测试失败
        //定义本次调用的预期值
        ContentResultMatchers content = MockMvcResultMatchers.content();
        //预计本次调用时成功的:状态 200
        ResultMatcher springboot = content.string("springboot");
        actions.andExpect(springboot);
    }
}

5.匹配响应体(Json):

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@AutoConfigureMockMvc //开启虚拟mvc调用
public class WebTest {
    
@Test
    void TestBody(@Autowired MockMvc mvc) throws Exception {
        //创建模拟请求 , 当前访问 /books
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
        //执行对应的请求
        ResultActions actions = mvc.perform(builder);

        //设置预期值 , 与真实值进行比较 , 成功测试通过 , 失败测试失败
        //定义本次调用的预期值
        ContentResultMatchers content = MockMvcResultMatchers.content();
        //预计本次调用时成功的:状态 200
        ResultMatcher springboot = content.json(json的字符串);
        actions.andExpect(springboot);
    }
}

(4)表现层测试事务回滚:

在业务层的测试类中添加一个注解

@Transactional //这个注解就是关闭提交事务的 , 这样测试数据就不会保存了 , 但是 , 数据库的id值会增加

(5)测试用例设置随机值

#在配置文件中设置 
testcase: 
  book:
  	id: ${random.int}
含义
int 产生一个int类型的随机数(可以指定开始范围和结束范围)
long 产生一个long类型(整数)的随机数(可以指定开始范围和结束范围)
uuid 产生一个uuid的随机值
value 产生一个随机的MD5加密的字符串 , 32位的

在生成int类型的值的时候 , 可以限定生成的范围 :

${random.int(100)}
//结束范围就是100

${random.int(50,100)}
//生成的范围就是50到100

你可能感兴趣的:(SpringBoot,SpringBoot)