SPRING BOOT 15.1 TEST
Posted jis117
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SPRING BOOT 15.1 TEST相关的知识,希望对你有一定的参考价值。
package example; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Matchers; import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; 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.web.context.WebApplicationContext; import com.google.gson.GsonBuilder; import html.example.SpringDemoApplication; import html.example.portal.data.MemberDto; import html.example.portal.data.MemberEntity; @WebAppConfiguration @RunWith(SpringRunner.class) @SpringBootTest(classes = { SpringDemoApplication.class, DataSourceAutoConfiguration.class, MybatisAutoConfiguration.class }) public class MemberControllerTest { @Autowired protected WebApplicationContext context; private MockMvc mockMvc; @Before public void before() { // mockMvc = MockMvcBuilders.standaloneSetup(memberService).build(); mockMvc = MockMvcBuilders.webAppContextSetup(context).build();// 建议使用这种 } @Test public void testAddUser() throws Exception { MemberDto dto = new MemberDto(); dto.setEmail("[email protected]"); dto.setStatus(0); String content = new GsonBuilder().create().toJson(dto); mockMvc.perform(MockMvcRequestBuilders.post("/member/add").contentType(MediaType.APPLICATION_JSON_UTF8) .content(content). // param("username", "123.123"). // param("password", "456.456"). // param("status", "0"). accept(MediaType.APPLICATION_JSON)).andExpect(MockMvcResultMatchers.status().isOk()) .andDo(MockMvcResultHandlers.print()) .andExpect(MockMvcResultMatchers.content().string(Matchers.contains("succ"))); } public void test() { MemberEntity member = new MemberEntity(); Assert.assertNotNull(member.getId()); } }
以上是关于SPRING BOOT 15.1 TEST的主要内容,如果未能解决你的问题,请参考以下文章
解决spring-boot启动中碰到的问题:Cannot determine embedded database driver class for database type NONE(转)(代码片段
一张图,理顺 Spring Boot应用在启动阶段执行代码的几种方式
一张图帮你记忆,Spring Boot 应用在启动阶段执行代码的几种方式