在Junit测试中如何覆盖捕获块?

时间:2018-09-21 07:42:31

标签: java unit-testing mockito junit4

我正在尝试覆盖100%的测试课程。但是,由于无法测试catch块,我最多只能做到90%。谁能告诉我如何遮盖挡块。

下面是我要测试的课程:

public class UnsubscribeXmlTransformer {

    public Map<String, Object> process(Map<String, Object> requestMap) {

        String inputStream = requestMap.get(NeoConstants.INPUTSTREAM).toString();

        Smooks smooks;
        try {
            smooks = new Smooks("Unsubscribe-smooks.xml");

            JavaResult javaResult = new JavaResult();


            smooks.filterSource(new StringSource(inputStream), javaResult);


            UnsubscribeDetailsVO unsubscribeDetailsVO = (UnsubscribeDetailsVO) javaResult
                    .getBean("unsubscribeDetailsVO");

            requestMap.put("unsubscribeDetailsVO", unsubscribeDetailsVO);

        } catch (IOException | SAXException e) {        }

        return requestMap;
    }

}

这是我的测试班:

public class UnsubscribeXmlTransformerTest {
    Map<String, Object> requestMap = null;  

    @Before
    public void prepareRequestMap() {
        requestMap = new HashMap<>();
        String inputStream = "some xml tags";

        requestMap.put(NeoConstants.INPUTSTREAM, inputStream);      
    }


    @Test
    public void processTest() throws Exception {
        UnsubscribeXmlTransformer ref = new UnsubscribeXmlTransformer();
        Map<String, Object> result = ref.process(requestMap);

        Assert.assertNotNull("Result is not null", result);
        Assert.assertFalse("Result is not empty", result.isEmpty());
        Assert.assertTrue("Result contains key named inputStream", result.containsKey(NeoConstants.INPUTSTREAM));
        Assert.assertTrue("Result contains key named unsubscribeDetailsVO", result.containsKey("unsubscribeDetailsVO"));
        Assert.assertTrue("Value with key unsubscribeDetailsVO is an instance of UnsubscribeDetailsVO", result.get("unsubscribeDetailsVO") instanceof UnsubscribeDetailsVO);
    }

    @Test(expected = IOException.class)
    public void processTestForException() {
        UnsubscribeXmlTransformer ref = new UnsubscribeXmlTransformer();

        // how to cover                 
    }
}

2 个答案:

答案 0 :(得分:3)

您可以在requestMap上使用Mockito.spy,并在需要抛出异常的情况下使用模拟案例。然后(由jannis指出),您可以使用AssertJ的fluent API来验证是否没有抛出异常(您正在catch子句中对其进行静音),例如

@Test
public void process_shouldCatchException() throws Exception{
    UnsubscribeXmlTransformer sut = new UnsubscribeXmlTransformer();
    Map<String, Object> requestMap = Mockito.spy(new HashMap<>());
    when(requestMap.put(eq("unsubscribeDetailsVO"), any()))
        .thenThrow(IOException.class);
    assertThatCode(() -> sut.process(requestMap))
        .doesNotThrowAnyException();
}

答案 1 :(得分:0)

在这里,当我们执行classUnderTest.handleError(clienthttpresponse);它将在Java类..中引发异常,以便覆盖java类中catch块中的代码,我们需要在try块和Assert.fail()中编写以下代码,然后与其立即抛出错误,不如立即抛出该错误

try {
            Mockito.when(clienthttpresponse.getBody()).thenReturn(targetStream);
            classUnderTest.handleError(clienthttpresponse);
            fail();
        }catch(Exception e) {
            System.out.println(e.getClass()+""+e.getMessage());
        }