Java 如何用Spy模拟部分方法

Java 如何用Spy模拟部分方法,java,unit-testing,spring-boot,mockito,junit4,Java,Unit Testing,Spring Boot,Mockito,Junit4,我正在我的项目中进行单元测试 我编写了一个测试脚本,用spy测试一个类,用“Mockito.doReturn”模拟一个方法。但我嘲笑的那个方法似乎不起作用 在我的测试用例中,我模拟syncLogs()并将返回值设置为“false”。因此,不应执行“syncLogs()”,并且返回值必须为“false” 实际上,我模拟的syncLogs()仍然被执行,返回值为“true”。我不知道为什么mock在我的测试中不起作用 有人知道或遇到过这个问题吗 class ApplicationTest{ pub

我正在我的项目中进行单元测试

我编写了一个测试脚本,用spy测试一个类,用“Mockito.doReturn”模拟一个方法。但我嘲笑的那个方法似乎不起作用

在我的测试用例中,我模拟syncLogs()并将返回值设置为“false”。因此,不应执行“syncLogs()”,并且返回值必须为“false”

实际上,我模拟的syncLogs()仍然被执行,返回值为“true”。我不知道为什么mock在我的测试中不起作用

有人知道或遇到过这个问题吗

class ApplicationTest{

public void testCase2() {
    setArgs(new String[] {"2018-04-23 00:08:00", "2018-04-23 00:10:00", "" });
    String startTime = "2018-04-23 00:08:00";
    String endTime = "2018-04-23 00:10:00";
    StringBuilder message = new StringBuilder();
    ApplicationCmdLine spy = Mockito.spy(ApplicationCmdLine.class);

    Mockito.doReturn(false).when(spy).syncLogs(startTime, endTime, message);
    TestCase.assertEquals(true, spy.manualSyncProess(getArgs()));
  }
}

class ApplicationCmdLine {

public boolean manualSyncProess(String[] args){...
   String information;
   if(!syncLog(.....))
      printUsageMessage(information);
}
public void printUsageMessage(StringBuilder message){
   System.out.println(message);
}
public boolean syncLogs(String startTime, String endTime, StringBuilder message) {....}

}

感谢您的帮助:)

此外,我还必须在测试中尝试“mock”,但无法执行void方法“printusegemessage()”。在我的测试中,我希望不能执行syncLog(),应该运行其他方法,但它在我的代码中不起作用。在你的方法流中,我看不出是否从
manualSyncProcess
@DhawalKapil调用了
syncLogs
,谢谢你的回复抱歉,我没有在这里放置完整的代码。但该过程将是manualSyncProcess-->syncLog-->printUsageMessage。为什么我选择mock syncLog(),因为此方法将对数据库进行传输。我只是想测试一下这个过程是否正常