如何使用MockMvc进行controller层单元测试
这篇文章主要介绍了如何使用MockMvc进行controller层单元测试,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
看代码吧~
package com.ieou.ms_backend.controller; import com.google.gson.Gson; import com.ieou.ms_backend.dto.account.CreateAccountReq; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvcBuilder; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.ResultMatcher; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultHandlers; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.context.WebApplicationContext; import static org.junit.Assert.*; /** * created by wyz on 2019/5/6 */ @SpringBootTest @RunWith(SpringJUnit4ClassRunner.class) public class AccountControllerTest { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; private String url = "/ms_backend/account/"; @Before public void setUp() throws Exception{ //初始化MockMvc对象 mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); } //GET 请求 @Test public void accountList() throws Exception { MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(url + "accountList") .param("companyName", "wang") .header("access_token", "accessToken"); mockHttpServletRequestBuilder.accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON_UTF8); ResultActions resultActions = mockMvc.perform(mockHttpServletRequestBuilder); resultActions.andReturn().getResponse().setCharacterEncoding("UTF-8"); resultActions.andExpect(MockMvcResultMatchers.status().isOk()); resultActions.andDo(MockMvcResultHandlers.print()); } @Test public void removeAccount() { } //post 请求 @RequestBody @Test @Transactional @Rollback() // 事务自动回滚,默认是true。可以不写 public void createAccount() throws Exception { CreateAccountReq req = new CreateAccountReq(); MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.post(url + "createAccount") .header("access_token", "accessToken"); mockHttpServletRequestBuilder.accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON_UTF8) .content(new Gson().toJson(req)); // post请求 ResultActions resultActions = mockMvc.perform(mockHttpServletRequestBuilder); resultActions.andReturn().getResponse().setCharacterEncoding("UTF-8"); resultActions.andExpect(MockMvcResultMatchers.status().isOk()); resultActions.andDo(MockMvcResultHandlers.print()); } }
Spring-test 单元测试数据不自动回滚的解决方案
在使用JUnit做单元测试时,为了使测试数据不对数据库造成污染,故选取了spring-test进行单元测试,但在进行dao层的测试时,发现save方法无法进行数据的自动回滚。
逐一进行检查分析,事务也开启了,并使用注解方式标记@RollBack(true),在控制台中也打出了事务回滚的信息,但仍然不能自动回滚。
后来感觉是数据库的原因,我的数据库使用的是MySql,这就存在数据表的类型是否支持事务情况。
逐查阅MySql相关文档,发现,InnoDB类型的表是支持事务的,而MyISAM是不支持事务的,立刻查看数据表类型,果然为MyISAM,改为InnoDB后,重新进行测试,问题得到解决。
感谢你能够认真阅读完这篇文章,希望小编分享的“如何使用MockMvc进行controller层单元测试”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注行业资讯频道,更多相关知识等着你来学习!