View on GitHub

软件开发与教练

blog

下面代码你们看看奇怪么?
//实现
    public String get() {
        try (InputStream in = context.getResourceAsStream(MANIFEST)) {
            Manifest manifest = new Manifest(in);
            return manifest.getMainAttributes().getValue("something");
        } catch (IOException e) {
            throw new RuntimeException("Not able to read the file: " + MANIFEST);
       }

    @Test(expected = RuntimeException.class)
    public void testHandleIOException() throws Exception {
        InputStream inputStream = mock(InputStream.class);
    //mockito 可以mock new?
        when(new Manifest(inputStream)).thenThrow(new IOException());
        when(servletContext.getResourceAsStream(anyString())).thenReturn(inputStream);

        defaultResource.get();
        fail("should throw exception");
    }
}