代码之家  ›  专栏  ›  技术社区  ›  Melad Basilius

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

  •  0
  • Melad Basilius  · 技术社区  · 5 年前

    我正在为一个场景编写一个测试用例,在这个场景中,客户可以将内容上传到数据库中。在这个测试用例中,我假设内容缺失,所以 RuntimeException(PAYLOAD_NOT_FOUND) 将被扔进 catch 块I将构造虚拟内容并继续另一个执行路径。

    为了模拟丢失的内容,我正在模拟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 回复  |  直到 5 年前