Junit 单元测试学习
POM 依赖
在使用 IDEA 新建 SpringBoot 项目时一般会自动引入此依赖
1 2 3 4 5 <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-test</artifactId > <scope > test</scope > </dependency >
测试类基类
测试类文件夹和项目的 main 文件夹在同一层级
测试类存放的位置和项目的 Application 类在同一相对目录下
自动生成的测试类名称为 XXXApplicationTests
在这个类下补齐所需要的注解,之后的测试类直接继承该类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @RunWith(SpringRunner.class) @SpringBootTest(classes = xxxApplication.class) @WebAppConfiguration public class xxxApplicationTests { @Before public void init () { System.out.println("开始测试-----------------" ); } @After public void after () { System.out.println("测试结束-----------------" ); } }
测试类
测试类继承测试基类
通过 @Autowired 引入想要测试的类 DAO, Service ……
测试方法
在真正的测试方法前要加上 @Test 注解
加上 @Transactional 后方法会回滚,不会在数据库中产生脏数据
如果想要跳过其中一个测试方法,在方法前加上 @Ignore(message) 即可忽略该方法
Service 单元测试
1 2 3 4 5 Assert.assertThat([value], [matcher statement]);
Assert 断言
字符相关匹配符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 assertThat(testedValue, equalTo(expectedValue)); assertThat(testedString, equalToIgnoringCase(expectedString)); assertThat(testedString, equalToIgnoringWhiteSpace(expectedString); assertThat(testedString, containsString(subString) ); assertThat(testedString, endsWith(suffix)); assertThat(testedString, startsWith(prefix));
一般匹配符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 assertThat(object,nullValue()); assertThat(object,notNullValue()); assertThat(testedString, is(equalTo(expectedValue))); assertThat(testedValue, is(expectedValue)); assertThat(testedObject, is(Cheddar.class)); assertThat(testedString, not(expectedString)); assertThat(testedNumber, allOf( greaterThan(8 ), lessThan(16 ) ) ); assertThat(testedNumber, anyOf( greaterThan(16 ), lessThan(8 ) ) );
数值相关匹配符
1 2 3 4 5 6 7 8 9 10 assertThat(testedDouble, closeTo( 20.0 , 0.5 )); assertThat(testedNumber, greaterThan(16.0 )); assertThat(testedNumber, lessThan (16.0 )); assertThat(testedNumber, greaterThanOrEqualTo (16.0 )); assertThat(testedNumber, lessThanOrEqualTo (16.0 ));
集合相关匹配符
1 2 3 4 5 6 7 8 assertThat(mapObject, hasEntry("key" , "value" ) ); assertThat(iterableObject, hasItem (element)); assertThat(mapObject, hasKey ("key" )); assertThat(mapObject, hasValue(value));
Controller 单元测试
会用到 MockMvc,MockMvc 实现了对 HTTP 请求的模拟,可以不用启动工程来测试接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 @RunWith(SpringRunner.class) @SpringBootTest public class xxxControllerTest { @Autowired private WebApplicationContext wac; private MockMvc mvc; private MockHttpSession session; @Before public void setupMockMvc () { mvc = MockMvcBuilders.webAppContextSetup(wac).build(); session = new MockHttpSession(); User user =new User("root" ,"root" ); session.setAttribute("user" ,user); } @Test public void getTest () throws Exception { mvc.perform(MockMvcRequestBuilders.get("/path" ) .contentType(MediaType.APPLICATION_JSON_UTF8) .accept(MediaType.APPLICATION_JSON_UTF8) .session(session) ) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$.key1" ).value("value1" )) .andExpect(MockMvcResultMatchers.jsonPath("$.key2" ).value("value2" )) .andDo(MockMvcResultHandlers.print()); } }
打包测试
用一个 TestSuits 类整理所有测试类然后打包执行测试方法
1 2 3 4 5 6 7 8 9 10 11 12 @RunWith(Suite.class) @Suite .SuiteClasses({xxxTest.class,xxxTest2.class})public class TestSuits { }
参考
https://blog.csdn.net/weixin_39800144/article/details/79241620
http://tengj.top/2017/12/28/springboot12/#Service单元测试