Mockito when method not working - java

I am using mockito as mocking framework. I have a scenerio here, my when(abc.method()).thenReturn(value) does not return value, instead it returns null.
public class DQExecWorkflowServiceImplTest {
#InjectMocks
DQExecWorkflowServiceImpl dqExecWorkflowServiceImpl = new DQExecWorkflowServiceImpl();
#Mock
private DQUtility dqUtility;
#Mock
private DqExec dqExec;
#Mock
private DqCntlDefn dqCntlDefn;
#Mock
private DqCntlWfDefn dqCntlWfDefn;
#Mock
private DqCntlWfDefnTyp dqCntlWfDefnTyp;
#Mock
private IDQControlWfDefTypeService controlWfDefTypeService;
#Before
public void setUp() throws Exception {
dqExec = new DqExec();
dqCntlWfDefn = new DqCntlWfDefn();
dqUtility = new DQUtility();
dqCntlWfDefnTyp = new DqCntlWfDefnTyp();
dqCntlWfDefnTyp.setDqCntlWfDefnTypCd("MIN_INCLUSIVE_VAL");
dqExecWorkflowServiceImpl
.setControlWfDefTypeService(controlWfDefTypeService);
}
#Test
public void testExecuteWorkflow() {
when(controlWfDefTypeService.getDqCntlWfDefnTypCd(dqCntlWfDefn))
.thenReturn(dqCntlWfDefnTyp);
dqExecWorkflowServiceImpl.executeWorkflow(dqExec, dqCntlWfDefn);
}
}
Java class
#Override
public DqCntlWfExec executeWorkflow(final DqExec dqExec,
final DqCntlWfDefn dqCntlWfDefn) {
final DqCntlWfExec dqCntlWfExec = new DqCntlWfExec();
dqCntlWfExec.setDqCntlWfExecEffDt(dqUtil.getDefaultEffectiveDt());
dqCntlWfExec.setDqCntlWfExecExpDt(dqUtil.getDefaultExpiryDt());
dqCntlWfExec.setDqCntlWfDefn(dqCntlWfDefn);
dqCntlWfExec.setDqExec(dqExec);
final DqCntlWfDefnTyp dqCntlWfDefnTyp = controlWfDefTypeService
.getDqCntlWfDefnTypCd(dqCntlWfDefn);
String workflowType = null;
if(null!=dqCntlWfDefnTyp){
workflowType = dqCntlWfDefnTyp.getDqCntlWfDefnTypCd();
}
When ever i run the test file the when is not working and i am using mockito1.8.5 jar in the buildpath. The service call is being mocked but returns the null value.
final DqCntlWfDefnTyp dqCntlWfDefnTyp = controlWfDefTypeService
.getDqCntlWfDefnTypCd(dqCntlWfDefn);
This object dqCntlWfDefnTyp is null
I have done this before and there was no problem with the when, It seems to be working with files i have done before. I had followed the same procedure for the test file but i couldnt figure out the issue. Can anyone please assist me
Thanks to all the folks in advance

Mockito mock works when we mock the objects loosely.
Here is the change i have made to make it work:
when(controlWfDefTypeService.getDqCntlWfDefnTypCd(any(DqCntlWfDefn.class))
.thenReturn(dqCntlWfDefnTyp);
Instead of passing the object of the Mock class, I passed the class with the Matcher any() and it works.

TL;DR If some arguments in your test are null, be sure to mock the parameter call with isNull() instead of any(SomeClass.class).
Explanation
This might not be the answer that helps OP, but might be useful for someone else. In my case the setup was all good, however, some mocks returned the desired thenReturn(...) value and some didn't.
It's important to understand, that the method call you're trying to mock (i.e. the method in when(someMock.methodToMock)) has to match the actual call and not the signature only.
In my case, I mocked a method with a signature:
public void SomeValue method(String string, SomeParam param)
The call however, in the test was something like:
method("some string during test", null);
Now if you mock the call with:
when(MockedClass.method(anyString(), any(SomeParam.class))
Mockito will not match it even though the signature is correct. The problem is that Mockito is looking for a call of method() with the arguments String and SomeParam, whereas the actual call was with a String and null. What you have to do is:
when(MockedClass.method(anyString(), isNull())
Hint
Since there are many isNull() implementations in different frameworks, be sure to use this one org.mockito.ArgumentMatchers.isNull.

I think I have found your issue, but not all the credit goes to me.
Since you are trying to mock 'dqCntlWfDefnTyp' in your test class and the object itself is being instantiated in the class that you are trying to test, you inevitably run into some issues. The primary problem is that the object cannot be mocked because it is being recreated in during the test.
There are a few options, but the best choice in my humble opinion is using PowerMockito. You will be able to replace the object within the class that is being tested with the one you mock.
An excellent example of this usage of PowerMockito from #raspacorp on this question:
public class MyClass {
void method1{
MyObject obj1=new MyObject();
obj1.method1();
}
}
And the test class...
#RunWith(PowerMockRunner.class)
#PrepareForTest(MyClass.class)
public class MyClassTest {
#Test
public void testMethod1() {
MyObject myObjectMock = mock(MyObject.class);
when(myObjectMock.method1()).thenReturn(<whatever you want to return>);
PowerMockito.whenNew(MyObject.class).withNoArguments().thenReturn(myObjectMock);
MyClass objectTested = new MyClass();
objectTested.method1();
... // your assertions or verification here
}
}

I had the same problem. The solution for me was to put the Mockito.when(...).thenReturn(...); into the #Before-SetUp method.

Just like Younes EO said: it usually is related to null arguments being passed to mocked functions.
One thing worth adding is that if you're using Kotlin for your tests (and you have mockito-kotlin in your project), for Nullable arguments you should usually use anyOrNull() instead of any().

Relevant to the topic, just in case this helps someone. My mockito.when wasn't working when asserting anyString() reason being the String within the tested class was an injected value from a properties file. The following line set the injected value in the unit test:
ReflectionTestUtils.setField(bean, "theVariable", "theValue");

I haved the same problem when i make test for controler. Use #MockBean instead #Mock

Related

Mockito 2.19.0 - Null returned upon static method being used in class method under test, not testable with Mockito?

I'm fairly new to Mockito having gone through their official documentation and a couple of online tutorials in regards to writing tests, as such there is probably something I am missing when I am getting a null returned from a static method I am calling inside a method of a service class under test. I am using Mockito 2.19.0 with assertJ 3.10.0 and the method under test is this:
public Fruit saveFruit(Fruit fruit) {
final String name = fruit.getName().toLowerCase();
Optional<Fruit> fruitOptional = fruitRepository.findOneByName(name);
if (!fruitOptional.isPresent()) {
Fruit newFruit = userDeviceRepository.save(Builder.buildFruitNew(fruit));
LOG.info("saveFruit\t\t->\Fruit saved, {}", newFruit);
return newFruit;
} else {
LOG.info("saveFruit\t\t->\tFruit already exists, updating existing fruit");
Fruit updatedFruit = fruitOptional.map(Builder::buildFruitGet).get();
updatedFruit.setFruitType(fruit.getFruitType());
return fruitRepository.save(updatedFruit);
}
}
The test method I have been trying to write:
#ExtendWith(MockitoExtension.class)
class UserDeviceServiceTest {
#Mock
private FruitRepository fruitRepository;
#InjectMocks
private FruitServiceImpl fruitServiceImpl;
#Test
public void whenSavingNewFruitItShouldReturnTheSavedFruit() {
Fruit newFruit = new Fruit("Lemon", "Yellow");
// Given that a new fruit is saved and returns the new fruit
given(fruitRepository.save(newFruit)).willReturn(newFruit);
// When saving a new fruit
assertThat(fruitServiceImpl.saveFruit(newFruit))
// Then it should return the new fruit
.isSameAs(newFruit);
}
}
When running the test I achieve the following fail message:
java.lang.AssertionError:
Expecting:
<com.fruits.domain.models.Fruit#3bf20acd>
and actual:
<null>
to refer to the same object
EDIT: Added the method I call in the Builder class:
public static Fruit buildFruitNew(Fruit fruit) {
return new Fruit(fruit.getId(), fruit.getType());
}
It seems to me the issue lies with the line where I call the Builder class which contains static methods that return a new instantiated fruit object having had a look at the standard output to see the logger line afterwards which sure enough reports a null object. I have had a read around on here and there was mention PowerMock and JMockit might be needed to test such as class so I assume at the moment it might not possible to test this method because of the static method it uses inside? I know for a fact having already written unit tests for the Builder class that the method used here does return a fruit object with the details of that fruit object passed in so I expected it to run the method without an issue however it seems it doesn't run the method at all. If this is indeed currently not testable code then I imagine if I replaced this static method with a non static method then I would be good to go?
Thanks now.

Mockito says "Wanted but not invoked" but debugging test jumps into that method

I have to deal with a legacy application that has no tests. So before I begin refactoring I want to make sure everything works as it is.
Now imagine the following situation:
public SomeObject doSomething(final OtherObject x, final String something) {
if(x != null) {
final String = someOtherMethod(x, something);
}
}
protected String someOtherMethod(final OtherObject x, final String something) {
....
}
Now I want to make sure that protected method is called as well
So I did this
#InjectMocks // Yes there is more going on here
private MyTestObject myTestObject;
private MyTestObject spy;
private static final OtherObject VALID_OTHER_OBJECT = new OtherObject();
#Before
public void setup() {
this.spy = Mockito.spy(myTestObject);
}
#Test
public void ifOtherObjectIsNotNullExpectSubMethodToBeCalled() {
myTestObject.doSomething(VALID_OTHER_OBJECT, null);
verify(spy).someOtherMethod(VALID_OTHER_OBJECT, null);
}
I get a failing test and "Wanted but not invoked..." for someOtherMethod().
I jumped right into debug mode and checked. That method is called!
What could be the cause of this? Am I misusing the spy here?
Edit: I want to stretch that I know this is not what you typically test for, especially since someOtherMethod(...) has a non-void return-value here. But imagine the return value was void...
Basically I just want to understand why the spy fails here.
As per its Javadoc, Mockito.spy(object) creates a copy of the passed in object. Calling methods on the original passed in object then does not register on the spy, because the spy instance is not the same object.
Change myTestObject.doSomething(...) to spy.doSomething(...) and it should work.
Alternative (different way to achieve the same thing):
Consider using the #Spy annotation on your myTestObject.
Be sure to add MockitoAnnotations.initMocks(this); to your initialization method (in your junit test).
The #Before and #Mock annotations are useful as well.
I had one object creating another and that other object making calls. So I needed to make that internal object be using the spied reference instead. I used reflection and updated the reference using Whitebox.
TestAddressFragment fragment = spy(new TestAddressFragment());
AddressPresenter presenter = fragment.getPresenter();
Whitebox.setInternalState(presenter, "view", fragment );
Now my fragment could check if its method was called.
verify( fragment ).showAddress(any(), anyBoolean());

Mocking - zero interactions

I have written a test to verify that a public method in my class is called. My test is failing as follows. Can anyone tell me why this happening?
Wanted but not invoked: `mockMyClass.runThis();
Actually, there were zero interactions with this mock.
class MyClass{
public void myMethod(){
runThis("hello");
}
}
public void runThis(String str){
return;
}
Test class
#Mock
MyClass mockMyClass;
MyClass myClass = new MyClass();
#Test
public void test(){
myClass.myMethod();
verify(mockMyClass).runThis(anyString());
}
You're not invoking against your mock, but rather the real class. You need to generate a mock, and then invoke on that mock. Try something like:
MyClass mock = mock(MyClass.class); // I'm using the methods rather than annotation here
mock.myMethod();
and assert on that. I'm not quite sure in the above where runThis() is, though. If it's on a contained class, then that's the thing to need to mock. If it's on the same class, then you can't use a mock per se, since the mock substitutes all functionality, and perhaps the Mockito spy() mechanism would be of use here. Section 13 of the doc advises more on this.

How to stub external method properly? NullPointer on mocking statements

No matter what I've tried, test end up on NullPointerException on stubbing statement.
Method to test:
public boolean selectEntity(final List<T> entities) {
if (entities.contains(helper.getLastEntity())) {
return true;
}
}
Above snippet is enough, cause (in unit test) cannot even enter into conditional statement. To clarify: getLastEntity returns field
private T lastEntity
for object of class Helper. T is an interface.
My best try is:
private #Mock Helper helper;
private #Mock List<T> entities;
...
#Test
public void testSelectEntity(){
when(entities.contains(notNull(T.class))).thenReturn(true);
when(helper.getLastEntity()).thenReturn((T) anyObject());
}
How to proceed here?
UPDATE: follow your suggestions, I rewrote test (mocks are for sure initialized this time:))
final DummyT dummyT = new DummyT();
when(helper.getLastEntity()).thenReturn(dummyT);
when(entities.contains(dummyT).thenReturn(true);
assertTrue(objectUnderTest.selectEntity(entities));
where DummyT implements T. Got null pointer on method execution, pointing on if statement.
At least two problems:
You cannot return an instance of the matcher anyObject(). Instantiate an object to return instead.
Depending on your mocking framework, you'll need to initialise mocking first. For example, in Mockito, use: MockitoAnnotations.initMocks(this);

Unit-testing with mockito (partial mocking)

I have a problem with Mockito.
Is it possible to do such a thing:
ClassX x = mock(ClassX.class)
when(x.methodB()).thenReturn("toto");
String result = x.methodA();
I'm working with Mockito 1.7.
I saw there was a "spy" system but they say it's not recommended to use it (why?) on the item we test...
I tried that spy function anyway but i get a strange behaviour.
Check what i want to do:
Real code:
String methodA(String arg) {
return this.methodB(arg);
}
String methodB(String arg) {
return "toto";
}
Test code:
#Test
public void testTest() {
final ClassX x = spy( new ClassX() );
final String argument = "arg";
doReturn("good").when(helper).methodB(argument);
assertTrue( x.methodB(argument).equals("good") );
assertTrue( x.methodA(argument).equals("good") );
}
As they said i avoided the when thenReturn syntax that could be a problem with a spy (but it doesn't work either anyway)
The strange thing is that:
assertTrue( x.methodB(argument).equals("good") );
is OK
Only the second
assertTrue( x.methodA(argument).equals("good") );
is not OK
Actually helper.methodA(argument) returns "toto" -> the real result and not the mock result
It isn't possible to tell mockito to return "good" in this case??? It seems when the test class call methodB it's ok, but if a method of the spy calls the methodB it doesn't work anymore...
I don't know what to do... is it such a strange thing to unit-test 2 methods of the same class and make the tests independent from each others so that a famous mock-test framework doesn't implement this basic function? Isn't it what we just call real unit-testing? Don't understand why they say to avoid using spy method on the tested object...
Thanks
UPDATE:
I wrote the stuff below, and then moments later discovered .thenCallRealMethod() which allows you to effectively perform partially stubbing. Mockito authors recommend you use refactoring to separate dependencies into different classes; but they do provide the means to partially stub. I have added a test method to demonstrate this approach, and leave my original comments.
ORIGINAL:
I really like Mockito, but this is the one place where EasyMock wins out. I have two solutions for you that don't involve Mockito. The first is to override methodB on your testing instance. The other is to partially mock with EasyMock:
import org.junit.Test;
import static org.junit.Assert.*;
import static org.easymock.EasyMock.*;
public class PartialMockTest {
class ClassX {
String methodA(String arg) {return methodB(arg);}
String methodB(String arg) {return "toto";}
}
#Test
public void MockitoOnClassX(){
ClassX classx = mock(ClassX.class);
when(classx.methodB("hiyas")).thenReturn("tomtom");
when(classx.methodA(anyString())).thenCallRealMethod();
String response = classx.methodA("hiyas");
assertEquals("tomtom",response);
}
#Test
public void OverrideOnClassX() {
ClassX classx = new ClassX(){#Override String methodB(String arg){return "tomtom";}};
String response = classx.methodA("hiyas");
assertEquals("tomtom",response);
}
#Test
public void PartialMockOnClassX() throws NoSuchMethodException {
ClassX classx = createMockBuilder(ClassX.class).addMockedMethod("methodB").createMock();
expect(classx.methodA("hiyas")).andReturn("tomtom");
replay(classx);
String response = classx.methodA("hiyas");
assertEquals("tomtom",response);
}
}
The spy is a different object from the spied object. The spy only delegates to the spied object. So when the spied object calls methodB from methodA, it will call it on itself, not on the spy.

Categories