Javaspring+mybit+maven中实现Junit测试类
作者:互联网
在一个Javaspring+mybit+maven框架中,增加Junit测试类。
系统:window7
1.首先在配置文件pom.xml文件中添加测试类的包文件,之后执行maven取得相对应的包文件到本地
<dependency><!-- JUnit单元测试框架 --> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency><!-- spring对测试框架的简单封装功能 --> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <!--- mock追加-> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>1.9.5</version> <scope>test</scope> </dependency>
测试类
package com.cnc.mht; import static org.mockito.Mockito.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import org.junit.Assert; 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.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import java.util.List; import java.util.Map; import com.cnc.mht.web.http.RequestUtil; import com.cnc.mht.web.weixin.dto.ImsWcyMakeAnAppointment; import net.sf.json.JSONObject; @SpringBootTest @RunWith(SpringJUnit4ClassRunner.class) public class SpringJunitTest { @Autowired private WebApplicationContext webApplicationContext; private MockMvc mockMvc; @Autowired JdbcTemplate jdbctemplate = null; // @Inject @MockBean // @Autowired RequestUtil requestUtil; @Before public void setUp() throws Exception { mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } @Test public void testComment() throws Exception { // parmがない String responseString = mockMvc .perform(MockMvcRequestBuilders.post("/openSesame").contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); System.out.println("哈哈哈" + responseString); } @Test public void testComment1() throws Exception { // parmがある String responseString = mockMvc .perform(MockMvcRequestBuilders.post("/openSesame/getTableInfos") .contentType(MediaType.APPLICATION_JSON_VALUE).param("weid", "11").param("bussinessid", "1") .param("storeid", "53").param("date", "20190901")) .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); System.out.println("哈哈哈" + responseString); } @Test public void testComment2() throws Exception { // DB検索 List<Map<String, Object>> aa = jdbctemplate.queryForList("select * from ims_wcy_mst_code"); ImsWcyMakeAnAppointment imsWcyMakeAnAppointment = new ImsWcyMakeAnAppointment(); imsWcyMakeAnAppointment.setWeid(11); imsWcyMakeAnAppointment.setBussinessid(1); imsWcyMakeAnAppointment.setStoreid(53); when(requestUtil.doPost(11, 1, 53, "/reserveCheck", null)).thenReturn("2"); String jsonObject = JSONObject.fromObject(imsWcyMakeAnAppointment).toString(); // parmがJSON String responseString = mockMvc.perform(MockMvcRequestBuilders.post("/openSesame/submit") .contentType(MediaType.APPLICATION_JSON_VALUE).content(jsonObject)).andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); // result検証 Assert.assertEquals(1, 1); System.out.println("哈哈哈" + responseString); } }
运行即可
注※1 mock不好用的场合要确认被测试的类是否是@Service
标签:web,Javaspring,springframework,maven,mybit,org,test,import,junit 来源: https://www.cnblogs.com/killclock048/p/11630171.html