Ensure non-mocked methods are not called in mockito - java

In the following example:
Execution execution = mock(Execution.class);
when(execution.getLastQty()).thenReturn(1000.0);
when(execution.getLastPrice()).thenReturn(75.0);
order.onFillReceived(execution);
assertEquals(0, order.getLeavesQty(), 0);
Execution has many other methods that should not be called. Only the two methods that have been mocked should be used within this test and should be called. If any other methods are called, then the test should fail.
How to I tell Mockito to fail if any other methods are called?

The documentation covers this explicitly. You want to call verifyNoMoreInteractions, either after calling verify (as per the docs) or
verify(execution).getLastQty();
verify(execution).getLastPrice();
verifyNoMoreInteractions(execution);
or using ignoreStubs:
verifyNoMoreInteractions(ignoreStubs(execution));

You could try the never method if that fits the use case:
i.e.
verify(execution, never()).someOtherMethod();

Related

How do I manipulate an argument with mockito

I am using junit to run a few unit tests. One of these calls a method in an object that I mock using mockito like;
#Mock
private MyClass myClass;
I then set up mockito to do something like
Mockito.when(myClass.foo(Mockito.any()).thenReturn(bar);
Now myClass.foo actually takes another one of my classes (say class Person) as an argument and what I would like to do is something like this
Mockito.when(myClass.foo(Person parson)).thenDo(person.setName("Name")).thenReturn(bar);
That is of course pseudo code but I hope it illustrates what I am trying to do. Is this possible?
You need to use thenAnswer or its twin doAnswer method.
See Mockito : doAnswer Vs thenReturn
You should use thenReturn or doReturn when you know the return value at the time you mock a method call. This defined value is returned when you invoke the mocked method.
Answer is used when you need to do additional actions when a mocked method is invoked, e.g. when you need to compute the return value based on the parameters of this method call.
If your answers become too complicated, consider using a fake instead of a mock.
In this case if your goal is to set field of Person object you can do that before or after the line:
Mockito.when(myClass.foo(Mockito.any()).thenReturn(bar);
doAnswer() would help do operations based on input, but operations would be performed on copy of arguments not the original arguments.

Mockito.doAnswer vs ArgumentCaptor in Unit Tests

I am new in Unit Testing in Java and tried to test a void method using ArgumentCaptor. On the other hand, I have seen that there is another approach called Mockito.doAnswer that can also be used for testing a void method.
In this scene:
1. What are the main purposes of Mockito.doAnswer and ArgumentCaptor?
2. What are the differences between Mockito.doAnswer and ArgumentCaptor and pros/cons of them?
They're completely different things.
DoAnswer is for stubbing. For setting up "fake" behaviour that you want to have occurring when a particular method is called.
ArgumentCaptor is for verification. You use it if you want to check what arguments were passed to a method, during the running of a test.

Mockito - wanted but not invoked - interface.method()

I am running a few tests right now in which I mock one of my interfaces like this:
interface = mock(InterfaceView.class);
I then try to verify one of my methods off of this interface but continually get an error:
verify(interface).someMethod(objList);
The error I get here is Wanted but not invoked: interface.someMethod(...)
, Actually, there were zero interactions with this mock.
mockito is actually telling you that your expectation was not met. You implemented your test to expect a call to that method (that's what verify does, unless you parametrize it to verify that never gets called), but your tested instance does not call it with the given parameters.
verify(interface).someMethod(objList) It's verification that someMethod was invoke exactly one time.
So if there is no interaction, then you shouldn't use this method :D
Note:
If You want to verify that object didn't have any interaction, then use:
Mockito.verifyZeroInteractions(obj)

Anyway to create a Test Helper class that creates mocks?

In my testing of a legacy system, I have found myself having to mock similar classes for different parts of the system I'm increasing test coverage for. I would like to create a helper class for my tests that could call a method that would set up particular mocks and their return values.
Here's an example of a method I have had to create a few times in multiple tests.
public void mockClassINeedToMockToReturn(Boolean bool){
mockStatic(classINeedToMock.class);
when(classINeedToMock.getSuccess(Mockito.any(someClass.class))).thenReturn(bool);
}
I have tried setting this up in a HelperTest.class (in the same project/folder as my tests) and it isn't working. I have included both the following Annotations in the class.
#RunWith(PowerMockRunner.class)
#PrepareForTest({classINeedToMock.class})
I have tried:
Using the methods statically and calling them in my tests. (does not mock)
Creating a HelperTest object and calling the method in my test. (still does not mock)
Having my tests Extend my HelperTest.class and calling the method from in my tests. (Still refuses to mock the class)
I'm sure this is something to do with the way the PowerMockRunner works. So is there a way to make this work? Or should I just come to terms with duplicating these methods throughout my project.
It's the little details.... When looking into it more I noticed the class I was mocking had 2 separate methods.
theClass.method(var1, var2);
theClass.method(var1, List<var2>);
One of my tests was calling the first method, the other was calling the second method. When I ran the second test (only having mocked the first method), it was calling the actual class because that method was not mocked.
After setting up the correct mock with the proper input parameters I could call the method statically and the mock would be created and used appropriately.

Easy mock behaviour while requested

I was thinking, is it possible to mock whole object behavior with EasyMock, but in a way that once declared mock with all expected values and results is used several times without caring about the order of the requests ?
The purpose for this is to create an instance of mock for example in JUnit test #BeforeClass and use it in several #Test methods.
Thank you in advance for any input,
Regards,
P.
If you are not interested in verifying calls to the mock, and your only aim is to ensure that whenever a specific method on the mock is called, it will always return the same desired result, you can configure it using andStubReturn(), e.g.
expect(mock.getMeaningOfLifeUniverseAndEverything()).andStubReturn(42);
I think you're really wanting two things:
The ability to use mocked methods out of order, which is the default easymock (non-strict mode),
The ability to use mocked methods any number of times.
You do the latter like this:
expect(someMock.someMethod()).anyTimes().andReturn(someValue);
If your method will get different arguments each time it is called, you can use the anyObject() method to ignore the provided argument.

Categories