Jmockit在同一方法中激活和停用MockUp对象

时间:2018-11-15 11:51:21

标签: java unit-testing testng jmockit

我正在为客户可以将内容上载到数据库的场景编写测试用例。在这个测试用例中,我假设内容丢失了,因此将抛出RuntimeException(PAYLOAD_NOT_FOUND),在catch块中,我将构造虚拟内容并继续执行另一条执行路径。

要模拟缺少的内容,我在模拟getContent getter

        new MockUp<CustomerContentDto>() {
            @Mock
            public String getContent() {
                throw new RuntimeException(PAYLOAD_NOT_FOUND);
            }
        };

到目前为止,在构造伪内容之后我需要调用public String getContent()的问题,但是每次我调用此方法时,模拟都会抛出new RuntimeException(PAYLOAD_NOT_FOUND); 在某些执行点之后,有什么方法可以停用该模拟程序?

 @Test
    public void uploadContent_PayloadMissing_uploadError() throws DaoException, URISyntaxException, IOException,MessageTaskException {

    final String sameAccounts = "some info...";

    new MockUp<CustomerContentDto>() {
        @Mock
        public String getContent() {
            throw new RuntimeException(PAYLOAD_NOT_FOUND);
        }
    };

    // i need to mock getContent() befor this call
    objectUnderTest.handleMessage(111,sxpConnection);

    new Verifications() {{
        CustomerContentDto customerContentDto;
        iCustomerContentDao.setApproved(anyLong, anyString, null);
        minTimes = 1;
        maxTimes = 1;
        iCustomerContentDao.uploadContent(customerContentDto = withCapture());
        maxTimes = 1;

        // i need to deactivate the mock getContent() befor this call
        Assert.assertEquals(customerContentDto.getContent(), "111\n\n\"Content is missing a Payload tag\"");
    }};
}

0 个答案:

没有答案