mercredi 3 juin 2015

Mocking void objects without parsing them

I have written the following method:

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

I need to write a Junit test for it, I also need to mock the sendPlansPublishMail as that would send an email if it is run.

Im using mockitio to mock the Object the method belongs to (ReportSyncCheck)

Is there a way to verify that sendPlansPublishMail is being called without having to parse a mock object into the checkIfPlansPublishMailShouldBeSent as a parameter.

Open to other methods of testing to. As I need to write of Junit tests I dont want to have to pass in every Object I need as a parameter as I imaging my methods will have large method signatures.

I have written a Junit test that does pass the mock object as a parameter which Im trying to avoid.

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

}

Thanks for your consideration

Aucun commentaire:

Enregistrer un commentaire