MockMvc 进行 controller层单元测试 事务自动回滚 完整实例
1 package com.ieou.ms_backend.controller; 2 3 import com.google.gson.Gson; 4 import com.ieou.ms_backend.dto.account.CreateAccountReq; 5 import org.junit.Before; 6 import org.junit.Test; 7 import org.junit.runner.RunWith; 8 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.boot.test.context.SpringBootTest; 10 import org.springframework.http.MediaType; 11 import org.springframework.test.annotation.Rollback; 12 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 import org.springframework.test.web.servlet.MockMvc; 14 import org.springframework.test.web.servlet.MockMvcBuilder; 15 import org.springframework.test.web.servlet.ResultActions; 16 import org.springframework.test.web.servlet.ResultMatcher; 17 import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; 18 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 19 import org.springframework.test.web.servlet.result.MockMvcResultHandlers; 20 import org.springframework.test.web.servlet.result.MockMvcResultMatchers; 21 import org.springframework.test.web.servlet.setup.MockMvcBuilders; 22 import org.springframework.transaction.annotation.Transactional; 23 import org.springframework.web.context.WebApplicationContext; 24 25 26 import static org.junit.Assert.*; 27 28 /** 29 * created by wyz on 2019/5/6 30 */ 31 32 @SpringBootTest 33 @RunWith(SpringJUnit4ClassRunner.class) 34 public class AccountControllerTest { 35 36 @Autowired 37 private WebApplicationContext wac; 38 39 private MockMvc mockMvc; 40 private String url = "/ms_backend/account/"; 41 42 @Before 43 public void setUp() throws Exception{ 44 //初始化MockMvc对象 45 mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); 46 } 47 48 49 //GET 请求 50 51 @Test 52 public void accountList() throws Exception { 53 54 MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(url + "accountList") 55 .param("companyName", "wang") 56 .header("access_token", "accessToken"); 57 58 mockHttpServletRequestBuilder.accept(MediaType.APPLICATION_JSON) 59 .contentType(MediaType.APPLICATION_JSON_UTF8); 60 61 ResultActions resultActions = mockMvc.perform(mockHttpServletRequestBuilder); 62 resultActions.andReturn().getResponse().setCharacterEncoding("UTF-8"); 63 resultActions.andExpect(MockMvcResultMatchers.status().isOk()); 64 resultActions.andDo(MockMvcResultHandlers.print()); 65 } 66 67 @Test 68 public void removeAccount() { 69 } 70 71 //post 请求 @RequestBody 72 73 @Test 74 @Transactional 75 @Rollback() // 事务自动回滚,默认是true。可以不写 76 public void createAccount() throws Exception { 77 78 CreateAccountReq req = new CreateAccountReq(); 79 80 MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.post(url + "createAccount") 81 .header("access_token", "accessToken"); 82 83 mockHttpServletRequestBuilder.accept(MediaType.APPLICATION_JSON) 84 .contentType(MediaType.APPLICATION_JSON_UTF8) 85 .content(new Gson().toJson(req)); // post请求 86 87 ResultActions resultActions = mockMvc.perform(mockHttpServletRequestBuilder); 88 resultActions.andReturn().getResponse().setCharacterEncoding("UTF-8"); 89 resultActions.andExpect(MockMvcResultMatchers.status().isOk()); 90 resultActions.andDo(MockMvcResultHandlers.print()); 91 } 92 }
hello world!!!