mockito- 模拟服务不 return 为真,总是 return 为假

mockito- mocked service does not return true, always returns false

下面的代码 (CallService.java) 调用名为 AService 的服务并进行一些更新,然后 returns 一个布尔值。

    public boolean getUpdateStatus() throws ServiceException {
    if (finder == null) {
        finderBean = new FinderBean();
    }
    myService = finderBean.find(AService.class);
    if (myService == null) {
        System.out.println("null >>>>>>>");
    }

    final Config config = new Config();
    final Update update = new Update();
    status = myService.update(update, config);
    System.out.println("Status: " + status);
    return status;
}

下面的测试用例验证了CallService.java;已经模拟了所有与 classes 相关的服务并调用我的 class 进行测试以调用 AService 并断言布尔值,但无论模拟如何,模拟总是 returns false。

@Before
public void setUp() throws Exception {

    myService = PowerMockito.mock(AService.class);
    finderBean = PowerMockito.mock(FinderBean.class);

    update = PowerMockito.mock(Update.class);
    config = PowerMockito.mock(Config.class);       
PowerMockito.whenNew(FinderBean.class).withNoArguments().thenReturn(finderBean);

PowerMockito.when(finderBean.find(AService.class)).thenReturn(myService);
}

@Test
public void TestUpdateState() throws Exception {

    callService = new CallService();
    MemberModifier.field(CallService.class, "finderBean").set(callService, finderBean);
    PowerMockito.when(finderBean.find(AService.class)).thenReturn(myService);
    PowerMockito.when(myService.update(update, config)).thenReturn(true);

    final boolean status = callService.getUpdateStatus();
    assertTrue(status);
}

我是不是遗漏了什么,有什么指点吗?

谢谢。

根据documentation

All usages require @RunWith(PowerMockRunner.class) and @PrepareForTest annotated at class level.

Use PowerMockito.whenNew, e.g.

whenNew(MyClass.class).withNoArguments().thenThrow(new IOException("error message"));

Note that you must prepare the class creating the new instance of MyClass for test, not the MyClass itself. E.g. if the class doing new MyClass() is called X then you'd have to do @PrepareForTest(X.class) in order for whenNew to work:

强调我的

因此请确保您拥有必要的属性并且相应地模拟了依赖关系

@RunWith(PowerMockRunner.class)
@PrepareForTest(CallService.class)
public class TestMyClass {
    FinderBean finderBean;
    AService myService;

    @Before
    public void setUp() throws Exception {        
        myService = PowerMockito.mock(AService.class);
        finderBean = PowerMockito.mock(FinderBean.class);
        PowerMockito.whenNew(FinderBean.class).withNoArguments().thenReturn(finderBean);            
    }
    
    @Test
    public void TestUpdateState() throws Exception {
        //Arrange                      
        PowerMockito.when(finderBean.find(AService.class)).thenReturn(myService);
        PowerMockito.when(myService.update(any(Update.class), any(Config.class))).thenReturn(true);
        CallService callService = new CallService(); 
    
        //Act
        final boolean status = callService.getUpdateStatus();

        //Assert
        assertTrue(status);
    }
}

它之前无法使用您的模拟服务的原因是因为测试中的参数与被测方法中的参数不同。被测方法在本地创建自己的实例,这意味着它们与测试设置中使用的实例不同,因此在调用时不会按预期运行。