I have a DAO method I want to verify is called inside Service
send(User user, Properties prop)
I can verify using a protected method in service, but I think it should be private
verify(dao).send(user, service.getProp())
I tried in different ways to define accepting any properties as:
verify(dao).send(user, any(Properties.class)); // or any()
verify(dao).send(user, Matchers.isA(Properties.class)));
But all failed with invalid arguments
FAILED: testService
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at com.package.TestService.testService(TestService.java:330)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
For more info see javadoc for Matchers class.
As the exception details explain, you're not allowed to mix raw values (user) with matchers (any(...)).
Instead, use matchers for all arguments using the eq(...) matcher:
verify(dao).send(eq(user), any());
Related
I am trying to use PowerMockito to mock a private class like this:
PowerMockito.when(spyClass, "privateMethod", anyString()).thenReturn(returnList);
I have these two annotations above the test method:
#Test
#PrepareForTest(CartItemRepository.class)
And these above the class:
#ExtendWith(MockitoExtension.class)
#RunWith(PowerMockRunner.class)
There are no previous method calls (stubbed or otherwise) in the test method that could be interfering.
But, I am getting an error related to incorrect use of argument matchers.
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"))
Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.
I am using the when method of PowerMockito (and not regular Mockito) but the 2nd error paragraph makes it seem like mocking private methods is still not possible.
I'm not sure what I'm doing wrong here.
I'm writing a test case to validate execution with specific method call with specific arguments. The test need to pass only when specific values are passed (for ex. only pass when status & ParseError is passed) to the method. Below is the code snippet for the test to verify:
Mockito.verify(exeImpl, Mockito.atLeastOnce()).setData(eq("status"), eq("ParseError"));
and case failed with below:
Argument(s) are different! Wanted:
exeImpl.setData(
"status",
"ParseError"
);
-> at com.TestTask.testRest(TestTask.java:280)
Actual invocation has different arguments:
exeImpl.setData(
"status",
"Error"
);
-> at
com.TestTask.setDefault(Task.java:186)
The actual invocation which is mentioned in the error is because the setData is called with default values before starting the business logic. After the business logic is done, the setData is called again to set the result.
And setData has below definition
setData(final String arg1, final Object arg2) {...}
I know about ArgumentCaptor, but I'm just trying to make it work like this as i believe the matcher eq() is used here to check the equals of the value passed for both the arguments. Or is it causing issue because it is object.
Will it be possible to verify like this? Thanks.
The issue was with my code as my test was not initialized properly with #Before.
For a case in which stubbed variable in made inside the actual production code,
How can we pass actual arguments with stubbing e.g using Mockito.when in JUnits?
E.g if a method in production code is like:
1) servicaClass.doSomething(Calendar.getInstance)
2) servicaClass.doSomething(someMethodToCreateActualVariable())
while testing how a actual parameter can be passed?
Like
-> when(mockedServiceClass.doSomething(Calendar.geInstance)).thenReturn("")
But while testing production code will take its own calendar value while executing.
There can be a way to make public setter getter method for the used variable to be stuffed. But that dont seem to be a optimum solution.
Any pointers with this regard will be helpful?
If you know the matching value before the fact, you can use stubbing. Mockito matchers like eq (compare with equals) and same (compare with ==) will help there, or you can get the eq behavior by specifying the value directly. Note that you have to use Matchers for all values if you use any at all; you can't use Matchers for only one argument of a two-argument method call.
// Without matchers
when(yourMock.method(objectToBeComparedWithEquals))
.thenReturn(returnValue);
// With matchers
when(yourMock.method(eq(objectToBeComparedWithEquals)))
.thenReturn(returnValue);
when(yourMock.method(same(objectToBeComparedReferentially)))
.thenReturn(returnValue);
If you don't know the matching value until after you run the method, you might want verification instead. Same rules apply about Matchers.
SomeValue someValue = yourSystemUnderTest.action();
verify(yourMock).initializeValue(someValue);
And if you need to inspect the value after the fact, you can use a Captor:
ArgumentCaptor myCaptor = ArgumentCaptor.forClass(SomeValue.class);
yourSystemUnderTest.action();
verify(yourMock).initializeValue(myCaptor.capture());
SomeValue valueMockWasCalledWith = myCaptor.getValue();
// Now you've retrieved the reference out of the mock, so you can assert as usual.
assertEquals(42, valueMockWasCalledWith.getInteger());
I have a method which return array of object.
public IConfigurationElement[] getConfigurationElementsFor(String extensionPointId);
I am not sure how can I mock this call using mockito and powermock.
I have tried
mockConfigurationElements = (IConfigurationElement[]) Mockito.anyListOf( IConfigurationElement.class ).toArray();
but this is ending in ClassCastException.
Mocking (stubbing) calls with Mockito is done in a following way (for example):
Mockito.when(mockObject.getConfigurationElementsFor(Mockito.anyString()).thenReturn(new IConfigurationElement[]{})
or
Mockito.doReturn(new IConfigurationElement[]{}).when(mockObject).getConfigurationElementsFor(Mockito.anyString());
Mockito.anyListOf() is a use of a matcher. Matchers are passed instead of real arguments when stubbing meaning that the behavior is to be applied if the method is called with arguments satisfying those matchers.
getting the error
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
1 matchers expected, 2 recorded.
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
at
when(
getProgramService
.callService(any(GetProgramType.class)))
.thenReturn(jAXBresponse);
please explain the error and possible resolution
The code you posted looks fine; I don't see a problem with it. It is likely the code immediately before or above it causing an exception. The key is here:
Invalid use of argument matchers! 1 matchers expected, 2 recorded.
any, as you used it above, doesn't return a "special kind of null" or "special kind of GetProgramType"; those don't exist. Instead, it returns null and as a side effect puts a matcher on a stack. When checking for matchers, Mockito looks at the stack, and checks that it is either empty (i.e. check equality with all arguments) or exactly equal to the number of arguments in the call you're checking (i.e. there is a matcher for each argument).
What's happening here is that you're getting one more matcher than the callService method expects. I see this often enough when developers mistakenly try to save a matcher in a local variable:
String expectedString = Matchers.anyString(); // DOESN'T WORK
// Instead, this just adds a matcher to the stack, almost certainly where it
// doesn't belong.
...or mock a final method:
// Assume getProgramService.otherMethod is final.
when(getProgramService.otherMethod(anyString())).thenReturn(123L);
// This actually calls getProgramService.otherMethod(null) and leaves an
// anyString matcher on the stack without setting any expectation that
// would returns 123L.
To confirm this is a problem, temporarily add this statement immediately before the one you posted:
Mockito.validateMockitoUsage();
This artificially checks the stack for matchers, and will likely throw an exception on that line. If it does, check the code above your post. Either way, adding some surrounding code to your question will likely help us debug it.