最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用MockMvc進(jìn)行controller層單元測試 事務(wù)自動回滾的完整案例

 更新時間:2021年06月10日 08:48:03   作者:低調(diào)的小白  
這篇文章主要介紹了使用MockMvc進(jìn)行controller層單元測試 事務(wù)自動回滾的完整案例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

看代碼吧~

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() // 事務(wù)自動回滾,默認(rèn)是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進(jìn)行單元測試,但在進(jìn)行dao層的測試時,發(fā)現(xiàn)save方法無法進(jìn)行數(shù)據(jù)的自動回滾。

逐一進(jìn)行檢查分析,事務(wù)也開啟了,并使用注解方式標(biāo)記@RollBack(true),在控制臺中也打出了事務(wù)回滾的信息,但仍然不能自動回滾。

后來感覺是數(shù)據(jù)庫的原因,我的數(shù)據(jù)庫使用的是MySql,這就存在數(shù)據(jù)表的類型是否支持事務(wù)情況。

逐查閱MySql相關(guān)文檔,發(fā)現(xiàn),InnoDB類型的表是支持事務(wù)的,而MyISAM是不支持事務(wù)的,立刻查看數(shù)據(jù)表類型,果然為MyISAM,改為InnoDB后,重新進(jìn)行測試,問題得到解決。

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • JAVA控制流程break?continue的示例代碼

    JAVA控制流程break?continue的示例代碼

    JAVA流程控制中有相關(guān)代碼可以終止整個流程的進(jìn)程,他們就是(break和continue),本文通過實(shí)例代碼介紹下JAVA控制流程break?continue的相關(guān)知識,感興趣的朋友一起看看吧
    2022-03-03
  • Spring Security源碼解析之權(quán)限訪問控制是如何做到的

    Spring Security源碼解析之權(quán)限訪問控制是如何做到的

    Spring Security 中對于權(quán)限控制默認(rèn)已經(jīng)提供了很多了,但是,一個優(yōu)秀的框架必須具備良好的擴(kuò)展性,下面小編給大家介紹Spring Security源碼解析之權(quán)限訪問控制是如何做到的,感興趣的朋友跟隨小編一起看看吧
    2021-05-05
  • springcloud配置ssh的問題及解決方法

    springcloud配置ssh的問題及解決方法

    這篇文章主要介紹了springcloud配置ssh,本文給大家介紹在配置過程中遇到的問題及解決方法,通過結(jié)合實(shí)例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • SpringBoot中的@CrossOrigin注解詳解

    SpringBoot中的@CrossOrigin注解詳解

    這篇文章主要介紹了SpringBoot中的@CrossOrigin注解詳解,跨源資源共享(CORS)是由大多數(shù)瀏覽器實(shí)現(xiàn)的W3C規(guī)范,允許您靈活地指定什么樣的跨域請求被授權(quán),而不是使用一些不太安全和不太強(qiáng)大的策略,需要的朋友可以參考下
    2023-11-11
  • 8種常見的Java不規(guī)范代碼

    8種常見的Java不規(guī)范代碼

    本文主要介紹了8種常見的Java不規(guī)范代碼。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • SpringBoot接收前端參數(shù)的幾種方式分享

    SpringBoot接收前端參數(shù)的幾種方式分享

    這篇文章給大家分享幾種SpringBoot接收前端參數(shù)的方式,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-09-09
  • springboot如何使用logback-spring配置日志格式,并分環(huán)境配置

    springboot如何使用logback-spring配置日志格式,并分環(huán)境配置

    這篇文章主要介紹了springboot如何使用logback-spring配置日志格式,并分環(huán)境配置的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java @RequestMapping注解功能使用詳解

    Java @RequestMapping注解功能使用詳解

    通過@RequestMapping注解可以定義不同的處理器映射規(guī)則,下面這篇文章主要給大家介紹了關(guān)于SpringMVC中@RequestMapping注解用法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • SpringBoot項(xiàng)目啟動錯誤:找不到或無法加載主類的三種解決方法

    SpringBoot項(xiàng)目啟動錯誤:找不到或無法加載主類的三種解決方法

    在開發(fā)SpringBoot應(yīng)用時,經(jīng)??赡軙龅揭粋€啟動錯誤:“錯誤:找不到或無法加載主類 com.example.controller.demo.DemoApplication”,本文將介紹三種解決這一問題的方法,需要的朋友可以參考下
    2024-10-10
  • spring boot 如何優(yōu)雅關(guān)閉服務(wù)

    spring boot 如何優(yōu)雅關(guān)閉服務(wù)

    這篇文章主要介紹了spring boot 如何優(yōu)雅關(guān)閉服務(wù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11

最新評論

凌海市| 景宁| 河源市| 长子县| 贵南县| 宁阳县| 盱眙县| 漾濞| 穆棱市| 高陵县| 博兴县| 乌审旗| 成武县| 凭祥市| 道孚县| 苍梧县| 灵武市| 台中市| 贵德县| 冀州市| 海伦市| 七台河市| 黄平县| 隆安县| 宁南县| 郯城县| 潮安县| 郑州市| 淮安市| 达拉特旗| 新化县| 桑植县| 同江市| 游戏| 夹江县| 绩溪县| 金平| 汕头市| 深泽县| 政和县| 博乐市|