spring web 测试用例

spring web 测试有三种方式

1. 自己初始化 MockMvc

2.依赖@springbootTest 它会帮你生产 webTestClient ,只需自己注入即可。

3.启动的时候,不加载整个应用,进行远程调用   使用WebClient 

 

 

 

 

 

spring web 最常用导入静态方法

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;

 junit 5断言。

import static org.junit.jupiter.api.Assertions.*;

 

 

 

 一、启用测试用例

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { WebMvcConfiguration.class, DataConfiguration.class })
@WebAppConfiguration

二、初始化mvc

    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext wac;

    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

三、常用方法

this.mockMvc.perform(get("/").accept(MediaType.TEXT_HTML)).andExpect(status().isOk())
                .andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML)).andDo(print());

 

@Test
    public void testForm() throws Exception{
        this.mockMvc.perform(post("/").param("text", "text").param("summary", "summary"))
        .andExpect(status().is3xxRedirection()).andDo(print());
    }
    /**
     * 对于 @Valid 无效,后续解决了进行补充
     * @throws Exception
     */
    @Test
    public void testFormFail() throws Exception{
        this.mockMvc.perform(post("/").param("text", "text").param("summary",""))
        .andDo(print());
    }

 如果使用  @SpringBootTest 则可以使用

    @Autowired
    private WebTestClient webTestClient;

    @Test
    public void test1CreateGithubRepository() {
        RepoRequest repoRequest = new RepoRequest("test-webclient-repository", "Repository created for testing WebClient");

        webTestClient.post().uri("/api/repos")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .accept(MediaType.APPLICATION_JSON_UTF8)
                .body(Mono.just(repoRequest), RepoRequest.class)
                .exchange()
                .expectStatus().isOk()
                .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
                .expectBody()
                .jsonPath("$.name").isNotEmpty()
                .jsonPath("$.name").isEqualTo("test-webclient-repository");
    }

使用 webClient

WebClient webClient = WebClient.builder()
        .baseUrl("https://api.github.com")
        .defaultHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.github.v3+json")
        .defaultHeader(HttpHeaders.USER_AGENT, "Spring 5 WebClient")
        .build();

public Flux<GithubRepo> listGithubRepositories(String username, String token) {
     return webClient.get()
            .uri("/user/repos")
            .header("Authorization", "Basic " + Base64Utils
                    .encodeToString((username + ":" + token).getBytes(UTF_8)))
            .exchange()
            .flatMapMany(clientResponse -> clientResponse.bodyToFlux(GithubRepo.class));
}

 

转载于:https://www.cnblogs.com/z-test/p/9367629.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值