简体   繁体   English

用于Void返回类型的方法的Junit测试用例

[英]Junit test case for method with Void return type

I am new in writing the junit test cases and need help. 我是撰写junit测试用例的新手,需要帮助。 I read about possible solutions but they are not working at this point. 我阅读了有关可能的解决方案的信息,但目前它们还不起作用。

Main class looks like below, which calls the method of ther class addResponseData(This method only sets values in session and returns nothing). 主类如下所示,它调用ther类的addResponseData方法(此方法仅在session中设置值,不返回任何内容)。 Please see the code as per below. 请参见下面的代码。

@Test
public void testPublishData_Success() throws java.lang.Exception {
    when(GetPropValues.getPropValue(PublisherConstants.ATMID)).thenReturn("ATM");
    when(GetPropValues.getPropValue(PublisherConstants.DATA_SOURCE)).thenReturn("PCE");

    ReadAndWriteFiles mockFiles = Mockito.mock(ReadAndWriteFiles.class);
    PowerMockito.whenNew(ReadAndWriteFiles.class).withNoArguments().thenReturn(mockFiles);
    Mockito.when(mockFiles.getAllFiles()).thenReturn(null);

    KafkaProducer mockProducer = Mockito.mock(KafkaProducer.class);
    PowerMockito.whenNew(ReadAndWriteFiles.class).withAnyArguments().thenReturn(mockProducer);

    producer.publishData(null, "Test", "Data1");
}

    ResponseWrapper signerResponse;
    try {
        privateClassObj.ensureDataLoaded(objSession); // Loads the required data into objSession)

        signerResponse = new ResponseWrapper(ResponseType.SUCCESS);
        signerResponse.addResponseData("signerList", objSession.getSignerList());
        signerResponse.addResponseData("additionalSignerList", objSession.getAdditionalSignerList());
    }
    catch (ServiceException err) {
        signerResponse = new ResponseWrapper(ResponseType.PARTIAL_SUCCESS);
    }

    return signerResponse;
}

TestClass: I have written junit test case as per below. TestClass:我已经按照下面编写了junit测试用例。

    @Test
public void testSuccessfulCallWithSigners() {
    List<Signer> signerList = setSignerList();
    List<Signer> additionalSignerList = setSignerList();
    when(objSession.getSignerList()).thenReturn(signerList);
    when(nsbSession.getAdditionalSignerList()).thenReturn(additionalSignerList);
    ResponseWrapper output = signerController.getUsers(request); // actual method call
    assertEquals(output, responseWrapper);
}

This test case is failing because, I always get empty signerList and additionalSignerList.(test case result is getAdditionalSignerList() method should return List) Please let me know what am I doing wrong here. 此测试用例失败,因为我总是得到空的signerList和AdditionalSignerList。(测试用例结果是getAdditionalSignerList()方法应返回List)请让我知道我在这里做错了什么。 Thanks in Advance. 提前致谢。 I am also posting my the code of setSignerList() in case if you want to see it for reference. 我还发布了setSignerList()的代码,以防您想参考。

private List<Signer> setSignerList() {
    List<Signer> signerList = new ArrayList<Signer>();
    signerList.add(primarySigner); // primarySigner is mock object.
    return signerList;
}

You have mocked objSession and also you have returned your empty list when these two methods get called by the test case. 您已经嘲笑了objSession,并且当测试用例调用了这两个方法时,您还返回了空列表。

when(....).thenReturn(....) is used to specify a condition and a return value (Stubbing) for this condition.As you have mocked it and return your desired list using stubbing, it will not return the list returned by actual call. when(....)。thenReturn(....)用于指定条件和该条件的返回值(存根)。您已经对其进行了模拟并使用存根返回了所需的列表,则不会返回实际调用返回的列表。

List<Signer> signerList = setSignerList(); // EMPTY LIST
List<Signer> additionalSignerList = setSignerList(); // EMPTY LIST
when(objSession.getSignerList()).thenReturn(signerList); //MOCK
when(nsbSession.getAdditionalSignerList()).thenReturn(additionalSignerList); // MOCK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM