场景:在项目开发中要测试springboot工程中一个几个dao和service的功能是否正常,初期是在web工程中进行要素的录入,工做量太大。使用该单元测试大大减少了工做强度。java
Junit这种老技术,如今又拿出来讲,不为别的,某种程度上来讲,更是为了要说明它在项目中的重要性。
凭本人的感受和经验来讲,在项目中彻底按标准都写Junit用例覆盖大部分业务代码的,应该不会超过一半。web
恰好前段时间写了一些关于SpringBoot的帖子,正好如今把Junit再拿出来从几个方面再说一下,也算是给一些新手参考了。spring
那么先简单说一下为何要写测试用例
1. 能够避免测试点的遗漏,为了更好的进行测试,能够提升测试效率
2. 能够自动测试,能够在项目打包前进行测试校验
3. 能够及时发现由于修改代码致使新的问题的出现,并及时解决数组
那么本文从如下几点来讲明怎么使用Junit,Junit4比3要方便不少,细节你们能够本身了解下,主要就是版本4中对方法命名格式再也不有要求,再也不须要继承TestCase,一切都基于注解实现。springboot
建立一个普通的Java类,在Junit4中再也不须要继承TestCase类了。
由于咱们是Web项目,因此在建立的Java类中添加注解:框架
@RunWith(SpringJUnit4ClassRunner.class) // SpringJUnit支持,由此引入Spring-Test框架支持! @SpringApplicationConfiguration(classes = SpringBootSampleApplication.class) // 指定咱们SpringBoot工程的Application启动类 @WebAppConfiguration // 因为是Web项目,Junit须要模拟ServletContext,所以咱们须要给咱们的测试类加上@WebAppConfiguration。
接下来就能够编写测试方法了,测试方法使用@Test注解标注便可。
在该类中咱们能够像日常开发同样,直接@Autowired来注入咱们要测试的类实例。
下面是完整代码:oop
package org.springboot.sample; import static org.junit.Assert.assertArrayEquals; import org.junit.Test; import org.junit.runner.RunWith; import org.springboot.sample.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; /** * * @author 单红宇(365384722) * @myblog http://blog.csdn.net/catoop/ * @create 2016年2月23日 */ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SpringBootSampleApplication.class) @WebAppConfiguration public class StudentTest { @Autowired private StudentService studentService; @Test public void likeName() { assertArrayEquals( new Object[]{ studentService.likeName("小明2").size() > 0, studentService.likeName("坏").size() > 0, studentService.likeName("莉莉").size() > 0 }, new Object[]{ true, false, true } ); // assertTrue(studentService.likeName("小明2").size() > 0); } }
接下来,你须要新增无数个测试类,编写无数个测试方法来保障咱们开发完的程序的有效性。post
//在全部测试方法前执行一次,通常在其中写上总体初始化的代码
@BeforeClass单元测试
//在全部测试方法后执行一次,通常在其中写上销毁和释放资源的代码
@AfterClass测试
//在每一个测试方法前执行,通常用来初始化方法(好比咱们在测试别的方法时,类中与其余测试方法共享的值已经被改变,为了保证测试结果的有效性,咱们会在@Before注解的方法中重置数据)
@Before
//在每一个测试方法后执行,在方法执行完成后要作的事情
@After
// 测试方法执行超过1000毫秒后算超时,测试将失败
@Test(timeout = 1000)
// 测试方法指望获得的异常类,若是方法执行没有抛出指定的异常,则测试失败
@Test(expected = Exception.class)
// 执行测试时将忽略掉此方法,若是用于修饰类,则忽略整个类
@Ignore("not ready yet")
@Test
@RunWith
在JUnit中有不少个Runner,他们负责调用你的测试代码,每个Runner都有各自的特殊功能,你要根据须要选择不一样的Runner来运行你的测试代码。
若是咱们只是简单的作普通Java测试,不涉及Spring Web项目,你能够省略@RunWith注解,这样系统会自动使用默认Runner来运行你的代码。
Junit为咱们提供的参数化测试须要使用 @RunWith(Parameterized.class)
然而由于Junit 使用@RunWith指定一个Runner,在咱们更多状况下须要使用@RunWith(SpringJUnit4ClassRunner.class)来测试咱们的Spring工程方法,因此咱们使用assertArrayEquals 来对方法进行多种可能性测试即可。
下面是关于参数化测试的一个简单例子:
package org.springboot.sample; import static org.junit.Assert.assertTrue; import java.util.Arrays; import java.util.Collection; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class ParameterTest { private String name; private boolean result; /** * 该构造方法的参数与下面@Parameters注解的方法中的Object数组中值的顺序对应 * @param name * @param result */ public ParameterTest(String name, boolean result) { super(); this.name = name; this.result = result; } @Test public void test() { assertTrue(name.contains("小") == result); } /** * 该方法返回Collection * * @return * @author SHANHY * @create 2016年2月26日 */ @Parameters public static Collection<?> data(){ // Object 数组中值的顺序注意要和上面的构造方法ParameterTest的参数对应 return Arrays.asList(new Object[][]{ {"小明2", true}, {"坏", false}, {"莉莉", false}, }); } }
正常状况下咱们写了5个测试类,咱们须要一个一个执行。
打包测试,就是新增一个类,而后将咱们写好的其余测试类配置在一块儿,而后直接运行这个类就达到同时运行其余几个测试的目的。
代码以下:
@RunWith(Suite.class) @SuiteClasses({ATest.class, BTest.class, CTest.class}) public class ABCSuite { // 类中不须要编写代码 }
咱们能够直接使用这个来测试咱们的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")); } }
使用 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,旨在让咱们测试一个方法的时候多传几种参数进行多种可能性测试。