Just like stubbing, Argument matchers can be used for verification also.
Example
Example Application
package com.logicbig.example;
public interface MyService {
public int doSomething(String processName);
}
package com.logicbig.example;
public class MyProcessor {
private String processName;
private MyService myService;
public MyProcessor(String processName, MyService myService) {
this.processName = processName;
this.myService = myService;
}
public void process() {
int returnInteger = processName != null ? myService.doSomething(processName) : -1;
System.out.println("My Integer is: " + returnInteger);
}
}
Using argument matcher with verify()
Following example uses Mockito.anyString() argument matcher with verify() method:
package com.logicbig.example;
import org.junit.Test;
import org.mockito.Mockito;
public class ProcessorTest {
@Test
public void processTest() {
MyService myService = Mockito.mock(MyService.class);
String processName = "dummy-process-name";
Mockito.when(myService.doSomething(processName))
.thenReturn(10);
MyProcessor myProcessor = new MyProcessor(processName, myService);
myProcessor.process();
Mockito.verify(myService).doSomething(Mockito.anyString());
}
}
D:\mockito-verify-method-with-argument-matchers-example>mvn test -Dtest=ProcessorTest [INFO] Scanning for projects... [INFO] [INFO] --< com.logicbig.example:mockito-verify-method-with-argument-matchers-example >-- [INFO] Building mockito-verify-method-with-argument-matchers-example 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mockito-verify-method-with-argument-matchers-example --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\mockito\verify-behavior\mockito-verify-method-with-argument-matchers-example\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ mockito-verify-method-with-argument-matchers-example --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 2 source files to D:\example-projects\mockito\verify-behavior\mockito-verify-method-with-argument-matchers-example\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mockito-verify-method-with-argument-matchers-example --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\mockito\verify-behavior\mockito-verify-method-with-argument-matchers-example\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ mockito-verify-method-with-argument-matchers-example --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:\example-projects\mockito\verify-behavior\mockito-verify-method-with-argument-matchers-example\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mockito-verify-method-with-argument-matchers-example --- [INFO] Surefire report directory: D:\example-projects\mockito\verify-behavior\mockito-verify-method-with-argument-matchers-example\target\surefire-reports
------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.logicbig.example.ProcessorTest My Integer is: 10 Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.821 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.850 s [INFO] Finished at: 2020-07-23T00:38:42-05:00 [INFO] ------------------------------------------------------------------------
Example ProjectDependencies and Technologies Used: - mockito-core 3.3.3: Mockito mock objects library core API and implementation.
- junit 4.13: JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck.
- JDK 8
- Maven 3.5.4
|