本次主要记录如何使用Junit和Mockito对Springboot进行单元测试,主要包含如下内容:
- controller测试
- service测试
- 打包测试
- 忽略注解@Ignore
- assertThat的常用方法
- 在pom文件中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
- 目录结构,在src/test/java目录下建测试文件
- controller层测试(其他层代码与平时写的是一样的,此处省略)
import org.junit.Before;
import org.junit.Ignore;
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.mock.web.MockHttpSession;
import org.springframework.test.context.junit4.SpringRunner;
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.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
@RunWith(SpringRunner.class)
@SpringBootTest
public class StuInfoTestController {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
private MockHttpSession session = new MockHttpSession();
Map<Object, Object> map ;
ObjectMapper mapper=new ObjectMapper();
@Before
public void befer() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();//这个很重要,初始化MockMvc对象
//登陆写在这里,并注入一个session,用于通过拦截器
}
@Ignore("not ready yet") //写在方法上,可以忽略这个测试方法,写在类上,可以忽略这个测试类。
@Test
@Transactional //这样测试完数据就会回滚了,不会造成垃圾数据。如果你想关闭回滚,只要加上@Rollback(false)注解即可。@Rollback表示事务执行完回滚,支持传入一个参数value,默认true即回滚,false不回滚。
public void insertStuInfo() throws Exception {
map = new HashMap<Object, Object>();
map.put("stuName", "测试啊00");
map.put("stuNo", "12121");
String jsonString = mapper.writeValueAsString(map);
mockMvc.perform( //执行一个请求
MockMvcRequestBuilders.post("/test/stuInfo/insertStuInfo") //构造一个请求,Post请求就用.post方法
.contentType(MediaType.APPLICATION_JSON_UTF8) //代表发送端发送的数据格式是application/json;charset=UTF-8,不加会报415。原因:应该是HttpRequester在发送请求的时候帮我们自己去做了一些处理,如果发送的是json数据自动帮我们加上了Content-Type字段的声明,从而能够正常返回结果。而在Java代码发送的时候,通过抓包我们发现是没有这个头字段的(当然没有,因为我们压根没有给他设置这个头字段)。所以我们要告诉接收方发送什么样的数据格式,如果没有告诉的话接收方可能就直接拒识了。
.accept(MediaType.APPLICATION_JSON_UTF8) //代表客户端希望接受的数据类型为application/json;charset=UTF-8
.content(jsonString) //请求参数(我这里是json格式的)
.session(session)) //注入一个session,这样拦截器才可以通过
.andExpect( //添加执行完成后的断言
MockMvcResultMatchers.status().isOk() //方法看请求的状态响应码是否为200如果不是则抛异常,测试不通过
)
//.andExpect(MockMvcResultMatchers.jsonPath("$.id").value("13")) //这里jsonPath用来获取id字段比对是否为2,不是就测试不通过
.andDo(MockMvcResultHandlers.print()); //添加一个结果处理器,表示要对结果做点什么事情,比如此处使用MockMvcResultHandlers.print()输出整个响应结果信息
}
@Test
public void selectStuInfo() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post("/stuInfo/getAllStuInfo01")
.accept(MediaType.APPLICATION_JSON_UTF8)
//.content(jsonString)
.session(session)
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print());
}
}
- service层测试(其他层代码与平时写的是一样的,此处省略)
import static org.hamcrest.CoreMatchers.is;
import org.junit.Assert;
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.test.context.junit4.SpringRunner;
import wmq.fly.mybatis.entity.StuInfo;
import wmq.fly.mybatis.service.StuInfoService;
@RunWith(SpringRunner.class)
@SpringBootTest
public class StuInfoTestService {
@Autowired
private StuInfoService stuInfoService;
@Test
public void getStuInfoById() {
StuInfo stuInfo = new StuInfo();
stuInfo.setId(8);
StuInfo reStuInfo = (StuInfo)stuInfoService.getStuInfoById(stuInfo);
Assert.assertThat(reStuInfo.getStuName(),is("快乐永存"));
}
}
- 打包测试,如果项目中写了很多测试方法,如果每个测试方法都需要执行,那么每个方法或每个类都去执行一遍,是不是有些抓狂?Springboot提供了打包的测试方法,即将所用的测试类都整理到一个类中,然后运行这个类,就可以将所用的测试方法运行。这里将StuInfoTestController和StuInfoTestService整理到StuInfoSuits 方法中,用JUnit Test方式运行StuInfoSuits 类即可。
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({StuInfoTestController.class,StuInfoTestService.class})
public class StuInfoSuits {
//不用写代码,只需要注解即可
}
- 使用打包方式的同时又不想运行某些测试方法,怎么办呢?我们可以使用忽略注解@Ignore,写在方法上,可以忽略这个测试方法;写在类上,可以忽略这个测试类。
@Ignore("not ready yet")//写在方法上,可以忽略这个测试方法,写在类上,可以忽略这个类。
@Test
@Transactional
public void insertStuInfo() throws Exception {
map = new HashMap<Object, Object>();
map.put("stuName", "测试啊00");
map.put("stuNo", "12121");
String jsonString = mapper.writeValueAsString(map);
mockMvc.perform(
MockMvcRequestBuilders.post("/test/stuInfo/insertStuInfo")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.content(jsonString)
.session(session))
.andExpect(
MockMvcResultMatchers.status().isOk()
)
//.andExpect(MockMvcResultMatchers.jsonPath("$.id").value("13"))
.andDo(MockMvcResultHandlers.print());
MockMvcResultHandlers.print();
}
运行后的效果:
7. assertThat的常用方法
字符相关匹配符
/**equalTo匹配符断言被测的testedValue等于expectedValue,
* equalTo可以断言数值之间,字符串之间和对象之间是否相等,相当于Object的equals方法
*/
assertThat(testedValue, equalTo(expectedValue));
/**equalToIgnoringCase匹配符断言被测的字符串testedString
*在忽略大小写的情况下等于expectedString
*/
assertThat(testedString, equalToIgnoringCase(expectedString));
/**equalToIgnoringWhiteSpace匹配符断言被测的字符串testedString
*在忽略头尾的任意个空格的情况下等于expectedString,
*注意:字符串中的空格不能被忽略
*/
assertThat(testedString, equalToIgnoringWhiteSpace(expectedString);
/**containsString匹配符断言被测的字符串testedString包含子字符串subString**/
assertThat(testedString, containsString(subString) );
/**endsWith匹配符断言被测的字符串testedString以子字符串suffix结尾*/
assertThat(testedString, endsWith(suffix));
/**startsWith匹配符断言被测的字符串testedString以子字符串prefix开始*/
assertThat(testedString, startsWith(prefix));
//一般匹配符
/**nullValue()匹配符断言被测object的值为null*/
assertThat(object,nullValue());
/**notNullValue()匹配符断言被测object的值不为null*/
assertThat(object,notNullValue());
/**is匹配符断言被测的object等于后面给出匹配表达式*/
assertThat(testedString, is(equalTo(expectedValue)));
/**is匹配符简写应用之一,is(equalTo(x))的简写,断言testedValue等于expectedValue*/
assertThat(testedValue, is(expectedValue));
/**is匹配符简写应用之二,is(instanceOf(SomeClass.class))的简写,
*断言testedObject为Cheddar的实例
*/
assertThat(testedObject, is(Cheddar.class));
/**not匹配符和is匹配符正好相反,断言被测的object不等于后面给出的object*/
assertThat(testedString, not(expectedString));
/**allOf匹配符断言符合所有条件,相当于“与”(&&)*/
assertThat(testedNumber, allOf( greaterThan(8), lessThan(16) ) );
/**anyOf匹配符断言符合条件之一,相当于“或”(||)*/
assertThat(testedNumber, anyOf( greaterThan(16), lessThan(8) ) );
//数值相关匹配符
/**closeTo匹配符断言被测的浮点型数testedDouble在20.0¡À0.5范围之内*/
assertThat(testedDouble, closeTo( 20.0, 0.5 ));
/**greaterThan匹配符断言被测的数值testedNumber大于16.0*/
assertThat(testedNumber, greaterThan(16.0));
/** lessThan匹配符断言被测的数值testedNumber小于16.0*/
assertThat(testedNumber, lessThan (16.0));
/** greaterThanOrEqualTo匹配符断言被测的数值testedNumber大于等于16.0*/
assertThat(testedNumber, greaterThanOrEqualTo (16.0));
/** lessThanOrEqualTo匹配符断言被测的testedNumber小于等于16.0*/
assertThat(testedNumber, lessThanOrEqualTo (16.0));
//集合相关匹配符
/**hasEntry匹配符断言被测的Map对象mapObject含有一个键值为"key"对应元素值为"value"的Entry项*/
assertThat(mapObject, hasEntry("key", "value" ) );
/**hasItem匹配符表明被测的迭代对象iterableObject含有元素element项则测试通过*/
assertThat(iterableObject, hasItem (element));
/** hasKey匹配符断言被测的Map对象mapObject含有键值“key”*/
assertThat(mapObject, hasKey ("key"));
/** hasValue匹配符断言被测的Map对象mapObject含有元素值value*/
assertThat(mapObject, hasValue(value));