单元测试是指,对软件中的最小可测试单元在与程序其他部分相隔离的情况下进行检查和验证的工作,这里的最小可测试单元通常是指函数或者类。
从“基础元件”开测,单元测试对象是代码,以函数或类为单位,完成基础测试,在代码封装成“功能”后,更容易定位功能上出现的问题
通常来讲,单元测试的用例是一个“输入数据”和“预计输出”的集合。 你需要针对确定的输入,根据逻辑功能推算出预期正确的输出,并且以执行被测试代码的方式进行验证,用一句话概括就是“在明确了代码需要实现的逻辑功能的基础上,什么输入,应该产生什么输出”。
Unit是一个易学易用的Java单元测试框架,一般我们在写完一段代码或一个方发的时候,都要测试一下这段代码和这个方法的逻辑是不是正确,输入一定的数据,返回的数据是不是我们想要的结果,即我们在写单个业务代码针对结果进行测试。这时Junit就派上了大用场了。
也许有的初学者会说,项目完成之后测试不行吗?如果你要这么想的话,那就错了,因为随着你代码的增加,你牵扯到的模块和项目中的逻辑就会越来越多,这样测试起来就会非常的麻烦,而且容易出错。Junit看起来是增加了代码量,可是它可以大大的减少后期的测试时间,提升代码的质量,让代码更易于维护。
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.8version>
<scope>testscope>
dependency>
新建项目,在com.yzheng.junit.demo路径下新建ComputeJunit
package com.yzheng.junit.demo;
public class ComputeJunit {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public int division(int a, int b) {
return a / b;
}
}
方法很简单,就是一般的加减乘除,下面我们就可以进行测试了,怎么测试呢, 在我们的测试目录下新建测试类ComputeJunitTest,然后定义测试方法。代码如下:
package com.yzheng.junit.demo;
import static org.junit.Assert.*;
import org.junit.Test;
public class ComputeJunitTest {
@Test
public void testAdd(){
// 3 + 0
assertEquals(3, new ComputeJunit().add(3, 0));
}
@Test
public void testSubtract(){
// 6 - 3
assertEquals(3, new ComputeJunit().subtract(6, 3));
}
@Test
public void testMultiply(){
// 6 * 1
assertEquals(6, new ComputeJunit().multiply(6, 1));
}
@Test
public void testDivision(){
// 6 / 1
assertEquals(6, new ComputeJunit().division(6, 1));
}
}
关于测试方法:
- 测试方法必须有@test;
- 该测试方法必须由public void修饰,没有返回值;
- 该方法不带任何参数;
- 新建一个源代码测试文件单独存放测试代码;
- 测试类的包和被测试类的包保持一致;
- 测试方法间互相独立没有任何依赖;
下面来讲解一下assertEquals这个函数,它的第一个参数是你预期的一个结果,第二个参数使我们想测试的函数,这个函数我们要通过先new出函数所在的类,然后通过类来调用方法,方法里面的参数就是你在写该方法是传进来的参数。在这里最好在你需要测试的方法的前面加上test,这样的话就会比较规范一些
写完之后你可以点击测试类左侧的小三角,Run ComputeJunitTest 执行,就能在弹出的窗口中看到你的测试结果,它会提示你失败的个数和错误的个数。如果只想测试一个方法,在你创建的测试类的下面还有目录,列表里面的会列出你所有的测试方法,你就可以右击你想测试的方法,执行该方法即可,测试成功后就会看到一个绿色的对号,结果如图:
在这里如果我们每一个方法都要自己手动的敲出它的测试方法,在这里我们只是简单的测试了几个方法,在项目中如果我们有很多的方法需要测试,一个一个的敲的话会有些浪费时间了,这里给大家介绍一个快速生成测试的方法:在你所需要测试的类或者接口名称上按ctrl+shift+t ,然后选择create new test ,出现一个弹窗Create test如下图所示, 勾选需要测试的方法,点击OK,即可生成测试方法。
在前面的情况中我们都是测试的是成功的例子,但是Junit的作用只是测试的方法里的返回数据是不是正确的,但是在数据返回正确的情况下我们未必是正确的,就比如如果你要求的是长方形的面积,但是你用的是周长公式,当你在测试的时候他也会给你测试成功,得到预期的结果,也就是说我们的测试用例对于逻辑错误是无能为力的
当我们预期值和程序执行的结果不一样的时候就会测试失败,比如我们上面的测试加法函数的方法:
@Test
public void testAdd(){
assertEquals(4, new ComputeJunit().add(3, 0));
}
如果把预期结果3,改为4,就会测试失败(failure)。如果你仔细观察的话,下面还会有相关的提示,如图所示:
下面我们在来测试除法:
@Test
public void testDivision(){
assertEquals(6, new ComputeJunit().division(6, 0));
}
如果在这里把除数改为0,会出现什么情况呢,执行后提示有一个错误(error)。
于是我们得出下面的两种情况:
1、failure一般由单元测试使用的断言方法判断失败所引起的,这表示测试点发现了问题,也就是说程序输出的结果和预期的不一样
2、error是由代码异常引起的,他可能是由于测试代码本身的错误,也可能是被测试代码中隐藏的一个bug
首先我们先在test包下的com.yzheng.junit.demo;新建一个junit case,和并且直接命名JunitCaseTest。在创建的时候把setUpBeforeClass(),tearDownAfterClass(),setUp() ,tearDown() 选上就会得到下面的代码:
package com.yzheng.junit.demo;
import org.junit.After;
import org.junit.AfterClass;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class JunitCaseTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void test() {
fail("Not yet implemented");
}
}
下面我们在每个方法中输入一句简单的输出语句,看看他们的运行状态,如下:
package com.yzheng.junit.demo;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class JunitCaseTest {
/**
* BeforeClass修饰的方法会在所有方法被调用前执行,
* 而且该方法是静态的,所以当测试类被加载后接着就执行它,
* 在内存中它只会存在一份,适合加载配置文件。
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("this is beforeclass");
}
/**
* AfterClass修饰的方法用来对资源的清理,如关闭数据库的连接
*/
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("this is afterclass");
}
/**
* Before修饰的方法在每个test修饰的方法执行前会被执行一次,假如有两个
* 就执行两次
*/
@Before
public void setUp() throws Exception {
System.out.println("this is before");
}
/**
* After修饰的方法在每个test修饰的方法执行后会被执行一次,假如有两个
* 就执行两次
*/
@After
public void tearDown() throws Exception {
System.out.println("this is after");
}
@Test
public void test1() {
System.out.println("this is test1");
}
@Test
public void test2() {
System.out.println("this is test2");
}
}
如果运行上面的代码,就会得到下面的结果
this is beforeclass
this is before
this is test2
this is after
this is before
this is test1
this is after
this is afterclass
上面我已经讲解了@test、@BeforeClass、@AfterClass、@Before、@After;@test他除了将一个普通的方法修饰为测试方法外,还可以处理异常,设置超时。下面来对test的异常处理做讲解test有两个参数:expected和timeout,即异常处理和设置超时如果对我们上面的除数为0的那个方法进行异常处理,那么我们就可以看到代码能够正常,测试通过,代码如下:
@Test(expected=ArithmeticException.class)
public void testDivision(){
assertEquals(6, new ComputeJunit().division(6, 0));
}
在测试一些对性能有要求的方法中设置超时是很有必要的,它可以检测你的代码能否在这个时间段内运行出结果,设置方法如下:
/**
* timeout:单位是毫秒
*/
@Test(timeout=2000)
public void testWhile(){
while(true){
System.out.println("run forever");
}
}
@Ignore:在test方法上加上该修饰,测试的时候就会不执行该测试方法;
@RunWith:可以更改测试运行器;我们除了使用junit提供的测试运行器之外,还可以自定义我们的运行器,只要继承org.junit.runner.Runner
代码如下:
package com.yzheng.junit.demo;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({ComputeJunitTest.class,JunitCaseTest.class})
public class SuitTest {
public void test(){
/**
* 由于在开发的项目中,测试的类很多,一个一个运行很浪费时间,
* 于是可以写一个测试套件把所有需要测试的类组合在一起测试运行
* 1、写一个测试入口,这个类不含其它的方法;
* 2、更改测试运行器@RunWith(Suite.class)
* 3、将要测试的类作为数组放在@Suite.SuiteClasses({})中;
*/
}
}
在上面的测试中,我们对一个方法都是只测试了一组数据,可是在真正的项目中,一组数据往往是不够的,我们需要很多组数据,如果每一组数组写一个测试方法的话那可把我们的工作人员累死了!这时我们可以使用参数设置来解决这个问题。
代码如下:
package com.yzheng.junit.demo;
import static org.junit.Assert.*;
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 {
//声明变量存放预期值和测试数据;
int expected=0;
int input1=0;
int input2=0;
@Parameters
public static Collection<Object[]> test(){
return Arrays.asList(new Object[][]{
{3,1,2},
{4,2,2}
});
}
public ParameterTest(int expected,int input1,int input2){
this.expected=expected;
this.input1=input1;
this.input2=input2;
}
@Test
public void testAdd(){
assertEquals(expected, new ComputeJunit().add(input1, input2));
}
}
我们需要测试多组数据,那么我们就需要用到数组来存放多组数据,这里用Arrays.asList来接收。
代码如下:
package com.yzheng.junit.demo;
import java.util.ArrayList;
import org.junit.Test;
public class Exception1Test {
/**
* 如果测试该方法时产生一个ArithmeticException的异常,则表示测试通过
* 你可以改成int i = 1 / 1;运行时则会测试不通过-因为与你的期望的不符
*/
@Test(expected = ArithmeticException.class)
public void testDivisionWithException() {
int i = 1 / 0;
}
/**
* 运行时抛出一个IndexOutOfBoundsException异常才会测试通过
*/
@Test(expected = IndexOutOfBoundsException.class)
public void testEmptyList() {
new ArrayList<>().get(0);
}
}
代码如下:
package com.yzheng.junit.demo;
import java.util.ArrayList;
import org.junit.Test;
//注意:这是java中的静态引入
import static junit.framework.TestCase.fail;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class Exception2Test {
@Test
public void testDivisionWithException() {
try {
int i = 1 / 0;
fail();
} catch (ArithmeticException e) {
assertThat(e.getMessage(), is("/ by zero"));
}
}
@Test
public void testEmptyList() {
try {
new ArrayList<>().get(0);
fail();
} catch (IndexOutOfBoundsException e) {
assertThat(e.getMessage(), is("Index: 0, Size: 0"));
}
}
}
ExpectedException从4.7之后才有的,可以让你测试到异常类型和异常信息。可以认为和try-catch+fail(),但是更优雅些。
package com.yzheng.junit.demo;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.hasProperty;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class Exception3Test {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void testDivisionWithException() {
thrown.expect(ArithmeticException.class);
thrown.expectMessage(containsString("/ by zero"));
int i = 1 / 0;
}
@Test
public void testNameNotFoundException() throws NameNotFoundException {
//test type
thrown.expect(NameNotFoundException.class);
//test message
thrown.expectMessage(is("Name is empty!"));
//test detail
thrown.expect(hasProperty("errCode")); //make sure getters n setters are defined.
thrown.expect(hasProperty("errCode", is(666)));
CustomerService cust = new CustomerService();
cust.findByName("");
}
}
自定义的异常类NameNotFoundException,代码如下:
package com.yzheng.junit.demo.exception;
public class NameNotFoundException extends Exception{
private int errCode;
public NameNotFoundException(int errCode, String message) {
super(message);
this.errCode = errCode;
}
public int getErrCode() {
return errCode;
}
public void setErrCode(int errCode) {
this.errCode = errCode;
}
}
CustomerService,代码如下:
package com.yzheng.junit.demo.service;
public class CustomerService {
public Customer findByName(String name) throws NameNotFoundException {
if ("".equals(name)) {
// 模拟异常场景
throw new NameNotFoundException(666, "Name is empty!");
}
return new Customer(name);
}
static class Customer{
private String name;
public Customer(String name) {
super();
this.name = name;
}
}
}
一般使用idea新建一个SpringBoot 项目时,一般都会自动引入此依赖,如果没有,请手动引入
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
如下图所示,将@SpringBootTest注解的classes属性换成你项目的启动类, 然后@Autowired引入你想测试的类就好,测试方法上面要加@Test注解。
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.Test;
@RunWith(SpringJUnit4ClassRunner.class)
//classes = Application.class Application:你项目的启动类
@SpringBootTest(classes = Application.class )
public class TestXXXService {
@Autowired
private XXXService ...;
//@Test
public void testXXX(){...}
}
Java开发手册(嵩山版):下载链接
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。