Junit对springMVC controller层测试

//指定使用的单元测试执行类

@RunWith(SpringRunner.class)

@WebAppConfiguration

//指定spring配置文件的指定路径,需要所有spring配置文件全部加载

@ContextConfiguration("test-servlet-context.xml")

public class ExampleTests {

 

    //容器

    @Autowired

    private WebApplicationContext wac;

   

    //MockMvc是springMVC提供的controller测试类

    private MockMvc mockMvc;

 

    //setup方法在每个方法执行之前都会执行,加载配置文件

    @Before

    public void setup() {

        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();

    }

 

    @Test

    public void getAccount() throws Exception {

     // get("/accounts/1")请求的URL,get请求

   this.mockMvc.perform(get("/accounts/1").accept(MediaType.parseMediaType("application/json;charset=UTF-8")))//数据格式

            .andExpect(status().isOk())//断言返回状态

            .andExpect(content().contentType("application/json"))//断言contentType

            .andExpect(jsonPath("$.name").value("Lee"))//断言json中key为name的value是否为Lee

    .andDo(print());//打印对应的请求和数据到控制台

    }

 

}

转载于:https://www.cnblogs.com/tulei/p/6768799.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值