mockito can ensure whether a mock method is being called with reequired arguments or not. it is done using the verify() method. take a look at the following code snippet.
//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);
example - verify() with same arguments
step 1 − create an interface called calculatorservice to provide mathematical functions
file: 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); }
step 2 − create a java class to represent mathapplication
file: 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); return 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); } }
step 3 − test the mathapplication class
let's test the mathapplication class, by injecting in it a mock of calculatorservice. mock will be created by mockito.
file: mathapplicationtester.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 initialize the test data @runwith(mockitojunitrunner.class) public class mathapplicationtester { //@injectmocks annotation is used to create and inject the mock object @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(calcservice.add(10.0, 20.0),30.0,0); //verify the behavior verify(calcservice).add(10.0, 20.0); } }
step 4 − execute test cases
create a java class file named testrunner in c:\> mockito_workspace to execute test case(s).
file: 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()); } }
step 5 − verify the result
compile the classes using javac compiler as follows −
c:\mockito_workspace>javac calculatorservice.java mathapplication. java mathapplicationtester.java testrunner.java
now run the test runner to see the result
c:\mockito_workspace>java testrunner
verify the output.
true
example - verify() with different arguments
step 1 − create an interface calculatorservice to provide mathematical functions
file: 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); }
step 2 − create a java class to represent mathapplication
file: 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); return 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); } }
step 3 − test the mathapplication class
let's test the mathapplication class, by injecting in it a mock of calculatorservice. mock will be created by mockito.
file: mathapplicationtester.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 initialize the test data @runwith(mockitojunitrunner.class) public class mathapplicationtester { //@injectmocks annotation is used to create and inject the mock object @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(calcservice.add(10.0, 20.0),30.0,0); //verify the behavior verify(calcservice).add(20.0, 30.0); } }
step 4 − execute test cases
create a java class file named testrunner in c:\> mockito_workspace to execute test case(s).
file: 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()); } }
step 5 − verify the result
compile the classes using javac compiler as follows −
c:\mockito_workspace>javac calculatorservice.java mathapplication. java mathapplicationtester.java testrunner.java
now run the test runner to see the result −
c:\mockito_workspace>java testrunner
verify the output.
testadd(mathapplicationtester): argument(s) are different! wanted: calcservice.add(20.0, 30.0); -> at mathapplicationtester.testadd(mathapplicationtester.java:32) actual invocation has different arguments: calcservice.add(10.0, 20.0); -> at mathapplication.add(mathapplication.java:10) false