英文:
How to make integration test fail when objectMapper throws exception
问题
我正在使用 JUNIT 5 的 `junit-platform-console-standalone` 库编写集成测试。在使用 Jackson 读取 JSON 对象时,如果在读取文件过程中出现任何错误,我希望使测试失败。
尝试将 throws IOException 添加到方法签名中,但似乎不起作用。
@Test
void objectMapperTest(){
// 逐行读取文件
objname.foreach(obj -> {
try{
Result result = objectMapper.readValue(line,Result.class);
} catch(IOException e) {
// 如何使测试失败?
}
});
}
英文:
I am writing using JUNIT 5 junit-platform-console-standalone
library to write integration tests. While reading a JSON object with Jackson, I wish to fail the test if there is any error in reading file.
Tried adding throws IOException to method signature but that doesn;t seem to work.
@Test
void objectMapperTest(){
// reading file line by line
objname.foreach(obj -> {
try{
Result result = objectMapper.readValue(line,Result.class);
} catch(IOException e) {
//How to make the test fail ?
}
});
}
答案1
得分: 0
你可以使用 fail()
@Test
void objectMapperTest(){
// 逐行读取文件
objname.foreach(obj -> {
try{
Result result = objectMapper.readValue(line,Result.class);
} catch(IOException e) {
Assertions.fail("测试失败信息");
}
});
}
英文:
You can use fail()
@Test
void objectMapperTest(){
// reading file line by line
objname.foreach(obj -> {
try{
Result result = objectMapper.readValue(line,Result.class);
} catch(IOException e) {
Assertions.fail("Test failure message");
}
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论