Python 官方文档:入门教程 => 点击学习
看代码吧~ package com.ieou.ms_backend.controller; import com.Google.gson.Gson; import com.ieo
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.*;
@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());
}
}
在使用JUnit做单元测试时,为了使测试数据不对数据库造成污染,故选取了spring-test进行单元测试,但在进行dao层的测试时,发现save方法无法进行数据的自动回滚。
逐一进行检查分析,事务也开启了,并使用注解方式标记@RollBack(true),在控制台中也打出了事务回滚的信息,但仍然不能自动回滚。
后来感觉是数据库的原因,我的数据库使用的是Mysql,这就存在数据表的类型是否支持事务情况。
逐查阅mysql相关文档,发现,InnoDB类型的表是支持事务的,而MyISAM是不支持事务的,立刻查看数据表类型,果然为MyISAM,改为InnoDB后,重新进行测试,问题得到解决。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: 使用MockMvc进行controller层单元测试 事务自动回滚的完整案例
本文链接: https://lsjlt.com/news/127955.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0