Mockito and matching varargs properly with overloaded methods with Spring Redis - java

I have following method in my service.
private final ReactiveRedisTemplate<String, String> reactiveRedisTemplate;
public Mono<Boolean> deleteObject(String... keys) {
return reactiveRedisTemplate.delete(keys).map(c -> c > 0);
}
ReactiveRedisTemplate has two internal implementations for delete with different signatures.
#SafeVarargs
public final Mono<Long> delete(K... keys) {
Assert.notNull(keys, "Keys must not be null!");
Assert.notEmpty(keys, "Keys must not be empty!");
Assert.noNullElements(keys, "Keys must not contain null elements!");
if (keys.length == 1) {
...
public Mono<Long> delete(Publisher<K> keys) {
Assert.notNull(keys, "Keys must not be null!");
return this.doCreateFlux((connection) -> {
Now, when I have the following
var key = "key";
when(reactiveRedisTemplate.delete(key)).thenReturn(Mono.just(1L));
StepVerifier.create(service.removeObject(key))
.expectNextMatches(c -> c)
.verifyComplete();
verify(this.reactiveRedisTemplate, times(1)).delete(key);
I'm getting error NullPointerException, so it looks like the varargs doesn't match. With ArgumentMatcher, following is received:
when(reactiveRedisTemplate.delete(ArgumentMatchers.<String>any())).thenReturn(Mono.just(1L));
=> java.lang.IllegalArgumentException: Keys must not contain null elements!
when(reactiveRedisTemplate.delete(eq(new String[] { key }))).thenReturn(Mono.just(1L));
=> java.lang.IllegalArgumentException: Keys must not be null!
when(reactiveRedisTemplate.delete(anyString())).thenReturn(Mono.just(1L));
=> java.lang.NullPointerException (doesn't seem to match)
lastly a custom matcher:
public class StringVarargsMatcher implements VarargMatcher, ArgumentMatcher<String[]> {
private String[] expectedValues;
public StringVarargsMatcher(String... expectedValues) {
Arrays.sort(expectedValues);
this.expectedValues = expectedValues;
}
#Override
public boolean matches(String[] strings) {
Arrays.sort(strings);
return Arrays.equals(strings, expectedValues);
}
}
when(reactiveRedisTemplate.delete(argThat(new StringVarargsMatcher(key)))).thenReturn(Mono.just(1L));
=> java.lang.IllegalArgumentException: Keys must not be null!
Is there any way to overcome this so I could mock delete from ReactiveRedisTemplate and get a unit test done here?

You are not mocking public final Mono<Long> delete(K... keys) as it is a final method and you are not using inline mock maker (as discussed in comments).
As a consequence, real method is called.
ArgumentMatcher factory methods, such as any()
register a metcher in Mockitos internal state
return null
Combine that with a real method being called - you get errors on assertions made by the real method.
See: Mocking final types, enums and final methods

Related

Java - how to analyze a function code

We are working with mvc design pattern, where all the data is stored under map.
I want to iterate over all the classes in the system and for each to check what the method is putting on the map and what does the method get from the map.
For example for the next code:
private void myFunc()
{
Object obj = model.get("mykey");
Object obj2 = model.get("mykey2");
.....
model.put("mykey3", "aaa");
}
I want to know that in this function we have 2 gets: mykey and mykey2 and 1 put: mykey3
How can I do it with the code.
Thanks.
You tagged this with "reflection", but that will not work. Reflection only allows you to inspect "signatures". You can use it to identify the methods of a class, and the arguments of the methods.
It absolutely doesn't help you to identify what each method is doing.
In order to find out about that, you would need to either parse the java source code side, or byte code classes. As in: write code that reads that content, and understands "enough" of it to find such places. Which is a very challenging effort. And of course: it is very easy to bypass all such "scanner" code, by doing things such as:
List<String> keysToUpdate = Arrays.asList("key1", "key2");
for (String key : keysToUpdate) {
... does something about each key
Bang. How would you ever write code that reliable finds the keys for that?
When you found that code, now imagine that the list isn't instantiated there, but far away, and past as argument? When you figured how to solve that, now consider code that uses reflection to acquire the model object, and calls method on that. See? For any "scanner" that you write down, there will be ways to make that fail.
Thus the real answer is that you are already going down the wrong rabbit hole:
You should never have written:
Object obj = model.get("mykey");
but something like
Object obj = model.get(SOME_CONSTANT_FOR_KEY_X);
Meaning: there is no good way to control such stuff. The best you can do is to make sure that all keys are constants, coming from a central place. Because then you can at least go in, and for each key in that list of constants, you can have your IDE tell you about their usage.
NOTES
I assumed that your situation is complicated enough that simple or advanced text search in codebase doesn't help you.
This is a hack not a generic solution, designed only for testing and diagnosis purposes.
To use this hack, you must be able to change your code and replace the actual model with the proxy instance while you're testing/diagnosing. If you can't do this, then you have to use an even more advanced hack, i.e. byte-code engineering with BCEL, ASM, etc.
Dynamic proxies have drawbacks on code performance, therefore not an ideal choice for production mode.
Using map for storing model is not a good idea. Instead a well-defined type system, i.e. Java classes, should be used.
A general design pattern for a problem like this is proxy. An intermediate object between your actual model and the caller that can intercept the calls, collect statistics, or even interfere with the original call. The proxied model ultimately sends everything to the actual model.
An obvious proxy is to simply wrap the actual model into another map, e.g.
public class MapProxy<K, V> implements Map<K, V> {
public MapProxy(final Map<K, V> actual) {
}
// implement ALL methods and redirect them to the actual model
}
Now, reflection doesn't help you with this directly, but can help with implementing a proxy faster using dynamic proxies (Dynamic Proxy Classes), e.g.
#SuppressWarnings("unchecked")
private Map<String, Object> proxy(final Map<String, Object> model) {
final InvocationHandler handler = new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Collect usage stats or intervene
return method.invoke(model, args);
}
};
return (Map<String, Object>) Proxy.newProxyInstance(Map.class.getClassLoader(),
new Class<?>[] { Map.class }, handler);
}
NOTE: Either case you need to be able to replace the actual model with the proxied model at least for the duration of your test.
With another trick, you can find out who called which method of your model. Simply by accessing Thread.currentThread().getStackTrace() and retrieving the appropriate element.
Now puting all the pieces together:
InvocationLog.java
public final class InvocationLog {
private Method method;
private Object[] arguments;
private StackTraceElement caller;
public InvocationLog(Method method, Object[] arguments, StackTraceElement caller) {
this.method = method;
this.arguments = arguments;
this.caller = caller;
}
public Method getMethod() { return this.method; }
public Object[] getArguments() { return this.arguments; }
public StackTraceElement getCaller() { return this.caller; }
#Override
public String toString() {
return String.format("%s (%s): %s",
method == null ? "<init>" : method.getName(),
arguments == null ? "" : Arrays.toString(arguments),
caller == null ? "" : caller.toString());
}
}
ModelWatch.java
public final class ModelWatch {
private final Map<String, Object> modelProxy;
private final List<InvocationLog> logs = new ArrayList<>();
public ModelWatch(final Map<String, Object> model) {
modelProxy = proxy(model);
}
#SuppressWarnings("unchecked")
private Map<String, Object> proxy(final Map<String, Object> model) {
final InvocationHandler handler = new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
log(method, args, Thread.currentThread().getStackTrace());
return method.invoke(model, args);
}
};
return (Map<String, Object>) Proxy.newProxyInstance(Map.class.getClassLoader(),
new Class<?>[] { Map.class }, handler);
}
private void log(Method method, Object[] arguments, StackTraceElement[] stack) {
logs.add(new InvocationLog(method, arguments, stack[3]));
// 0: Thread.getStackTrace
// 1: InvocationHandler.invoke
// 2: <Proxy>
// 3: <Caller>
}
public Map<String, Object> getModelProxy() { return modelProxy; }
public List<InvocationLog> getLogs() { return logs; }
}
To put it in use:
private Map<String, Object> actualModel = new HashMap<String, Object>();
private ModelWatch modelWatch = new ModelWatch(model);
private Map<String, Object> model = modelWatch.getModelProxy();
// Calls to model ...
modelWatch.getLogs() // Retrieve model activity

Checking an object is correctly built in Java

This is a general issue/problem that I have come across. I wondered if anyone knows of any well suited design patterns or techniques.
private ExternalObject personObject;
private String name;
private int age;
private String address;
private String postCode;
public MyBuilderClass(ExternalObject obj)
this.personObject=obj;
build();
}
public build() {
setName(personObject.getName());
setAge(personObject.getAge());
setAddress(personObject.getAddress());
setPostCode(personObject.getPostCode());
.
.
. many more setters
}
The class above takes external objects from a queue and constructs MyBuilderClass objects.
A MyBuilderClass object is successfully built if all of the fields have been set to non-null non-empty values.
There will be many MyBuilderClass objects that cannot be built because data will be missing from the ExternalObject.
My problem, what is the best way to detect if an object has been correctly built?
I could check for null or empty values in the set methods and throw an exception. The problem with this approach is throwing exceptions is expensive and it will clogg the log files up because there will be many instances where an object cannot be built;
What other approaches could I use?
Correct me if I'm wrong: you are trying to find a good way to check if an object is valid, and if it is not, tell the client code about this without using an exception.
You can try a factory method:
private MyBuilderClass(ExternalObject obj)
this.personObject=obj;
build();
}
public static MyBuilderClass initWithExternalObject(ExternalObject obj) {
// check obj's properties...
if (obj.getSomeProperty() == null && ...) {
// invalid external object, so return null
return null;
} else {
// valid
MyBuilderClass builder = new MyBuilderClass(obj);
return builder.build();
}
}
Now you know whether an object is valid without using an exception. You just need to check whether the value returned by initWithExternalObject is null.
I wouldn't throw exceptions in cases that aren't exceptional. And as the only way for a constructor not to produce an object is to throw, you should not delay validation to the constructor.
I'd still recommend the constructor to throw if its results were to be invalid, but there should be a validation before that, so you don't even call the constructor with an invalid ExternalObject.
It's up to you if you want to implement that as a static method boolean MyBuilderClass.validate(ExternalObject) or by using the builder pattern with this validation.
Another approach for such a validation is to use java Annotations:
Make a simple annotaion class, let's say Validate:
#Target({ElementType.FIELD})
#Retention(RetentionPolicy.RUNTIME)
#interface Validate {
boolean required() default true;
}
then annotate the fields you want to be present as #Validate(required=true):
class MyBuilderClass {
private ExternalObject externalObject;
#Validate(required=true)
private String name;
#Validate(required=false) /*since it's a primitive field*/
private int age;
#Validate(required=true)
private String address;
#Validate(required=true)
private String postCode;
MyBuilderClass(ExternalObject externalObject) {
this.externalObject = externalObject;
build();
}
public void build() {
setName(personObject.getName());
setAge(personObject.getAge());
setAddress(personObject.getAddress());
setPostCode(personObject.getPostCode());
}
//.
//.
//. many more setters
}
And then add this method in the MyBuilderClass class, in order to check if your Object is built correctly:
public boolean isCorrectlyBuilt() throws IllegalAccessException {
boolean retVal = true;
for (Field f : getClass().getDeclaredFields()) {
f.setAccessible(true);
boolean isToBeChecked = f.isAnnotationPresent(Validate.class);
if (isToBeChecked) {
Validate validate = f.getAnnotation(Validate.class);
if (validate.required()/*==true*/) {
if (f.get(this) == null) {
retVal = false;
break;
/* return false; */
}
}
}
}
return retVal;
}
Here is an example of use :
public static void main(String[] args) throws Exception {
ExternalObject personObject = new ExternalObject();
personObject.setAge(20);
personObject.setName("Musta");
personObject.setAddress("Home");
personObject.setPostCode("123445678");
MyBuilderClass myBuilderClass = new MyBuilderClass(personObject);
System.out.println(myBuilderClass.isCorrectlyBuilt());
}
Output : true because the object is correctly built.
This will allow you to choose the fields that you want to be in the structure by reflection, without bringing those inherited from a base class.
As this previous answer suggests, here are 2 options either of which should be added after you have tried to set the variables.
use reflection to check whether any of the variables are null. (As mentioned in comments this will check all fields in this object but be careful with fields in any superclasses).
public boolean checkNull() throws IllegalAccessException {
for (Field f : getClass().getDeclaredFields())
if (f.get(this) != null)
return false;
return true;
}
perform a null check on each variable.
boolean isValidObject = !Stream.of(name, age, ...).anyMatch(Objects::isNull);
Previous answer
From what I've come across you could overwrite the equals method of your object and compare it with a valid example object. Its dirty and might only work in some cases.
Your approach is the best I could think of. Write a seperate method or class that has for example a static validate method. You could reuse it anywhere.

