In this post, We will talk and learn about @Captor Annotation in Mockito with Example
Mockito ArgumentCaptor and @Captor Annotation
Usually, Mockito’s ArgumentCaptor is used to capture arguments for mocked methods. ArgumentCaptor is used with Mockito to verify() methods to get the arguments passed when any method is called. This way usually, we can use additional JUnit assertions for our test cases.
Mockito ArgumentCaptor
We can usually create ArgumentCaptor
an instance for any given class, then we use its capture()
method to use with verify()
methods.
Finally, we can get arguments from getValue()
and getAllValues()
methods.
getValue()
the method can be used when we have captured a single argument. If the verified method was called multiple times then getValue()
the method will return the latest captured value.
If you have multiple arguments to capture then you have to call getAllValues()
to get the list of arguments.
Mockito ArgumentCaptor Example
Mockito @Captor
We can use @Captor
Mockito annotation to create argument captor at field level. So instead of initializing field level ArgumentCaptor as:
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
We can use @Captor as:
@Captor
private ArgumentCaptor<String> argumentCaptor;
Note that we have to call MockitoAnnotations.initMocks(this);
before test methods to get it initialized by the Mockito framework.
Mockito @Captor Example
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutoials</groupId> <artifactId>MockitoCaptorAnnotationExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-junit-jupiter</artifactId> <version>3.5.7</version> <scope>test</scope> </dependency> </dependencies> </project> |
Person.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.kkjavatutorials.model; public class Person { private Integer id; private String name; public Person(Integer id, String name) { this.id = id; this.name = name; } public Integer getId() { return id; } public String getName() { return name; } } |
MockitoCaptorTest1.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.kkjavatutoials; import java.util.List; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; public class MockitoCaptorTest1 { @Test void capterTest() { List<String> mockList = mock(List.class); mockList.add("KK"); ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class); verify(mockList).add(argumentCaptor.capture()); assertEquals("KK", argumentCaptor.getValue()); } } |
MockitoCaptorTest2.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.kkjavatutoials; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import java.util.List; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(value = { MockitoExtension.class }) public class MockitoCaptorTest2 { @Captor private ArgumentCaptor<String> argumentCaptor; @Test void capterTest() { List<String> mockList = mock(List.class); mockList.add("KK"); verify(mockList).add(argumentCaptor.capture()); assertEquals("KK", argumentCaptor.getValue()); } } |
MockitoCaptorTest3.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.kkjavatutoials; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import java.util.List; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.junit.jupiter.MockitoExtension; import static org.mockito.Mockito.times; import com.kkjavatutorials.model.Person; @ExtendWith(value = { MockitoExtension.class }) public class MockitoCaptorTest3 { @Captor private ArgumentCaptor<Person> argumentCaptor; @Test void capterTest() { List<Person> mockList = mock(List.class); mockList.add(new Person(10, "KK")); mockList.add(new Person(20, "PK")); mockList.add(new Person(30, "SK")); verify(mockList, times(3)).add(argumentCaptor.capture()); List<Person> personList = argumentCaptor.getAllValues(); assertEquals(10, personList.get(0).getId()); assertEquals("KK", personList.get(0).getName()); assertEquals(20, personList.get(1).getId()); assertEquals("PK", personList.get(1).getName()); assertEquals(30, personList.get(2).getId()); assertEquals("SK", personList.get(2).getName()); } } |
Now right-click on the Project and select Run As then JUnit Test as shown in the below Image.
The Output of the above project:
Some Key Points:
- We may use to create org.mockito.ArgumentCaptor<T> instance for any class then its capture() method is used with verify() methods.
- We can capture arguments from getValue() and getAllValues() methods.
- ArgumentCaptor getValue() method can be used when we have captured a single argument. If the verify method was called multiple times then getValue() method will return the latest captured value only.
- If multiple arguments are captured then we may call getAllValues() to get the list of arguments.
You May Also Like:
What is Mocking?
Why Need for mocking?
What are the Benefits of Mockito?
How to mock interface using Mockito example?
Mockito and JUnit Integration Using Maven Example
@Mock and @Spy Mockito Annotations With Example
@InjectMocks Annotation in Mockito with Example
Mockito – Verifying Method Calls
Adding behavior to mocked object in Mockito
Mocking Void Methods with Mockito
Mocking Exception Throwing using Mockito
Mockito’s mock Overloaded Methods
That’s all about @Captor Annotation in Mockito with Example?
If you have any feedback or suggestion please feel free to drop in below comment box.