本文仅仅是为了快速让初学者学会使用spring boot 单元测试,所以不会深入讲解。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
</dependency>
import com.yubangweb.WebApp;
import com.yubangweb.api.WelcomeApi;
import org.junit.Before;
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.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
/**
* Created by yubang on 2017/11/8.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = WebApp.class)
@WebAppConfiguration
public class TestDevApi {
private MockMvc mvc;
@Autowired
private WelcomeApi context;
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(context).build();
}
}
我们来看看代码的含义,WebApp.class这个是我们平时启动spring boot的那个有main函数的主类,我们在进行单元测试的时候会自动启动工程。WelcomeApi这个类是我们测试时候用到的路由类,就是有@RestController或者@Controller注解的类。@Before装修的函数就是用来初始化的,反正这样子写就好。
@Test(timeout = 1000)
public void testDataController() throws Exception {
RequestBuilder request = null;
request = org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get("/welcome/test").param("data", "123")..param("data2", "345");
mvc.perform(request).andExpect(status().isOk())
.andExpect(jsonPath("a", is("123")))
.andExpect(jsonPath("b", notNullValue()));
}
我们来分析一下代码,用于单元测试的方法用@Test注解,timeout = 1000是说超过1000毫秒没执行完成测试不通过,然后需要请求的路径"/welcome/test"改成自己的路径,param方法是用于填充get参数的。andExpect方法是用于判断结果是否正确,status().isOk()是判断返回码是否是200,其它返回码可以用status().XXX。jsonPath是校验返回数据(json格式的情况下)里面的值是否符合要求。
@Test
public void testDataController() throws Exception {
RequestBuilder request = null;
request = org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post("/welcome/test").param("data", "123")..param("data2", "345");
mvc.perform(request).andExpect(status().isOk())
.andExpect(jsonPath("a", is("123")))
.andExpect(jsonPath("b", notNullValue()));
}
其实就是get函数换成post函数,没有多大区别
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.fileUpload;
@Test
public void testUpload() throws Exception {
[]byte fileContent = 文件二进制数据; mvc.perform(fileUpload("/adminapi/v1/firmware/uploadFirmware").file("file", fileContent).param("data", "123456").andExpect(status().isOk()).andExpect(jsonPath("code", is(0)));
}
其实就是用了fileUpload这个方法,后面param就是填充post参数用的。
我只知道在pycharm里面,每个单元测试方法和类隔壁都有一个运行按钮,点一点即可
| 符号 | 说明 |
|---|---|
| $ | 根节点 |
| @ | 当前节点 |
| . 或者 [] | 子节点 |
| .. | 选择所有符合条件的节点 |
| * | 所有节点 |
| [] | 迭代器标示,如数组下标 |
| [,] | 支持做迭代器多选 |
| [start:end:step] | 数组切片运算符 |
| ?() | 支持过滤操作 |
| () | 支持表达式计算 |
| 函数 | 说明 |
|---|---|
| is(数据) | 判断是否一致 |
| notNullValue() | value不是空值 |
| nullValue() | value是空值 |
| equalTo(数据) | 判断字符串是否相等 |
文章首发于: https://blog.yubangweb.com/spring-boot-dan-yuan-ce-shi/ 转载请注明地址。