Caching variables in a custom Hamcrest Matcher

I have created a custom Hamcrest matcher for an interface I'm using.
The matcher is an instance of TypeSafeMatcher and it overrides the following three methods:
TypeSafeMatcher#matchesSafely(T item) : boolean
TypeSafeMatcher#describeMismatchSafely(T item, Description mismatchDescription) : void
TypeSafeMatcher#describeTo(Description description) : void
The class I'm matching handles the validation of a certain type of objects. It comes from an external library so I cannot simply change it. Let's call this class ValidationSubject
Every instance of ValidationSubject this class defines some logic behind the validation to be performed. This is done by implementing ValidationSubject#validate(ValidationData validationData) where validationData is a builder-type object that allows the programmer to report validation errors based on the state of an object of a class implementing ValidationSubject
public class Foo implements ValidationSubject {
private String state;
private Map<String, Baz> moreState;
// constructor, methods affecting the state
// this method is required by ValidationSubject
#Override
public void validate(ValidationData validationData) {
/*
* call methods on validationData based on the state
* of the object
*/
}
}
I'm using my matcher to test the validation logic implemented in each concrete class such as Foo.
In order to do that, I'd need to stub/mock/spy an instance of ValidationData in each test case and see how the state of the ValidationData object changed based on the logic performed by the subject under test. That's a lot of boilerplate. I want my matcher to abstract that away
assertThat(testedValidationSubject, hasValidationErrors("Illegal character in name", "Description exceeds 200 words", "Age cannot be negative"));
In this case, what I'm really matching against the arguments of the hasValidationErrors matcher is a set of String values that the subject under test stored in the ValidationData object.
Extracting these values takes a bit of code.
return new TypeSafeMatcher<ValidationSubject>() {
#Override
protected boolean matchesSafely(ValidationSubject item) {
// this calls the relevant methods on 'item' internally
Validator validator = new Validator(item);
List<ValidationMessage> errorMessages = validator.getErrorMessageGroup()
.getMessages();
Set<String> actualMessages = errorMessages.stream().map(e -> e.getMessage())
.collect(Collectors.toSet());
Set<String> expectedMessages = Stream.of(expectedErrors).collect(Collectors.toSet());
Set<String> missingMessages = SetUtils.difference(expectedMessages, actualMessages);
Set<String> unexpectedMessages = SetUtils.difference(actualMessages, expectedMessages);
return SetUtils.union(unexpectedMessages, missingMessages).isEmpty();
}
#Override
public void describeMismatchSafely(final ValidationSubject item, final Description description) {
// this calls the relevant methods on 'item' internally
Validator validator = new Validator(item);
List<ValidationMessage> errorMessages = validator.getErrorMessageGroup()
.getMessages();
Set<String> actualMessages = errorMessages.stream().map(e -> e.getMessage())
.collect(Collectors.toSet());
Set<String> expectedMessages = Stream.of(expectedErrors).collect(Collectors.toSet());
Set<String> missingMessages = SetUtils.difference(expectedMessages, actualMessages);
Set<String> unexpectedMessages = SetUtils.difference(actualMessages, expectedMessages);
description.appendText("Validation errors were missing or unexpected\n")
.appendValueList("\tSupefluous messages: ", ", ", "\n", unexpectedMessages.toArray())
.appendValueList("\tMissing messages: ", ", ", "\n", missingMessages.toArray());
}
#Override
public void describeTo(Description description) {
description.appendText("validation should result in the expected errors");
}
}
This piece of code is repeated line-by-line:
Validator validator = new Validator(item);
List<ValidationMessage> errorMessages = validator.getErrorMessageGroup()
.getMessages();
Set<String> actualMessages = errorMessages.stream().map(e -> e.getMessage())
.collect(Collectors.toSet());
Set<String> expectedMessages = Stream.of(expectedErrors).collect(Collectors.toSet());
Set<String> missingMessages = SetUtils.difference(expectedMessages, actualMessages);
Set<String> unexpectedMessages = SetUtils.difference(actualMessages, expectedMessages);
I can get rid of the duplication by wrapping this piece in a method or a lambda expression (returning a pair of sets or accepting as a parameter a function to compute the boolean or string I need) but ideally, I'd like to only execute this once.
I need the item to figure out the result of both matchesSafely and the message output by describemisMatchSafely but each time it's passed as a parameter. It's not a parameter of the static method hasValidationErrors so I can't see a clean way to cache the result in a couple of variables.
I could potentially execute this code in one of those methods and cache it in a field but the Javadoc for TypeSafeMatcher seems to make no guarantees as to which method is executed first.
If I understand what you're trying to do, you're looking for functionality provided by TypeSafeDiagnosingMatcher. Try extending that instead of TypeSafeMatcher:
return new TypeSafeDiagnosingMatcher<ValidationSubject>() {
#Override
protected boolean matchesSafely(ValidationSubject item, Description mismatchDescription) {
// this calls the relevant methods on 'item' internally
Validator validator = new Validator(item);
List<ValidationMessage> errorMessages = validator.getErrorMessageGroup()
.getMessages();
Set<String> actualMessages = errorMessages.stream().map(e -> e.getMessage())
.collect(Collectors.toSet());
Set<String> expectedMessages = Stream.of(expectedErrors).collect(Collectors.toSet());
Set<String> missingMessages = SetUtils.difference(expectedMessages, actualMessages);
Set<String> unexpectedMessages = SetUtils.difference(actualMessages, expectedMessages);
mismatchDescription.appendText("Validation errors were missing or unexpected\n")
.appendValueList("\tSuperfluous messages: ", ", ", "\n", unexpectedMessages.toArray())
.appendValueList("\tMissing messages: ", ", ", "\n", missingMessages.toArray());
return SetUtils.union(unexpectedMessages, missingMessages).isEmpty();
}
#Override
public void describeTo(Description description) {
description.appendText("validation should result in the expected errors");
}
}

