springboot-单元测试

service的测试
直接在测试类中注入实例就可以进行测试

package org.akk.test;

import org.akk.test.service.HelloService;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.regex.Matcher;

@SpringBootTest
class TestApplicationTests {
    @Autowired
    HelloService helloService;
    @Test
    void contextLoads() {
        String akk = helloService.sayHello("akk");
        Assert.assertThat(akk, Matchers.is("helloakkk"));
    }
}

controller的测试
用postman或者浏览器测是简单的

Spring中提供了mockMVC 可以模拟http请求 不依赖网络的情况下进行快速测试

keyValue的测试

 @Autowired
    private WebApplicationContext webApplicationContext;
    private MockMvc mockMvc;
    
    //Befor可能会失效
    @BeforeEach
    public void before(){
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @Test
    public void test1() throws Exception {
        //开启一个requestBuilder请求
        MvcResult mvcResult = mockMvc.perform(
        //测试一个hello接口,
        //请求的参数形式keyvalue形式传递的用MediaType.APPLICATION_FORM_URLENCODED
        //param传入的参数
        //andExpect期待  期待返回的东西希望状态返回的是OK
        //andDo 将结果输出
        //andReturn 返回一个mvcResult
                MockMvcRequestBuilders.get("/hello")
                        .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                        .param("name", "akk")).andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
        //mvcResult里面有测试的结果getResponse获取结果 以String的形式打印出来
        System.out.println(mvcResult.getResponse().getContentAsString());
    }

json的测试

  //请求地址不一样了,传参方式变了 ContentType变了 其他都没有变
    @Test
    public void test2() throws Exception {
        Book book = new Book();
        book.setId(99);
        book.setName("三国演义");
        book.setAuther("罗贯中");
        String s = new ObjectMapper().writeValueAsString(book);
        MvcResult mvcResult = mockMvc.perform(
                MockMvcRequestBuilders.post("/book")
                        .contentType(MediaType.APPLICATION_JSON).content(s))
                .andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
        System.out.println(mvcResult.getResponse().getContentAsString());
    }

Spring中还提供RestTemplate 用来发rest请求的工具
SpringBoot中提供了testRestTemplate 测试用
如果使用了@SpringBootTest testRestTemplate 将直接可用 前提是在注解里默认的SpringBootTest.WebEnvironment.MOCK改为DEFINED_PORT或者RANDOM_PORT

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class TestApplicationTests2 {
    @Autowired
    TestRestTemplate testRestTemplate;

    @Test
    public void contextLoads(){
        String akk = testRestTemplate.getForObject("/hello?name={1}", String.class, "akk");
        System.out.println(akk);
    }
}
发布了35 篇原创文章 · 获赞 1 · 访问量 617

猜你喜欢

转载自blog.csdn.net/weixin_39232166/article/details/104848894