【发布时间】:2013-12-20 05:48:08
【问题描述】:
我正在学习模拟和单元测试。这是我的构造函数。为了测试它抛出 jparse 异常的构造函数,我有一个测试方法。为什么我们要生成自己的异常并断言它。它是如何测试的?我们在这里测试什么?请帮忙!
public ClassA(File file) throws JsonParseException,
JsonMappingException, IOException {
ObjectMapper json= new ObjectMapper();
Map<String, String>> readValue= mapper.readValue(file,
Map.class);
..........
}
@Test(expected = JsonParseException.class)
public void testCorruotionInContent() throws Exception {
ObjectMapper json= Mockito.mock(ObjectMapper.class);
PowerMockito.whenNew(ObjectMapper.class).withNoArguments()
.thenReturn(json);
Mockito.when(
mapper.readValue(Mockito.any(File.class), Mockito.eq(Map.class)))
.thenThrow(new JsonParseException(null, null));
new ClassA(Mockito.mock(File.class));
}
【问题讨论】:
标签: java unit-testing exception junit mockito