使用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 單元測試數(shù)據(jù)不自動回滾的解決方案
在使用JUnit做單元測試時,為了使測試數(shù)據(jù)不對數(shù)據(jù)庫造成污染,故選取了spring-test進行單元測試,但在進行dao層的測試時,發(fā)現(xiàn)save方法無法進行數(shù)據(jù)的自動回滾。
逐一進行檢查分析,事務也開啟了,并使用注解方式標記@RollBack(true),在控制臺中也打出了事務回滾的信息,但仍然不能自動回滾。
后來感覺是數(shù)據(jù)庫的原因,我的數(shù)據(jù)庫使用的是MySql,這就存在數(shù)據(jù)表的類型是否支持事務情況。
逐查閱MySql相關(guān)文檔,發(fā)現(xiàn),InnoDB類型的表是支持事務的,而MyISAM是不支持事務的,立刻查看數(shù)據(jù)表類型,果然為MyISAM,改為InnoDB后,重新進行測試,問題得到解決。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Java中finally和return的執(zhí)行順序
try-catch-finally是一種針對程序運行時出錯的響應手段,對于一些可以預料到的出錯類型,在發(fā)生時對其進行報告和補救,這篇文章主要介紹了Java中finally和return的執(zhí)行順序,需要的朋友可以參考下2024-01-01
SpringBoot中動態(tài)數(shù)據(jù)源配置與使用詳解
在現(xiàn)代應用中,處理多數(shù)據(jù)源是常見的需求,在 Spring Boot 中,這樣的需求可以通過動態(tài)數(shù)據(jù)源來輕松實現(xiàn),本篇博客將詳細介紹如何在 Spring Boot 中配置和使用動態(tài)數(shù)據(jù)源,并演示如何切換到指定的數(shù)據(jù)源,需要的朋友可以參考下2024-10-10
一場由Java中Integer引發(fā)的踩坑實戰(zhàn)
Java中的數(shù)據(jù)類型分為基本數(shù)據(jù)類型和復雜數(shù)據(jù)類型int是前者而integer是后者(也就是一個類),下面這篇文章主要給大家介紹了關(guān)于由Java中Integer引發(fā)的踩坑實戰(zhàn),需要的朋友可以參考下2022-11-11
防止SpringMVC攔截器攔截js等靜態(tài)資源文件的解決方法
本篇文章主要介紹了防止SpringMVC攔截器攔截js等靜態(tài)資源文件的解決方法,具有一定的參考價值,有興趣的同學可以了解一下2017-09-09

