在本文中,我们将学习如何将 JUnit 和 Mockito 集成在一起。在这里,我们将创建一个数学应用程序,它使用 CalculatorService 执行基本的数学运算,例如加法、减法、乘法和除法。
我们将使用 Mockito 来模拟 CalculatorService 的虚拟实现。此外,我们还广泛使用注解来展示它们与 JUnit 和 Mockito 的兼容性。
下面将逐步讨论该过程。
第 1 步 - 创建一个名为 CalculatorService 的接口以提供数学函数
CalculatorService.java
public interface CalculatorService {
public double add(double input1, double input2);
public double subtract(double input1, double input2);
public double multiply(double input1, double input2);
public double divide(double input1, double input2);
}
第 2 步 - 创建一个 JAVA 类来表示 MathApplication
MathApplication.java
public class MathApplication {
private CalculatorService calcService;
public void setCalculatorService(CalculatorService calcService){
this.calcService = calcService;
}
public double add(double input1, double input2){
return calcService.add(input1, input2);
}
public double subtract(double input1, double input2){
return calcService.subtract(input1, input2);
}
public double multiply(double input1, double input2){
return calcService.multiply(input1, input2);
}
public double divide(double input1, double input2){
return calcService.divide(input1, input2);
}
}
第 3 步 - 测试 MathApplication 类
让我们通过在其中注入一个calculatorService 的模拟来测试MathApplication 类。 Mock 将由 Mockito 创建。
MathApplicationTester.java
import static org.mockito.Mockito.when;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
// @RunWith attaches a runner with the test class to initialize the test data
@RunWith(MockitoJUnitRunner.class)
public class MathApplicationTester {
//@InjectMocks annotation is used to create and inject the mock object
//这里这个对象是可以通过set注入来创建,它注入的实际上就是下面的@Mock注解的对象
@InjectMocks
MathApplication mathApplication = new MathApplication();
//@Mock annotation is used to create the mock object to be injected
@Mock
CalculatorService calcService;
@Test
public void testAdd(){
//add the behavior of calc service to add two numbers
when(calcService.add(10.0,20.0)).thenReturn(30.00);
//test the add functionality
Assert.assertEquals(mathApplication.add(10.0, 20.0),30.0,0);
}
}
第四步 - 创建一个类来执行测试用例
在 C> Mockito_WORKSPACE 中创建一个名为 TestRunner 的 java 类文件来执行测试用例。
TestRunner.java
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses (MathApplicationTester.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
第 5 步 - 验证结果
使用 javac 编译器编译类如下
C:\Mockito_WORKSPACE>javac CalculatorService.java MathApplication.
java MathApplicationTester.java TestRunner.java
现在运行 Test Runner 以查看结果
C:\Mockito_WORKSPACE>java TestRunner
验证输出
true
为mock添加行为
有了上面的例子,我们可以学习第一个基本的Mockito测试功能,那就是为被测试的接口添加行为,这里的具体代码就是:
//add the behavior of calc service to add two numbers
when(calcService.add(10.0,20.0)).thenReturn(30.00);
这里的意义就是为mock对象calcService的add方法添加了一个行为就是10.0+20.0返回30;在这里添加的是正确的行为?如果您感兴趣的话,可以试试这样:
//add the behavior of calc service to add two numbers
when(calcService.add(10.0,20.0)).thenReturn(50.00);
//很明显这是一个错误的行为
验证mock的行为
验证mock的行为如下:
//test the add functionality
Assert.assertEquals(calcService.add(10.0, 20.0),30.0,0);
//verify call to calcService is made or not with same arguments.
verify(calcService).add(10.0, 20.0);
//这里验证mock对象calcService的add方法是否被调用
同样的到来,您试试这样写会有什么效果?
//test the add functionality
Assert.assertEquals(calcService.add(10.0, 20.0),30.0,0);
//verify call to calcService is made or not with same arguments.
verify(calcService).add(10.0, 30.0);
//这里验证mock对象calcService的add方法是否被调用
验证mock的期望调用
期望调用的diamante如下:
when(calcService.add(10.0,20.0)).thenReturn(30.00);
//limit the method call to 1, no less and no more calls are allowed
verify(calcService, times(1)).add(10.0, 20.0);
下面是一个具体的期望调用的例子:
@Test
public void testAdd(){
//add the behavior of calc service to add two numbers
when(calcService.add(10.0,20.0)).thenReturn(30.00);
//add the behavior of calc service to subtract two numbers
when(calcService.subtract(20.0,10.0)).thenReturn(10.00);
//test the add functionality
Assert.assertEquals(mathApplication.add(10.0, 20.0),30.0,0);
Assert.assertEquals(mathApplication.add(10.0, 20.0),30.0,0);
Assert.assertEquals(mathApplication.add(10.0, 20.0),30.0,0);
//test the subtract functionality
Assert.assertEquals(mathApplication.subtract(20.0, 10.0),10.0,0.0);
//default call count is 1
verify(calcService).subtract(20.0, 10.0);
//check if add function is called three times
verify(calcService, times(3)).add(10.0, 20.0);
//verify that method was never called on a mock
verify(calcService, never()).multiply(10.0,20.0);
}
好了,今天我们简单理解一下以上的测试功能,后续的其他测试功能我们下一篇文章再继续。