How can I capture (for assertion purposes) the parmeters passed to a static stub method call?
The methodBeingStubbed looks like this...
public class SomeStaticClass{
protected static String methodBeingStubbed(Properties props){
...
I am stubbing the method call because it i need to verify that it gets called...
PowerMockito.stub(PowerMockito.method(SomeStaticClass.class, "methodBeingStubbed")).toReturn(null);
PowerMockito.verifyStatic();
But I now also want to know what properties were passed to this "methodBeingStubbed" and assert it is as expected
After the call to verifyStatic, you'll need to actually call the method you're trying to verify, as in the documentation here:
PowerMockito.verifyStatic(Static.class);
Static.thirdStaticMethod(Mockito.anyInt());
At that point you can use Mockito argument captors, as demonstrated (but not tested):
ArgumentCaptor<Properties> propertiesCaptor =
ArgumentCaptor.forClass(Properties.class);
PowerMockito.verifyStatic(SomeStaticClass.class);
SomeStaticClass.methodBeingStubbed(propertiesCaptor.capture());
Properties passedInValue = propertiesCaptor.getValue();
If you're used to #Mock annotations, or you need to capture a generic (as in List<String>), you may also be interested in using the #Captor annotation instead.
Related
My question, which was similar to this:
Using mockito; is it possible to mock a method that takes a lambda as a parameter and assert variables captured by the lambda?
But different enough that I still had take a while to figure it out was:
How do I verify that a method called by a mock that was used inside a lambda that was passed to a method of another mock object?
This may seem convoluted, but it happens a lot with Java 8 libraries like JDBI, for example, you have a JDBI object:
JDBI MyDBConnection
That you should mock. And then that is used with the withHandle method to pass a lambda implementing the HandleCallback<R,X> type:
//code I'm testing. I implement the lambda, and want to verify it
//calls the correct method in dao provided by JDBI.
MyDBConnection.withHandle(
(handle) -> { ... handle.attach(SomeDao.class).findSomethingInDB(args) .. }
Which is the recommended way to do this.
So I want to verify that findSomethingInDB(eq(args)) is called.
Like I said this was similar, but different enough, that, I at least, will find this answer valuable at some future point, when I forget how to do this. So the original 3rd party library method that invokes my lambda is processed similar to the answer given in the question referenced above, but with some tweaks:
when(JDBIMock.withHandle(any())).then(
//Answer<Void> lambda
invocationOnMock -> {
Object[] args = invocationOnMock.getArguments();
assertEquals(1, args.length);
//the interface def for the callback passed to JDBI
HandleCallback lambda = (HandleCallback) args[0];
when(mockHandle.attach(SomeDao.class)).thenReturn(mockDao);
//this actually invokes my lambda, which implements the JDBI interface, with a mock argument
lambda.withHandle(mockHandle);
//bingo!
verify(mockDao).findSomethingInDB(eq(args));
}
)
See the question, it should be answered sufficiently above ;)
I am trying to do something very similar with verifying the arguments passed to another mock from withHandle on a mock JDBI call in a test.
The answer you give in the question pointed me in the right direction but gives me the error message:
The method then(Answer<?>) in the type OngoingStubbing<Object> is not applicable for the arguments ((<no type> invocationOnMock) -> {})
Instead I had to use a new org.mockito.stubbing.Answer passed to the then, similar to in the other question you linked to.
In your example this would be something like:
when(JDBIMock.withHandle(any())).then(
//Answer<Void> lambda
new Answer<Void>() {
#Override
public Void answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
assertEquals(1, args.length);
//the interface def for the callback passed to JDBI
HandleCallback lambda = (HandleCallback) args[0];
when(mockHandle.attach(SomeDao.class)).thenReturn(mockDao);
//this actually invokes my lambda, which implements the JDBI interface, with a mock argument
lambda.withHandle(mockHandle);
//bingo!
verify(mockDao).findSomethingInDB(eq(args));
return null; // to match the Void type
}
}
)
In my case I was expecting a result list from withHandle so I had to change the Answer type, and return type of answer to match and return a dummy list instead of Void. (The actual results returned didn't matter in this test, only that the expected arguments were passed to my subsequent mock object).
I also moved the verify call outside of the Answer into the main body of my test so it was clearer this was the expectation of the test, not part of the mocking setup.
I have scenario when I want mock a method with this parameter, my method looks like below
this.listener = myCustomFactory.buildCustomListener(this);
I have mocked instance of myCustomFactory as given below
#Mock
MyCustomFactory myCustomFactory;
Is it possible to mock method with this parameter. 'this' refer the class for which I am writing unit test, I am creating reference for that class
Mockito.when(this.myCustomFactory.buildCustomListener(this.myClassObjectToBeTested).thenReturn(this.mockedListener);
Using Mockito.any(Class<T> clazz) solved the issue
Mockito.when(this.myCustomFactory.buildCustomListener(Mockito.any(MyClassToBeTested.class)).thenReturn(this.mockedListener);
I've developed an application in Java and I'm trying to create unit tests using Powermockito (I should add that I'm new to unit testing).
I have a class called Resource which has a static method called readResources:
public static void readResources(ResourcesElement resourcesElement);
ResourcesElement is also coded by me.
In testing, I want to create my own Resource, so I want the above method to do nothing.
I tried using this code:
PowerMockito.spy(Resource.class);
PowerMockito.doNothing().when(Resource.class, "readResources", Matchers.any(ResourcesElement.class));
The unit test throws an exception:
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:36)
Powermockito also suggest that I should use thenReturn or thenThrow after when, but it seems that the method 'when' returns void when it is called after doNothing (which is logical).
If I try:
PowerMockito.when(Resource.class, "readResources", Matchers.any(ResourcesElement.class)).....
doNothing is not an option after when.
I managed to make methods without arguments to do nothing, using the 2 arguments version of the method. For example:
PowerMockito.doNothing().when(Moduler.class, "startProcessing");
This works (startProcessing doesn't take any arguments).
But how can I make methods that do take arguments to do nothing with Powermockito?
You can find a fully functional example below. Since you didn't post the complete example, I can only assume that you did not annotate the test class with #RunWith or #PrepareForTest because the rest seems fine.
#RunWith(PowerMockRunner.class)
#PrepareForTest({Resource.class})
public class MockingTest{
#Test
public void shouldMockVoidStaticMethod() throws Exception {
PowerMockito.spy(Resource.class);
PowerMockito.doNothing().when(Resource.class, "readResources", Mockito.any(String.class));
//no exception heeeeere!
Resource.readResources("whatever");
PowerMockito.verifyStatic();
Resource.readResources("whatever");
}
}
class Resource {
public static void readResources(String someArgument) {
throw new UnsupportedOperationException("meh!");
}
}
Why go through so much trouble just so that your method does not do anything. Just calling PowerMockito.mockStatic(Resource.class) should replace all static methods in your class with default stubs which basically mean they do nothing.
Unless you do want to change the behavior of your method to actually do something just calling PowerMockito.mockStatic(Resource.class) should suffice. Ofcourse this also means all static methods in the class are stubbed which you need to consider.
If doNothing() isn't working you can hack it a bit using the PowerMockito.doAnswer(). This lets you mock into void methods that are supposed to do something, like setting values, etc. If doNothing() doesn't work, using a blank doAnswer() should work fine.
Example:
PowerMockito.doAnswer(new org.mockito.stubbing.Answer<Object>() {
#Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null; //does nothing
}
}).when(mockObject).methodYouWantToDoNothing(args);
Maybe i can't undestand your question, but i believe it's necessary specify what must do
the method, so if you don't specify thenReturn or thenThrow or whatever powerMockito doesn't know what have to do when read your real code, for example:
REAL CODE:
IPager pag;
IPagerData<List<IDeute>> dpag;
pag = new PagerImpl();
pag.setFiles(nombrefilesPaginador);
pag.setInici(1);
dpag = gptService.obtenirDeutes(idSubjecte, idEns, tipusDeute, periode, pag);
Testing real code by mockito:
IPager pag = new PagerImpl();
pag.setInici(1);
pag.setFiles(0);
when(serveiGpt.obtenirDeutes(eq(331225L),
eq(IConstantsIdentificadors.ID_ENS_BASE),
Matchers.any(ETipusDeute.class),
Matchers.any(EPeriodeDeute.class),
eq(pag)))
.thenThrow(new NullPointerException(" Null!"));
If haven't specify the return my test will be fail.
I hope it helps.
I tried doNothing with different variations but nothing worked except the below solution.
#Before
public void setUp(){
obj = new ClassObj (parameters);
//parameters should also include the class obj for which void method is available
}
I would like to verify if a parameter of a mocked object is correctly mapped (before the methodcall there is a mapping).
I've created two objects, one of which has the expected values.
I expect the false one to fail, but somehow easymock does not verify if the parameter contains the expected values. How can I fix that?
Real class does:
public SomeObject methodname() {
MyClass parameter = mapper.map(source,Dest.class);
serviceBeingMocked.doSomething(parameter); //<-- want to verify this parameter
}
Test
MyClass correct = ...;
correct.setA=a;
correct.setB=b;
expect(serviceBeingMocked(correct)).andReturn(iDontCare); //this should work
MyClass false = ...;
false.setA=eeeeeeeeeee;
false.setB=ffffffffff;
expect(serviceBeingMocked(false)).andReturn(iDontCare); // this test should fail
Thanks in advance!
Edit: Found the answer (using capture to record the paramter and test the values via assert)
Capture paramter = new Capitre();
expect(serviceBeingMocked(EasyMock.capture(parameter)));
assertEquals(parameter.getValue().getWhatever().equals(correct.getWhatever());
If I understand correctly, this test is a test foe methodname(). And you would like to test that, given a source, the correct MyClass paremeter is created by the mapper, and passed to serviceBeingMocked.doSomething().
So in fact, your unit test for methodname() actually tests the mapper.map() method. That's wrong. You should write a unit test for mapper.map() to test that. Once you've ensured that mapper.map() works as expected, you can test methodname() by mocking the mapper.map() method, and verify that the result of this mocked method is passed to the mocked service (although this test wouldn't add much value).
To be complete, if you really want to go your way and check the value passed to serviceBeingMocked(), then you should use a capture.
I'm using Mockito 1.9.0. I want mock the behaviour for a single method of a class in a JUnit test, so I have
final MyClass myClassSpy = Mockito.spy(myInstance);
Mockito.when(myClassSpy.method1()).thenReturn(myResults);
The problem is, in the second line, myClassSpy.method1() is actually getting called, resulting in an exception. The only reason I'm using mocks is so that later, whenever myClassSpy.method1() is called, the real method won't be called and the myResults object will be returned.
MyClass is an interface and myInstance is an implementation of that, if that matters.
What do I need to do to correct this spying behaviour?
Let me quote the official documentation:
Important gotcha on spying real objects!
Sometimes it's impossible to use when(Object) for stubbing spies. Example:
List list = new LinkedList();
List spy = spy(list);
// Impossible: real method is called so spy.get(0) throws IndexOutOfBoundsException (the list is yet empty)
when(spy.get(0)).thenReturn("foo");
// You have to use doReturn() for stubbing
doReturn("foo").when(spy).get(0);
In your case it goes something like:
doReturn(resultsIWant).when(myClassSpy).method1();
In my case, using Mockito 2.0, I had to change all the any() parameters to nullable() in order to stub the real call.
My case was different from the accepted answer. I was trying to mock a package-private method for an instance that did not live in that package
package common;
public class AnimalĀ {
void packageProtected();
}
package instances;
class Dog extends Animal { }
and the test classes
package common;
public abstract class AnimalTest<T extends Animal> {
#Before
setup(){
doNothing().when(getInstance()).packageProtected();
}
abstract T getInstance();
}
package instances;
class DogTest extends AnimalTest<Dog> {
Dog getInstance(){
return spy(new Dog());
}
#Test
public void myTest(){}
}
The compilation is correct, but when it tries to setup the test, it invokes the real method instead.
Declaring the method protected or public fixes the issue, tho it's not a clean solution.
The answer by Tomasz Nurkiewicz appears not to tell the whole story!
NB Mockito version: 1.10.19.
I am very much a Mockito newb, so can't explain the following behaviour: if there's an expert out there who can improve this answer, please feel free.
The method in question here, getContentStringValue, is NOT final and NOT static.
This line does call the original method getContentStringValue:
doReturn( "dummy" ).when( im ).getContentStringValue( anyInt(), isA( ScoreDoc.class ));
This line does not call the original method getContentStringValue:
doReturn( "dummy" ).when( im ).getContentStringValue( anyInt(), any( ScoreDoc.class ));
For reasons which I can't answer, using isA() causes the intended (?) "do not call method" behaviour of doReturn to fail.
Let's look at the method signatures involved here: they are both static methods of Matchers. Both are said by the Javadoc to return null, which is a little difficult to get your head around in itself. Presumably the Class object passed as the parameter is examined but the result either never calculated or discarded. Given that null can stand for any class and that you are hoping for the mocked method not to be called, couldn't the signatures of isA( ... ) and any( ... ) just return null rather than a generic parameter* <T>?
Anyway:
public static <T> T isA(java.lang.Class<T> clazz)
public static <T> T any(java.lang.Class<T> clazz)
The API documentation does not give any clue about this. It also seems to say the need for such "do not call method" behaviour is "very rare". Personally I use this technique all the time: typically I find that mocking involves a few lines which "set the scene" ... followed by calling a method which then "plays out" the scene in the mock context which you have staged... and while you are setting up the scenery and the props the last thing you want is for the actors to enter stage left and start acting their hearts out...
But this is way beyond my pay grade... I invite explanations from any passing Mockito high priests...
* is "generic parameter" the right term?
One more possible scenario which may causing issues with spies is when you're testing spring beans (with spring test framework) or some other framework that is proxing your objects during test.
Example
#Autowired
private MonitoringDocumentsRepository repository
void test(){
repository = Mockito.spy(repository)
Mockito.doReturn(docs1, docs2)
.when(repository).findMonitoringDocuments(Mockito.nullable(MonitoringDocumentSearchRequest.class));
}
In above code both Spring and Mockito will try to proxy your MonitoringDocumentsRepository object, but Spring will be first, which will cause real call of findMonitoringDocuments method. If we debug our code just after putting a spy on repository object it will look like this inside debugger:
repository = MonitoringDocumentsRepository$$EnhancerBySpringCGLIB$$MockitoMock$
#SpyBean to the rescue
If instead #Autowired annotation we use #SpyBean annotation, we will solve above problem, the SpyBean annotation will also inject repository object but it will be firstly proxied by Mockito and will look like this inside debugger
repository = MonitoringDocumentsRepository$$MockitoMock$$EnhancerBySpringCGLIB$
and here is the code:
#SpyBean
private MonitoringDocumentsRepository repository
void test(){
Mockito.doReturn(docs1, docs2)
.when(repository).findMonitoringDocuments(Mockito.nullable(MonitoringDocumentSearchRequest.class));
}
Important gotcha on spying real objects
When stubbing a method using spies , please use doReturn() family of methods.
when(Object) would result in calling the actual method that can throw exceptions.
List spy = spy(new LinkedList());
//Incorrect , spy.get() will throw IndexOutOfBoundsException
when(spy.get(0)).thenReturn("foo");
//You have to use doReturn() for stubbing
doReturn("foo").when(spy).get(0);
I've found yet another reason for spy to call the original method.
Someone had the idea to mock a final class, and found about MockMaker:
As this works differently to our current mechanism and this one has different limitations and as we want to gather experience and user feedback, this feature had to be explicitly activated to be available ; it can be done via the mockito extension mechanism by creating the file src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker containing a single line: mock-maker-inline
Source: https://github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2#mock-the-unmockable-opt-in-mocking-of-final-classesmethods
After I merged and brought that file to my machine, my tests failed.
I just had to remove the line (or the file), and spy() worked.
One way to make sure a method from a class is not called is to override the method with a dummy.
WebFormCreatorActivity activity = spy(new WebFormCreatorActivity(clientFactory) {//spy(new WebFormCreatorActivity(clientFactory));
#Override
public void select(TreeItem i) {
log.debug("SELECT");
};
});
As mentioned in some of the comments, my method was "static" (though being called on by an instance of the class)
public class A {
static void myMethod() {...}
}
A instance = spy(new A());
verify(instance).myMethod(); // still calls the original method because it's static
Work around was make an instance method or upgrade Mockito to a newer version with some config: https://stackoverflow.com/a/62860455/32453
Bit late to the party but above solutions did not work for me , so sharing my 0.02$
Mokcito version: 1.10.19
MyClass.java
private int handleAction(List<String> argList, String action)
Test.java
MyClass spy = PowerMockito.spy(new MyClass());
Following did NOT work for me (actual method was being called):
1.
doReturn(0).when(spy , "handleAction", ListUtils.EMPTY_LIST, new String());
2.
doReturn(0).when(spy , "handleAction", any(), anyString());
3.
doReturn(0).when(spy , "handleAction", null, null);
Following WORKED:
doReturn(0).when(spy , "handleAction", any(List.class), anyString());