前言:本文详细的记录了SpringBoot如何结合Junit写测试用例,如何执行,打包执行,忽略执行等操作,SpringBoot内置了Junit测试组件,使用很方便,不用再单独引入其他测试组件。
1. 依赖引用
在pom文件中引入以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2. 测试类基类
项目中创建test包和test类,结构如下:
由于一个项目中我们会写很多很多测试类,而测试类上面是需要以下几个注解的,每建一个类都去补注解,太麻烦,我们就在这个类中加上注解,其他测试类直接继承这个类就好了:
package cn.com.oceansoft.osc.ms;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringRunner.class)
@SpringBootTest
//由于是Web项目,Junit需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration。
@WebAppConfiguration
public class TestNGSpringTests {
@Before
public void init() {
System.out.println("开始测试-----------------");
}
@After
public void after() {
System.out.println("测试结束-----------------");
}
}
3. 测试类
建一个测试类,继承基类,然后测试我service中的方法
package cn.com.oceansoft.osc.ms.service.impl;
import cn.com.oceansoft.osc.ms.TestNGSpringTests;
import cn.com.oceansoft.osc.ms.service.ITaskBaseService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
public class TaskBaseServiceImplTest extends TestNGSpringTests {
@Autowired
ITaskBaseService taskBaseService;
@Test
public void testGetByNum() throws Exception {
taskBaseService.getByNum("1902134303000101010001");
}
}
4. 运行测试用例
运行有两种方法:
1.选中方法,右键,然后run
2.点击方法前的小标;
具体操作如下截图:
5. 打包测试
项目开发完后,我们写了100个测试用例类,我不能每个类都点击进去,然后慢慢执行,SpringBoot提供了打包测试的方式:我们用一个类,把所有的测试类整理进去,然后直接运行这个类,所有的测试类都会执行。
我这里建了两个测试类,分别是TaskBaseServiceImplTest,MatterServiceImplTest,现在我打包进TestSuits,让他们一次运行
@Suite.SuiteClasses({TaskBaseServiceImplTest.class,MatterServiceImplTest.class})
打包完整代码:
package cn.com.oceansoft.osc.ms;
import cn.com.oceansoft.osc.ms.service.impl.MatterServiceImplTest;
import cn.com.oceansoft.osc.ms.service.impl.TaskBaseServiceImplTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
* Created by ben
* Date 2019/03/12
* Description:打包测试
*/
//@Ignore("not ready yet")
@RunWith(Suite.class)
@Suite.SuiteClasses({TaskBaseServiceImplTest.class,MatterServiceImplTest.class})
public class TestSuits {
//不用写代码,只需要注解即可
}
6. 忽略方法
当我一个测试类写了10个测试方法时,其中有1个我暂时不想测,想跳过,但是其他9个我想一次运行,怎么办?这里有一个忽略注解”@Ignore(“not ready yet”)“,写在方法上,可以忽略这个测试方法,写在类上,可以忽略这个类。
运行这个测试类testGetByNum方法就会忽略执行。
package cn.com.oceansoft.osc.ms.service.impl;
import cn.com.oceansoft.osc.ms.TestNGSpringTests;
import cn.com.oceansoft.osc.ms.service.ITaskBaseService;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
public class TaskBaseServiceImplTest extends TestNGSpringTests {
@Autowired
ITaskBaseService taskBaseService;
@Ignore("not ready yet")
@Test
public void testGetByNum() throws Exception {
taskBaseService.getByNum("1902134303000101010001");
}
@Test
public void testGet() throws Exception {
taskBaseService.get("869fbecf2acb4266b9096e0dffaf0c12");
}
}