简体   繁体   中英

Mockito - thenReturn always returns null object

I'm trying to implement Mockito to test a particular method but the.thenReturn(...) seems to always be returning a null object instead of what I intended:

CUT:

public class TestClassFacade {

  // injected via Spring
  private InterfaceBP bpService;

  public void setBpService(InterfaceBP bpService) {

      this.bpService = bpService;
  }

  public TestVO getTestData(String testString) throws Exception {

    BPRequestVO bpRequestVO = new BPRequestVO();

    bpRequestVO.setGroupNumber(testString) ;
    bpRequestVO.setProductType("ALL") ;           
    bpRequestVO.setProfileType("Required - TEST") ;

    IBPServiceResponse serviceResponse = bpService.getProduct(bpRequestVO);  //PROBLEM

    if (serviceResponse.getMessage().equalsIgnoreCase("BOB")) {

        throw new Exception();

    } else {

        TestVO testVO = new TestVO();
    }

    return testVO;
  }

}

Spring Configuration:

<bean id="testClass" class="com.foo.TestClassFacade">

   <property name="bpService" ref="bpService" />

</bean>

<bean id="bpService" class="class.cloud.BPService" />

Mockito Test Method:

@RunWith(MockitoJUnitRunner.class)
public class BaseTest {

    @Mock BPService mockBPService;
    @InjectMocks TestClassFacade mockTestClassFacade;

    private String testString = null;
    private BPRequestVO someBPRequestVO = new BPRequestVO();
    private IBPServiceResponse invalidServiceResponse = new BPServiceResponse();

    @Test (expected = Exception.class)
    public void getBPData_bobStatusCode_shouldThrowException() throws Exception {

        invalidServiceResponse.setMessage("BOB");

        someBPRequestVO.setGroupNumber(null);
        someBPRequestVO.setProductType("ALL");
        someBPRequestVO.setProfileType("Required - TEST");

        System.out.println("1: " + someBPRequestVO.getGroupNumber());
        System.out.println("2: " + someBPRequestVO.getProductType());
        System.out.println("3: " + someBPRequestVO.getProfileType());
        System.out.println("4: " + someBPRequestVO.getEffectiveDate());

        when(mockBPService.getProduct(someBPRequestVO)).thenReturn(invalidServiceResponse);

        mockTestClassFacade.getTestData(testString);

        verify(mockBPService).getProduct(someBPRequestVO);
    }
}

System output:

1: null
2: ALL
3: Required - TEST
4: null

What's happening here is that when I run the test the serviceResponse object is null on the line in the CUT marked with //PROBLEM above. My desire is to have that object be populated with my "invalidServiceResponse" object from my test method. Judging from the output of my System.out.println's it appears that my bpRequestVO matches my someBPRequestVO in content.

Could some one show me what I'm missing here?

Thanks for your time!

The instance of BPRequestVO that you use with when() is different than the one used in getTestData() .
Unless you override equals() , they will not match.

You should not need to write a custom Matcher if you override equals(). Note the following from the Mockito documentation :

"Custom argument matchers can make the test less readable. Sometimes it's better to implement equals() for arguments that are passed to mocks (Mockito naturally uses equals() for argument matching). This can make the test cleaner."

您可以使用“any(YourObject.class)”创建一个模拟参数,而不是在您的 BPRequestVO 类中创建一个 equals 方法,如下所示:

when(mockBPService.getProduct(any(BPRequestVO.class))).thenReturn(invalidServiceResponse);

The problem is in your usage of when() .

You submit a reference to a constructed instance; as a result, the mocking will return what you want only if the argument passed to the method is the same reference .

What you want is an argument matcher; something like:

when(mockBPService.getProduct(argThatMatches(someBPRequestVO))
    .thenReturn(whatYouWant);

Of course, it requires that you write the argument matcher!

Note that there is a builtin matcher which can do what you want:

when(mockBPService.getProduct(eq(someBPRequestVO))).thenReturn(whatYouWant);

This matcher of course requires that your BPRequestVO class implements equals() (and hashCode() too)!

我的问题是模拟服务被定义为最终的。

The BPRequestVO Object instance used for mocking is different than Instance used while junit execution.

The best way is to configure any instance of the object while mocking

when(mockBPService.getProduct(someBPRequestVO)).thenReturn(invalidServiceResponse);

Can be updated with

when(mockBPService.getProduct(Mockito.any(BPRequestVO.class))).thenReturn(invalidServiceResponse);

My issue was passing null as method arguments doesn't match the when() clause I set up.

eg

Car car = mock(Car.class)
when(car.start(anyString()).thenReturn("vroom");
assertEquals("vroom", car.start(null));

This would fail.

assertEquals("vroom", car.start("Now"));

This passes.

My issue was with the instance of the service which is autowired/mockbean had different instance at the Test->given() part and whi lein the execution it had different instance.

This was found by running the test in debug mode and checking each value in the mock stub and execution code. If all the parameters and the mocked instance are same then only the thenReturn() will return the expected value.

In myscenario the mocked instance of the class had multiple implementations and by adding @Qualifier("name") the instance became same in the given() and real execution.

it may also happened in multi-thread case. the mocked object's handler been reset by mockito after the return of @Test method while somewhere(another Thread) still using the mocked object.

as for the situation Thread provided by a pool, you can mock a thread pool instead, executing the Runner.run() in current Thread is proved effective.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM