Mockito - thenReturn总是返回null对象

ris*_*ide 16 java null mockito

我正在尝试实现Mockito来测试一个特定的方法,但.thenReturn(...)似乎总是返回一个null对象,而不是我想要的:

切:

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;
  }

}
Run Code Online (Sandbox Code Playgroud)

弹簧配置:

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

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

</bean>

<bean id="bpService" class="class.cloud.BPService" />
Run Code Online (Sandbox Code Playgroud)

Mockito测试方法:

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

系统输出:

1: null
2: ALL
3: Required - TEST
4: null
Run Code Online (Sandbox Code Playgroud)

这里发生的是当我运行测试时,serviceResponse对象在CUT标记为// PROBLEM的行上为空.我的愿望是用我的测试方法中的"invalidServiceResponse"对象填充该对象.从我的System.out.println的输出来看,我的bpRequestVO在内容中与我的someBPRequestVO匹配.

有人能告诉我这里我缺少什么吗?

谢谢你的时间!

小智 9

您使用的BPRequestVO实例与使用的实例when()不同getTestData().
除非你覆盖equals(),否则它们将不匹配.

如果覆盖equals(),则不需要编写自定义Matcher.请注意Mockito文档中的以下内容:

"自定义参数匹配器可以使测试的可读性降低.有时最好为传递给模拟的参数实现equals()(Mockito自然地使用equals()进行参数匹配).这可以使测试变得更清晰."


fge*_*fge 7

问题出在你的使用上when().

您提交对构造实例的引用; 因此,只有传递给方法的参数是相同的引用时,模拟才会返回您想要的内容.

你想要的是一个论证匹配器; 就像是:

when(mockBPService.getProduct(argThatMatches(someBPRequestVO))
    .thenReturn(whatYouWant);
Run Code Online (Sandbox Code Playgroud)

当然,它要求你写出参数匹配器!

请注意,有一个内置匹配器可以做你想要的:

when(mockBPService.getProduct(eq(someBPRequestVO))).thenReturn(whatYouWant);
Run Code Online (Sandbox Code Playgroud)

这个匹配器当然要求你的BPRequestVO类实现equals()(hashCode()也是)!


小智 6

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


Vic*_*doy 5

除了可以在BPRequestVO类中创建equals方法之外,还可以使用“ any(YourObject.class)”创建一个模拟参数,如下所示:

when(mockBPService.getProduct(any(BPRequestVO.class))).thenReturn(invalidServiceResponse);
Run Code Online (Sandbox Code Playgroud)

  • any(YourObject.class) 就是答案。不知道为什么其他帖子比自己聪明。点赞 (3认同)