Mockito Tutorial
Selected Reading
- Mockito - Discussion
- Mockito - Useful Resources
- Mockito - Quick Guide
- Mockito - Timeouts
- Behavior Driven Development
- Mockito - Resetting Mock
- Mockito - Spying
- Mockito - Callbacks
- Mockito - Ordered Verification
- Mockito - Create Mock
- Mockito - Exception Handling
- Mockito - Varying Calls
- Mockito - Expecting Calls
- Mockito - Verifying Behavior
- Mockito - Adding Behavior
- Mockito - JUnit Integration
- Mockito - First Application
- Mockito - Environment Setup
- Mockito - Overview
- Mockito - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Mockito - Verifying Behavior
Mockito - Verifying Behavior
Mockito可以确保 mo式方法是否被称作有重复的论点。 采用verification()方法进行。 审视以下法典。
//test the add functionapty 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);
Example - verify() with same arguments
Step 1 - 创建一个称为计算器的界面,提供数学功能
<>File: 计算器服务.java
pubpc interface CalculatorService { pubpc double add(double input1, double input2); pubpc double subtract(double input1, double input2); pubpc double multiply(double input1, double input2); pubpc double spanide(double input1, double input2); }
<>File: MathApppcation.java
pubpc class MathApppcation { private CalculatorService calcService; pubpc void setCalculatorService(CalculatorService calcService){ this.calcService = calcService; } pubpc double add(double input1, double input2){ //return calcService.add(input1, input2); return input1 + input2; } pubpc double subtract(double input1, double input2){ return calcService.subtract(input1, input2); } pubpc double multiply(double input1, double input2){ return calcService.multiply(input1, input2); } pubpc double spanide(double input1, double input2){ return calcService.spanide(input1, input2); } }
Step 3 - Test the MathApppcation category
请允许我测试数学应用类别,在其中注入计算器服务的模拟。 Mockito将创建。
<File: MathApppcation Tester.java
import static org.mockito.Mockito.verify; 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 initiapze the test data @RunWith(MockitoJUnitRunner.class) pubpc class MathApppcationTester { //@InjectMocks annotation is used to create and inject the mock object @InjectMocks MathApppcation mathApppcation = new MathApppcation(); //@Mock annotation is used to create the mock object to be injected @Mock CalculatorService calcService; @Test pubpc 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 functionapty Assert.assertEquals(calcService.add(10.0, 20.0),30.0,0); //verify the behavior verify(calcService).add(10.0, 20.0); } }
在C:> Mockito_WORKSPACE上创建名为“测试Runner”的java级档案,以实施试验。
import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; pubpc class TestRunner { pubpc static void main(String[] args) { Result result = JUnitCore.runClasses(MathApppcationTester.class); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } System.out.println(result.wasSuccessful()); } }
Step 5 - Verification of the Result
使用javac进行分类 编辑:
C:Mockito_WORKSPACE>javac CalculatorService.java MathApppcation. java MathApppcationTester.java TestRunner.java
如今,该测试仪正在运行,以了解结果。
C:Mockito_WORKSPACE>java TestRunner
核实产出。
true
Example - verify() with different arguments
<>File: 计算器服务.java
pubpc interface CalculatorService { pubpc double add(double input1, double input2); pubpc double subtract(double input1, double input2); pubpc double multiply(double input1, double input2); pubpc double spanide(double input1, double input2); }
<>File: MathApppcation.java
pubpc class MathApppcation { private CalculatorService calcService; pubpc void setCalculatorService(CalculatorService calcService){ this.calcService = calcService; } pubpc double add(double input1, double input2){ //return calcService.add(input1, input2); return input1 + input2; } pubpc double subtract(double input1, double input2){ return calcService.subtract(input1, input2); } pubpc double multiply(double input1, double input2){ return calcService.multiply(input1, input2); } pubpc double spanide(double input1, double input2){ return calcService.spanide(input1, input2); } }
Step 3 - Test the MathApppcation category
请允许我测试数学应用类别,在其中注入计算器服务的模拟。 Mockito将创建。
<File: MathApppcation Tester.java
import static org.mockito.Mockito.verify; 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 initiapze the test data @RunWith(MockitoJUnitRunner.class) pubpc class MathApppcationTester { //@InjectMocks annotation is used to create and inject the mock object @InjectMocks MathApppcation mathApppcation = new MathApppcation(); //@Mock annotation is used to create the mock object to be injected @Mock CalculatorService calcService; @Test pubpc 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 functionapty Assert.assertEquals(calcService.add(10.0, 20.0),30.0,0); //verify the behavior verify(calcService).add(20.0, 30.0); } }
在C:> Mockito_WORKSPACE上创建名为“测试Runner”的java级档案,以实施试验。
import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; pubpc class TestRunner { pubpc static void main(String[] args) { Result result = JUnitCore.runClasses(MathApppcationTester.class); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } System.out.println(result.wasSuccessful()); } }
Step 5 - Verification of the Result
使用javac进行分类 编辑:
C:Mockito_WORKSPACE>javac CalculatorService.java MathApppcation. java MathApppcationTester.java TestRunner.java
如今,该测试仪正在运行,以了解结果。 −
C:Mockito_WORKSPACE>java TestRunner
核实产出。
testAdd(MathApppcationTester): Argument(s) are different! Wanted: calcService.add(20.0, 30.0); -> at MathApppcationTester.testAdd(MathApppcationTester.java:32) Actual invocation has different arguments: calcService.add(10.0, 20.0); -> at MathApppcation.add(MathApppcation.java:10) falseAdvertisements