5

I am using @RunWith(SpringRunner.class) to writing unit test case to mock the object. I am trying to mock the repository instance which is accepting request object and returning response, but in unit test case implementation I have mocked the repository using @MockBean annotation and register the it's method call using Mockito.when(respository.post(request)).thenReturn(response). But this call is returning null.

vsk.rahul
  • 433
  • 1
  • 6
  • 11
  • Try @RunWith(MockitoJUnitRunner.class), or if you need to use SpringRunner, initialise your mocks -- see https://stackoverflow.com/questions/10806345/runwithmockitojunitrunner-class-vs-mockitoannotations-initmocksthis – tgdavies May 08 '18 at 01:08
  • Have you added `@SpringBootTest` to your test? – Luay Abdulraheem May 08 '18 at 03:41
  • @tgdavies Agreed but that's a completely different story. I am sure `@RunWith(MockitoJUnitRunner.class)` will work. @Luay - @SpringBootTest is not working either. – vsk.rahul May 08 '18 at 06:40

3 Answers3

6

I faced similar situation, the problem is given parameter in Mockito.when() block may not be the same as spring generated. I'll explain my case below, hope to help you:

Product product = new Product(..);
Mockito.when(service.addProduct(product)).thenReturn(saveProduct)

When I send a request, spring generates new Project object which has same fields with product but instance is different. That is, Mockito cannot catch when statement. I changed it like below and it's worked:

Mockito.when(service.addProduct(Mockito.any())).thenReturn(savedProduct)
GalAbra
  • 5,048
  • 4
  • 23
  • 42
farukcankaya
  • 544
  • 7
  • 11
3

I figured it out. But the solution is still weird to me...

I was facing this issue because, I was instantiating the request and response in @Before annotated method... as describing below.

    @Before
public void setup() {
    Request reqA = new Request();
    reqA.set..(..);

    Response res = new Response();
    res.set..(..);

    Mockito.when(this.respository.post(reqA)).thenReturn(res);
}

@Test
public void test() {

    // Creating Request instance again with all same properties. 
    // Such that this req instance is technically similarly as instantiated in @Before annotated method (above). 
    // By, implementing the equals and hashCode method.
    Request reqB = new Request();
    reqB.set..(..);

    // Getting res as 'null' here....
    Response res = this.service.post(reqB);
}

Since, reqA and reqB are technically similar then why mocked call not returning the same response as registered.

If I moved setup() method code inside test() method every thing starts working !!!!!

vsk.rahul
  • 433
  • 1
  • 6
  • 11
  • If `Request` does not implement equals in a way that `reqA.equals( actualRequest )` returns `true`, then Mockito will not know that this "`actualRequest`" is the one you are looking for. By moving it into the test method, I assume you are then using `reqA` as the actual request, which of course works, since `reqA == reqA`, allowing Mockito to realize that this is the one you are waiting for. – Florian Schaetz May 08 '18 at 07:34
0

I had the same issue here, vsk.rahul's comment helped me a lot.
I was trying to to use a method to return a mock interaction, but not succeeded, turning it into a static method gave me the expected behavior.

Problem:
The method bar.foo() was returning null for any interaction

public void test1() {
    doReturn(mockReturn()).when(bar).foo();
}

private String mockReturn() {
    return "abc";
}

Solution:
The method bar.foo() is returning abc String for any interaction

public void test1() {
    doReturn(mockReturn()).when(bar).foo();
}

private static String mockReturn() {
    return "abc";
}