模拟void对象而不传递它们

时间:2015-06-03 13:19:17

标签: java unit-testing junit mockito

我写了以下方法:

public void checkIfPlansPublishMailShouldBeSent(
        boolean allPlansPublished, String currentPhase,
        String chaseIterationCount, LoadConfig config,
        String clientFolderName, LoggerHandler logger){
    if(allPlansPublished){
        sendPlansPublishMail(
            currentPhase, chaseIterationCount, config, clientFolderName,logger);
    }else{
        System.out.println(
            "allPlansPublish is set to false, Plans Publish Mail not sent out");
    }
}

我需要为它编写一个JUnit测试。我还需要模拟sendPlansPublishMail,因为如果它运行会发送电子邮件。

我正在使用Mockito来模拟方法所属的Object(ReportSyncCheck)。

有没有办法验证是否正在调用sendPlansPublishMail而不必将模拟对象作为参数传递给checkIfPlansPublishMailShouldBeSent

也可以使用其他测试方法。因为我需要编写JUnit测试,所以我不希望将所需的每个Object作为参数传递,因为我想我的方法将具有大的方法签名。

我编写了一个JUnit测试,它将mock对象作为参数传递,我试图避免这样做:

public class checkIfPlansPublishMailShouldBeSentTest {

    ReportSyncCheck mockRSC;
    ReportSyncCheck RSCtoTest;
    LoggerHandler mockLogger;
    LoadConfig mockLoadConfig;
    boolean allPlansPublished;
    String currentPhase;
    String chaseIterationCount;
    String clientFolderName;
    @Before
    public void setUp() throws Exception {
        mockRSC = mock(ReportSyncCheck.class);
        RSCtoTest = new ReportSyncCheck();
        mockLogger = mock(LoggerHandler.class);
        mockLoadConfig = mock(LoadConfig.class);
        currentPhase = "Time Submission Chase";
        chaseIterationCount ="1";
        clientFolderName = "TestClient";

    }

    @Test
    public void testMailMethodcalledWhenAllPlansPublishedIsSetToFalse() {
        allPlansPublished =true;
        RSCtoTest.checkIfPlansPublishMailShouldBeSent(allPlansPublished, currentPhase, chaseIterationCount, mockLoadConfig, clientFolderName, mockLogger,mockRSC);
        verify(mockRSC).sendPlansPublishMail(currentPhase, chaseIterationCount, mockLoadConfig, clientFolderName, mockLogger);
    }

    @Test
    public void testMailMethodcalledWhenAllPlansPublishedIsSetToTrue() {
        allPlansPublished =false;
        RSCtoTest.checkIfPlansPublishMailShouldBeSent(allPlansPublished, currentPhase, chaseIterationCount, mockLoadConfig, clientFolderName, mockLogger,mockRSC);
        verify(mockRSC,times(0)).sendPlansPublishMail(currentPhase, chaseIterationCount, mockLoadConfig, clientFolderName, mockLogger);
    }

}

3 个答案:

答案 0 :(得分:1)

您应该检查org.mockito.Mockito.verifyorg.mockito.BDDMockito.given这是您想要的。

http://gojko.net/2009/10/23/mockito-in-six-easy-examples/http://www.javahotchocolate.com/notes/mockito.html

也是很好的例子

答案 1 :(得分:1)

您正在使用Mockito来模拟您的依赖项,以便您可以将测试集中在被测试的类上。好。

在对象中注入依赖项的最常用方法是在构造时使用它的构造函数。构建服务对象,将其所有依赖项注入到类的构造函数中:

public class MyService {
  private final MyDao dao;

  public MyService(MyDao dao){
    this.dao = dao;
  }

  public String myMethod(){
    return dao.find();
  }
}

或者,一种流行的方法是使用setter注入依赖项:

  public class MyOtherService {
  private MyDao dao;

  public void setDao(MyDao dao){
    this.dao = dao;
  }

  public String myMethod(){
    return dao.find();
  }
}

答案 2 :(得分:1)

你需要间谍来做部分行为。

public class checkIfPlansPublishMailShouldBeSentTest {


    ReportSyncCheck mockRSC;

    LoggerHandler mockLogger;
    LoadConfig mockLoadConfig;
    boolean allPlansPublished;
    String currentPhase;
    String chaseIterationCount;
    String clientFolderName;

    @Before
    public void setUp() throws Exception {
        mockRSC = spy(ReportSyncCheck.class);

        mockLogger = mock(LoggerHandler.class);
        mockLoadConfig = mock(LoadConfig.class);
        currentPhase = "Time Submission Chase";
        chaseIterationCount ="1";
        clientFolderName = "TestClient";

    }


    @Test
    public void testMailMethodcalledWhenAllPlansPublishedIsSetToFalse() {
        doNothing().when(mockRSC).sendPlansPublishMail(currentPhase, chaseIterationCount, mockLoadConfig, clientFolderName, mockLogger);
        allPlansPublished =true;
        mockRSC.checkIfPlansPublishMailShouldBeSent(allPlansPublished, currentPhase, chaseIterationCount, mockLoadConfig, clientFolderName, mockLogger,mockRSC);
        verify(mockRSC).sendPlansPublishMail(currentPhase, chaseIterationCount, mockLoadConfig, clientFolderName, mockLogger);
    }