4、打包测试
正常情况下我们写了5个测试类,我们需要一个一个执行。
打包测试,就是新增一个类,然后将我们写好的其他测试类配置在一起,然后直接运行这个类就达到同时运行其他几个测试的目的。
代码如下:
@RunWith(Suite.class) @SuiteClasses({ATest.class, BTest.class, CTest.class}) public class ABCSuite { // 类中不需要编写代码 } |
5、使用Junit测试HTTP的API接口
我们可以直接使用这个来测试我们的Rest API,如果内部单元测试要求不是很严格,我们保证对外的API进行完全测试即可,因为API会调用内部的很多方法,姑且把它当做是整合测试吧。
下面是一个简单的例子:
package org.springboot.sample; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.hamcrest.Matchers; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.TestRestTemplate; import org.springframework.boot.test.WebIntegrationTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; /** * * @author 单红宇(365384722) * @myblog http://blog.csdn.net/catoop/ * @create 2016年2月23日 */ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SpringBootSampleApplication.class) //@WebAppConfiguration // 使用@WebIntegrationTest注解需要将@WebAppConfiguration注释掉 @WebIntegrationTest("server.port:0")// 使用0表示端口号随机,也可以具体指定如8888这样的固定端口 public class HelloControllerTest { private String dateReg; private Pattern pattern; private RestTemplate template = new TestRestTemplate(); @Value("${local.server.port}")// 注入端口号 private int port; @Test public void test3(){ String url = "http://localhost:"+port+"/myspringboot/hello/info"; MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>(); map.add("name", "Tom"); map.add("name1", "Lily"); String result = template.postForObject(url, map, String.class); System.out.println(result); assertNotNull(result); assertThat(result, Matchers.containsString("Tom")); } } |
6、捕获输出
使用 OutputCapture 来捕获指定方法开始执行以后的所有输出,包括System.out输出和Log日志。
OutputCapture 需要使用@Rule注解,并且实例化的对象需要使用public修饰,如下代码:
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SpringBootSampleApplication.class) //@WebAppConfiguration // 使用@WebIntegrationTest注解需要将@WebAppConfiguration注释掉 @WebIntegrationTest("server.port:0")// 使用0表示端口号随机,也可以具体指定如8888这样的固定端口 public class HelloControllerTest { @Value("${local.server.port}")// 注入端口号 private int port; private static final Logger logger = LoggerFactory.getLogger(StudentController.class); @Rule // 这里注意,使用@Rule注解必须要用public public OutputCapture capture = new OutputCapture(); @Test public void test4(){ System.out.println("HelloWorld"); logger.info("logo日志也会被capture捕获测试输出"); assertThat(capture.toString(), Matchers.containsString("World")); } } |
关于Assert类中的一些断言方法,都很简单,本文不再赘述。
但是在新版的Junit中,assertEquals 方法已经被废弃,它建议我们使用assertArrayEquals,旨在让我们测试一个方法的时候多传几种参数进行多种可能性测试。
上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。