Java Mockito.when failing with StringBuilder as parameter

Below is the JUnit Test, oneLoginAuthUtil is Mocked. But Mockito.when is returning null. oneLoginAuthUtil.getMetaData is always null. Below is the code -
public void func() throws Exception {
StringBuilder b = new StringBuilder("test");
RequestContext context = new RequestContext();
Mockito.when(oneLoginAuthUtil.getMetaData(context, b)).thenReturn("abcdef");
ResponseEntity<Object> response = loginControllerImpl.handleGetMetaDataEndPointImpl(context);
}
public String getMetaData(RequestContext context, StringBuilder b) throws Exception {
Auth auth = getOneLoginAuthObject(context);
final Saml2Settings settings = auth.getSettings();
String metadata = settings.getSPMetadata();
List<String> errors = Saml2Settings.validateMetadata(metadata);
if (!errors.isEmpty()) {
b.append(errors.toString());
throw new SSOException("metadata_validation_error");
}
return metadata;
}
public ResponseEntity<Object> handleGetMetaDataEndPointImpl(RequestContext context) {
try {
StringBuilder b = new StringBuilder();
String metadata = oneLoginAuthUtil.getMetaData(context, b);
log.info(metadata);
return new ResponseEntity<>(metadata, new HttpHeaders(), HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity<>("<error>Exception: " + e.getMessage() + "</error>", new HttpHeaders(),
HttpStatus.CREATED);
}
}
But oneLoginAuthUtil.getMetaData method without StringBuilder as parameter works fine.
It returns null because when you mock a method using specific instances as parameters, Mockito will return the mapped return value if and only if the parameters provided while testing are equal to the parameters that you provided while mocking which is obviously not the case. Mockito did not have a match so it returns the default value for a method that returns an object called on a mock which is null.
In other words Mockito.when(oneLoginAuthUtil.getMetaData(context, b)) is equivalent to Mockito.when(oneLoginAuthUtil.getMetaData(Mockito.eq(context), Mockito.eq(b))), here as the class StringBuilder doesn't override the method equals(Object obj), the instance provided while mocking is not equal to the instance provided while testing as they are not the same instance (the only possibility to be equal when relying on the default implementation of equals(Object obj)) such that you get this behavior.
So you should use Mockito.any(StringBuilder.class) to match with any instances of type StringBuilder such that the instances don't need to be equal anymore, your code would then be:
Mockito.when(
oneLoginAuthUtil.getMetaData(Mockito.eq(context), Mockito.any(StringBuilder.class))
).thenReturn("abcdef");
Assuming that your main class is LoginControllerImpl and that it has a member field oneLoginAuthUtil of type OneLoginAuthUtil, you could use the annotation #InjectMocks to inject directly your mock of type OneLoginAuthUtil, the complete code would then be:
#RunWith(MockitoJUnitRunner.class)
public class LoginControllerImplTest {
#Mock
private OneLoginAuthUtil oneLoginAuthUtil;
#InjectMocks
private LoginControllerImpl loginControllerImpl;
#Test
public void func() throws Exception {
RequestContext context = new RequestContext();
Mockito.when(
oneLoginAuthUtil.getMetaData(
Mockito.eq(context), Mockito.any(StringBuilder.class)
)
).thenReturn("abcdef");
ResponseEntity<Object> response
= loginControllerImpl.handleGetMetaDataEndPointImpl(context);
...
}
}
StringBuilder does not implement equals() or hashCode(), so it can't be used for argument matching like that.
The problem really is that you shouldn't be passing a mutable object (such as StringBuilder) into your test subject. Why does returning "metadata" require you to append to a StringBuilder outside your scope?

mockito verify interactions with ArgumentCaptor

To check the number of interactions with a mock where the parameter in the method call is of a certain type, one can do
mock.someMethod(new FirstClass());
mock.someMethod(new OtherClass());
verify(mock, times(1)).someMethod(isA(FirstClass.class));
This will pass thanks to the call to isA since someMethod was called twice but only once with argument FirstClass
However, this pattern seems to not be possible when using an ArgumentCaptor, even if the Captor was created for the particular argument FirstClass
this doesn't work
mock.someMethod(new FirstClass());
mock.someMethod(new OtherClass());
ArgumentCaptor<FirstClass> captor = ArgumentCaptor.forClass(FirstClass.class);
verify(mock, times(1)).someMethod(captor.capture());
it says the mock was called more than once.
Is there any way to accomplish this verification while capturing the argument for further checking?
I recommend using Mockito's Hamcrest integration to write a good, clean matcher for it. That allows you to combine the verification with detailed checking of the passed argument:
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
verify(mock, times(1)).someMethod(argThat(personNamed("Bob")));
Matcher<Person> personNamed(final String name) {
return new TypeSafeMatcher<Person>() {
public boolean matchesSafely(Person item) {
return name.equals(item.getName());
}
public void describeTo(Description description) {
description.appendText("a Person named " + name);
}
};
}
Matchers generally lead to more readable tests and more useful test failure messages. They also tend to be very reusable, and you'll find yourself building up a library of them tailored for testing your project. Finally, you can also use them for normal test assertions using JUnit's Assert.assertThat(), so you get double use out of them.
Quoting the docs:
Note that an ArgumentCaptordon't do any type checks, it is only
there to avoid casting in your code. This might however change (type
checks could be added) in a future major release.
I wouldn't use an ArgumentCaptor for this. This class captures (literally) everything, despite what class was provided as it's .forClass argument.
To achieve what you want I suggest intercept the argument using Mockito's Answer interface:
private FirstClass lastArgument;
#Test
public void captureFirstClass() throws Exception {
doAnswer(captureLastArgument()).when(mock).someMethod(anInstanceOfFirstClass());
mock.someMethod(new FirstClass());
mock.someMethod(new OtherClass());
verify(mock, times(1)).someMethod(anInstanceOfFirstClass());
//write your desired matchers against lastArgument object
}
private Answer<FirstClass> captureLastArgument() {
return new Answer<FirstClass>() {
#Override
public FirstClass answer(InvocationOnMock invocation) throws Throwable {
TestClass.this.lastArgument = (FirstClass) invocation.getArguments()[0];
return null;
}
};
}
private static Object anInstanceOfFirstClass(){
return Mockito.argThat(isA(FirstClass.class));
}
You can use the the captor for the sake of capturing, then verify the number of invocations with each argument type separately.
// given
ArgumentCaptor<AA> captor = ArgumentCaptor.forClass(AA.class);
CC cc = new CC();
// when
cut.someMethod(new AA());
cut.someMethod(new BB());
cut.someMethod(new BB());
cut.someMethod(cc);
// then
Mockito.verify(collaborator, atLeastOnce()).someMethod(captor.capture());
Mockito.verify(collaborator, times(1)).someMethod(isA(AA.class));
Mockito.verify(collaborator, times(2)).someMethod(isA(BB.class));
Mockito.verify(collaborator, times(1)).someMethod(isA(CC.class));
assertEquals(cc, captor.getValue());
Apparently the generic type of the captor reference doesn't affect anything at runtime.
I also encountered this problem today. I thought I could simply do something like
verify(mock).someMethod(and(isA(FirstClass.class), captor.capture()));
but I couldn't get it to work. I ended up with this solution:
#Test
public void Test() throws Exception {
final ArgumentCaptor<FirstClass> captor = ArgumentCaptor.forClass(FirstClass.class);
mock.someMethod(new FirstClass());
mock.someMethod(new OtherClass());
verify(eventBus, atLeastOnce()).post(captor.capture());
final List<FirstClass> capturedValues = typeCheckedValues(captor.getAllValues(), FirstClass.class);
assertThat(capturedValues.size(), is(1));
final FirstClass capturedValue = capturedValues.get(0);
// Do assertions on capturedValue
}
private static <T> List<T> typeCheckedValues(List<T> values, Class<T> clazz) {
final List<T> typeCheckedValues = new ArrayList<>();
for (final T value : values) {
if (clazz.isInstance(value)) {
typeCheckedValues.add(value);
}
}
return typeCheckedValues;
}
Note: if only one class needs to be captured in this way typeCheckedValues can be simplified into:
private static List<FirstClass> typeCheckedValues(List<FirstClass> values) {
final List<FirstClass> typeCheckedValues = new ArrayList<>();
for (final Object value : values) {
if (value instanceof FirstClass) {
typeCheckedValues.add((FirstClass) value);
}
}
return typeCheckedValues;
}

Categories