软件测试mvc,WebMvcTest和MockBean测试Controller

@WebMvcTest注解简介

Spring框架提供了@WebMvcTest这一注解来配置Controller的上下文环境,以帮助实现对Controller层的测试。从中可以看出,

只实例化Controller。默认实例化所有的Controller,也可以指定只实例化某一到多个Controller。

会实例化一个MockMvc的bean,用于模拟收发http请求和响应。

默认搜索@SpringBootConfiguration注解的类作为配置类。(这里坑最多)

通过properties 指定运行时的参数

具体可以参见 spring.factories

以下是这个注解的定义,

package org.springframework.boot.test.autoconfigure.web.servlet;

//import

@Target({ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Inherited

@BootstrapWith(WebMvcTestContextBootstrapper.class)

@ExtendWith({SpringExtension.class})

@OverrideAutoConfiguration(

enabled = false

)

@TypeExcludeFilters({WebMvcTypeExcludeFilter.class})

@AutoConfigureCache

@AutoConfigureWebMvc

@AutoConfigureMockMvc

@ImportAutoConfiguration

public @interface WebMvcTest {

String[] properties() default {};

@AliasFor("controllers")

Class>[] value() default {};

@AliasFor("value")

Class>[] controllers() default {};

boolean useDefaultFilters() default true;

Filter[] includeFilters() default {};

Filter[] excludeFilters() default {};

@AliasFor(

annotation = ImportAutoConfiguration.class,

attribute = "exclude"

)

Class>[] excludeAutoConfiguration() default {};

}

接下来看一下,如何通过@WebMvcTest搭配MockMvc和@MockBean来进行单个Controller的测试

案例1-单个Controller的测试

package com.testlink4j.controller;

//import

@WebMvcTest(KeywordsRestController.class)

public class KeywordsControllerMockBean2Test {

@MockBean

private KeywordsService keywordsServic;

@Autowired

private MockMvc mockMvc;

@Test

public void CreateKeywordsSuccessfullyTest() throws Exception {

Keywords keywords=Keywords.builder().id(666).keyword("tester").testproject_id(333).notes("tester").build();

Mockito.when(keywordsServic.findKeywordById(1)).thenReturn(keywords);

String responseString = mockMvc.perform(

get("/api/keywords?id=1") //请求的url,请求的方法是Post

.contentType(MediaType.APPLICATION_JSON) //数据的格式

// .content(jsonString) //body

).andExpect(status().isOk()) //返回的状态是200

.andDo(print()) //打印出请求和相应的内容

.andReturn().getResponse().getContentAsString(); //将相应的数据转换为字符串

assertThatJson(responseString).isEqualTo(keywords);

assertThatJson(responseString).isEqualTo(keywords);

}

}

以上代码实现了

1在Sping容器中只注入指定的KeywordsRestController,

2并通过@MockBean将该Controller对Service的依赖进行了mock 。在测试时,

3通过MockMvc模拟发送HTTP GET请求,并对http响应的状态码进行了验证,

4对返回的结果也进行了验证。

案例2-请求格式校验

再编写一个针对入参的校验用例

(@RequestParam(value = "id", required = true)

也就是说,id这个字段是必填项

@Test

public void CreateKeywordsNoIdProvidedTest() throws UnsupportedEncodingException, Exception {

mockMvc.perform(

get("/api/keywords?idd=1") //请求的url,请求的方法是Get

.contentType(MediaType.APPLICATION_JSON) //数据的格式

// .content(jsonString) //body

).andExpect(status().is(400)) //返回的状态是400

.andDo(print()); //打印出请求和相应的内容

}

}

以上用例假设因为笔误,id错写成了idd,用例预期HTTP状态码会从200变成400 Bad Request ,即客户端错误。

以下是Spring1.4引入的各层进行单测的注解

@WebMvcTest - for testing the controller layer

@JsonTest - for testing the JSON marshalling and unmarshalling

@DataJpaTest - for testing the repository layer

@RestClientTests - for testing REST clients

本次主要介绍了@WebMvcTest,后续我们会继续涵盖其它注解。

By 软-件-测试-那-些 事

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值