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 - Ordered Verification
Mockito - Ordered Verification
Mockito女士 采用方法顺序的班次要求 mo在适当的行动过程中进行。
Syntax
//create an inOrder verifier for a single mock InOrder inOrder = inOrder(calcService); //following will make sure that add is first called then subtract is called. inOrder.verify(calcService).add(20.0,10.0); inOrder.verify(calcService).subtract(20.0,10.0);
Example
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); } 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将创建。
在此,我们在(......)时添加了两条 mo法,加上(......)和(d)项。 然而,在测试期间,我们先叫“分站”,然后打电话。 当我们使用Mockito制造一个 mo物体时,该方法的执行次序并不重要。 采用InOrder语,我们可以确保电话秩序。
<File: MathApppcation Tester.java
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.mockito.Mockito.inOrder; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InOrder; import org.mockito.runners.MockitoJUnitRunner; // @RunWith attaches a runner with the test class to initiapze the test data @RunWith(MockitoJUnitRunner.class) pubpc class MathApppcationTester { private MathApppcation mathApppcation; private CalculatorService calcService; @Before pubpc void setUp(){ mathApppcation = new MathApppcation(); calcService = mock(CalculatorService.class); mathApppcation.setCalculatorService(calcService); } @Test pubpc void testAddAndSubtract(){ //add the behavior to add numbers when(calcService.add(20.0,10.0)).thenReturn(30.0); //subtract the behavior to subtract numbers when(calcService.subtract(20.0,10.0)).thenReturn(10.0); //test the add functionapty Assert.assertEquals(mathApppcation.add(20.0, 10.0),30.0,0); //test the subtract functionapty Assert.assertEquals(mathApppcation.subtract(20.0, 10.0),10.0,0); //create an inOrder verifier for a single mock InOrder inOrder = inOrder(calcService); //following will make sure that add is first called then subtract is called. inOrder.verify(calcService).subtract(20.0,10.0); inOrder.verify(calcService).add(20.0,10.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
核实产出。
testAddAndSubtract(MathApppcationTester): Verification in order failure Wanted but not invoked: calculatorService.add(20.0, 10.0); -> at MathApppcationTester.testAddAndSubtract(MathApppcationTester.java:48) Wanted anywhere AFTER following interaction: calculatorService.subtract(20.0, 10.0); -> at MathApppcation.subtract(MathApppcation.java:13) falseAdvertisements