How match varargs in mockito 2? - mockito

Approach from mockito 1 not working after updating to 2.3.
private class ArgumentsMatcher implements ArgumentMatcher<Object[]> {
private final Object[] expected;
private ArgumentsMatcher(Object[] expected) {
this.expected = expected;
}
#Override
public boolean matches(Object[] argument) {
return Arrays.equals(expected, argument);
}
}

You can match against it using a captor like this:
// Use an argument captor of whatever type the varargs method is
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
// Verify on the method using the captor
verify(fooClass).fooMethod(captor.capture());
// Assert on the expected values
assertEquals(captor.getAllValues(), Arrays.asList("vararg1", "vararg2"));
The nice thing about this is that you can match against arrays (if you're in a case where arrays and varargs can be mixed) also use whatever hamcrest matchers you want so you can do things like verify a single element is present, ignore order, ignore duplicates, or whatever else you need to do.

It looks like the VarargMatcher interface is not needed anymore. I am using Mockito in a Scala project and it appears you can create custom varargs matchers like normal custom matchers but you just treat the argument as a sequence (not sure how it works in Java, I suspect you get an Array or a List).
A matcher checking if the varargs contain a certain element works like this:
import org.mockito.ArgumentMatcher
case class IsVarargsContaining[T](expected: T) extends ArgumentMatcher[T] {
override def matches(arg: T): Boolean =
arg.isInstanceOf[Seq[T]] && arg.asInstanceOf[Seq[T]].contains(expected)
override def toString() = s"<vararg list containing element $expected>"
}

It looks like this is no longer supported in Mockito 2. The alternative to your code would be:
// imagine this was expected
String[] expected = {"A", "B", "C"};
// and this was the method
void call(String ... varArgs);
// here's how you'd verify it
verify(someFunction).call(eq("A"), eq("B"), eq("C"));
// or
verify(someFunction).call(any());

I'm using mockito 2.15 and I ended up with creating a custom Matcher that implements org.mockito.ArgumentMatcher<T> and takes a set of values to match as a constructor argument.
Then I just pass it around:
verify(someone).aMethods(argThat(matcher), argThat(matcher), etc.);
Not great (the number of argThat() calls has to match the args length) but at least it's not order-sensitive...

Related

Map is not getting populated for ArgumentMatcher object

I am trying to mock an external call along with an ArgumentMatcher to match the input values to the request. But when I trying to fetch the map from the ArgumentMatcher object, it gives me a null value.
Mockito.when(
dynamoDbMapper.scanPage(eq(ABC.class), argThat(new ArgumentMatcher<DynamoDBScanExpression>() {
#Override
public boolean matches(Object argument)
{
DynamoDBScanExpression scanExp = (DynamoDBScanExpression) argument;
Assert.assertEquals("5", scanExp.getLimit());
Assert.assertEquals("xyz",scanExp.getFilterExpression());
Assert.assertEquals(new HashMap(), scanExp.getExpressionAttributeNames());
return true;
}
}))).thenReturn(prepareScanResponse());
This expression scanExp.getExpressionAttributeNames() should ideally return a map but gives me a null value.
So suppose I have to mock a request whose input contains a map, and then try to implement ArgumentMatcher on that inout object which contains a map as an attribute, how would I do that?
Why not use a #Captor? Captors are used to get record parameters passed to methods. It seems like a cleaner way than to try to misuse a matcher.
#ExtendWith(MockitoExtension.class)
class MarketplaceHttpConnectorImplTest {
#Captor
ArgumentCaptor<DynamoDBScanExpression> scanExpressionCaptor;
#Mock
DynamoMapper dynamoDbMapper; // or something like this
#InjectMocks
MyClassToTest sut; // System Under Test
#Test
public void myTest() {
// prepare mocks
when(dynamoDbMapper.scanPage(eq(ABC.class), any(DynamoDBScanExpression.class)).thenReturn(prepareScanResponse());
// Now call the method to test
sut.methodToCall();
// Verify calls
verify(dynamoDbMapper, times(1)).scanPage(eq(ABC.class), scanExpressionCaptor.capture());
DynamoDBScanExpression param = scanExpressionCaptor.getValue();
// now test what was passed to the method.
assertNotNull(param);
// .....
}
}
Btw: don't mind the JUnit5. It also works in JUnit4. Also, I presumed there was just one value. You can capture multiple values in one #Captor and check all values.

Changing Object.toString() dynamically in Groovy has no effect when calling Integer.toString()

I injected overridden method toString into Object.metaClass:
Object.metaClass.toString ={
System.out.println("the string is $delegate")
}
and I thought that following code will execute this method:
1500.toString()
But it didn't not,nothing was printed to the console. That is what exactly confuses me: if something goes bad, then an error is to throw out; if Object.metaClass.toString is found and invoked, then the message will turn up, but why it is not working? What happened inside?
This behavior is correct, because java.lang.Integer overrides Object.toString() with its own implementation. If your assumption was correct then it would mean that you can break overridden method by forcing to use an implementation from parent class.
Consider following Groovy script:
Object.metaClass.toString = {
System.out.println("the string is $delegate")
}
class GroovyClassWithNoToString {}
class GroovyClassWithToString {
#Override
String toString() {
return "aaaa"
}
}
new GroovyClassWithNoToString().toString()
new GroovyClassWithToString().toString()
1500.toString()
Runtime.runtime.toString()
When you run it you will see something like:
the string is GroovyClassWithNoToString#3a93b025
the string is java.lang.Runtime#128d2484
You can see that GroovyClassWithNoToString.toString() called Object.toString() method and its modified version, also Runtime.toString() calls Object.toString() - I picked this class as an example of pure Java class that does not override toString() method.
As you can see overriding toString() method from Object level makes sense for classes that base on Object.toString() implementation. Classes that provide their own implementation of toString() wont use your dynamically modified method. It also explains why following code works:
Object.metaClass.printMessage = {
System.out.println("Hello!")
}
1500.printMessage()
In this example we are adding a new method called printMessage() to Object class and all classes that don't override this method will use this dynamic method we just created. Integer class does not have method like that one so it's gonna print out:
Hello!
as expected.
Also keep in mind that toString() should return a String and it's better to not print anything to output inside this method - you can end up with nasty StackOverflowError caused by circular calls to toString() method.
UPDATE: How toString() method is being picked by Groovy runtime?
Let me show you under the hood what happens when we call following script:
Object.metaClass.toString = {
System.out.println("Hello!")
}
1500.toString()
and let's see what does Groovy during the runtime. Groovy uses Meta Object Protocol (MOP) to e.g. invoke any method called in a Groovy code. In short, when you call any Java or Groovy method it uses MOP as an intermediate layer to find an execution plan for a method - call it directly or use e.g. a method that was injected dynamically.
In our case we use plain Java class - Integer. In this case Groovy will create an instance of PojoMetaMethodSite class to meta class implementation for Java class - an Integer. Every meta method is executed using one of the Groovy groovy.lang.MetaClass implementation. In this case groovy.lang.MetaClassImpl is being used. One of the last methods that picks a method to execute is MetaClassImpl.getMethodWithCachingInternal(Class sender, CallSite site, Class [] params). If you put a breakpoint in the beginning of this method and run a script with a debugger, you will see that this method is executed with following parameters:
In line 1331 you can see that helper method called chooseMethod(e.name, methods, params) is being used:
cacheEntry = new MetaMethodIndex.CacheEntry (params, (MetaMethod) chooseMethod(e.name, methods, params));
This method is responsible for picking the right method to execute when we try to invoke toString() on Integer object. Let's get there and see what happens. Here is what this method implementation looks like:
/**
* Chooses the correct method to use from a list of methods which match by
* name.
*
* #param methodOrList the possible methods to choose from
* #param arguments
*/
protected Object chooseMethod(String methodName, Object methodOrList, Class[] arguments) {
Object method = chooseMethodInternal(methodName, methodOrList, arguments);
if (method instanceof GeneratedMetaMethod.Proxy)
return ((GeneratedMetaMethod.Proxy)method).proxy ();
return method;
}
Source: https://github.com/apache/groovy/blob/GROOVY_2_4_X/src/main/groovy/lang/MetaClassImpl.java#L3158
Now let's see what parameters are received when we call our script:
What is most interesting in our case is the first element of methodOrList.data. It's a method object of:
public java.lang.String java.lang.Integer.toString()
which is the method toString() that Integer class overrides from its parent class. Groovy runtime picks this method, because it is the most accurate from the runtimes point of view - it is the most specific method for Integer class provided. If there is no toString() method overridden at the class level (e.g. Runtime class example I mentioned earlier) then the best candidate for invoking toString() method is a ClosureMetaMethod provided by us in Object.metaClass.toString = .... I hope it gives you a better understanding of what happens under the hood.
I don't think you can override the Object.toString() that way.
But this works:
Integer.metaClass.toString = { ->
System.out.println("the string is $delegate")
}
https://groovyconsole.appspot.com/script/5077208682987520

Mockito verify method was called with set, that contains specific value

I want to be sure that mocked is called with specific set of strings as parameter.
For example, I have the following code:
public class SomeLogic {
#Autowired
private SpecificService specificService;
public void action() {
Set<String> args = fillArgsMethod();
specificService.handleArgs(args);
}
}
And my current try to test it is the following
#Mock
private SpecificService specificService
#InjectMocks
private SomeLogic someLogic;
#Test
public void testAction() {
someLogic.action();
verify(specificService).handleArgs(anySet());
}
But I want to be sure, that handleArgs() will receive the exact set of strings, that I expect. How can I modify verifying to check that handleArgs is called with set "first","second"?
Thanks
Isah gave a valid answer, but I want to turn your attention to a more general feature of Mockito which is ArgumentCaptor
In you case you would do something along the following lines:
Class<HashSet<String>> setClass = (Class<HashSet<String>>)(Class)HashSet.class;
ArgumentCaptor<Set<String>> setCaptor= ArgumentCaptor.forClass(setClass .class);
verify(specificService).create(setCaptor.capture());
HashSet<String> capturedSet = setCaptor.getValue();
//do whatever test you want with capturedSet
Prepare your Set parameters before calling the test method
#Test
public void testAction() {
Set<String> expectedParams = new HashSet(Arrays.asList("first", "second");
//call tested method
verify(specificService).handleArgs(expectedParams);
}
isah's solution is perfect for you if you want to confirm that the set contains exactly the two items you specify; Mockito compares using .equals by default, and Set.equals is defined as refer to equal elements in any order.
For a more-flexible "contains" test that matches your question title, that allows for set members beyond your expected values, you can also use the Hamcrest contains matcher:
someLogic.action();
verify(specificService).handleArgs(argThat(contains("first", "second")));
At least, that's how it should look. Unfortunately, argThat infers its return type from the Matcher, which infers its return type from the arguments, so Java assumes your first argument is not a Set<String> but a Iterable<capture#1-of ? extends String>. You'll need to cast explicitly and suppress warnings to get it to work:
// requires #SuppressWarnings("unchecked")
verify(specificService).handleArgs(
(Set<String>) argThat(contains("first", "second")));

ValueConverter for String to String

I need to perform additional modifying of all Strings, is it possible to make a ValueConverter with a Target Type of String, or if not, in what other way can I do it?
class StringValueConverter implements ValueConverter {
#Override
boolean canConvert(Object value) {
value instanceof String
}
#Override
Object convert(Object value) {
//modifying value
}
#Override
Class<?> getTargetType() {
return String
}
}
I need to do it before binding
In Groovy yes you can. There are various ways:
Using Extension Module is one of them.
Another way would be to use runtime metaprogramming, for example:
String.metaClass.flip = {
delegate.reverse()
}
assert "Hello".flip() == "olleH"
Above is a contrived example of adding methods to metaClass of String, but a convert method as in your example can fit in as well.
However, the extension module is what can be robust in your case if you are looking for adding sanity by canConvert which would not be needed for runtime metaClass methods as they are only available for String class.

Can i make a linq expression optional parameter in c# 4.0?

I have the following code.
public void GetMessages(Expression<Func<IMessageQueryable, bool>> messageSpecification, string folder = "INBOX")
{
// Implementation stripped
}
How can i provide default value for messageSpecification?. Specification says the value must be a compile time constant. Is this possible?.
EDIT: Not lookig for specifying it as Expression<Func<IMessageQueryable, bool>> messageSpecification = null
You can OverLoad it. What would your default value be?
Why bother? Create an overload for the same method without messageSpecification parameter and define it's default value yourself inside the overloaded method and pass it to your original method. Default parameters are actually never meant to be used like that anyway.
public void GetMessages(string folder = "INBOX")
{
this.GetMessages(DEFAULT_VALUE, folder);
}

Resources