- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的代码中有以下几行:
@Autowired
private ObjectMapper mapper = new ObjectMapper();
@PostMapping("/postPrueba")
public ResponseEntity<String> postPrueba(@RequestBody Prueba prueba) {
String pTest = null;
try {
pTest = mapper.writeValueAsString(prueba);
System.out.println(pTest );
} catch (JsonProcessingException e) {
return new ResponseEntity<>("", HttpStatus.INTERNAL_SERVER_ERROR);
}
return new ResponseEntity<>("", HttpStatus.OK);
}
我的模型Prueba.java
public class Prueba {
@Id
private String nombre;
private String apellidos;
private String edad;
}
在测试中我想强制 JsonProcessingException 但我不能。我已经尝试过这个:
@Mock
private ObjectMapper mapperMock;
@Test
public void testKo() throws Exception {
ObjectMapper om = Mockito.spy(new ObjectMapper());
Mockito.when(this.mapperMock.writeValueAsString(Mockito.any())).thenThrow(new JsonProcessingException("") {});
ObjectMapper om = Mockito.spy(new ObjectMapper());
Mockito.when( om.writeValueAsString(Mockito.eq(Prueba.class))).thenThrow(new JsonProcessingException("") {});
Mockito.when(this.mapperMock.writeValueAsString(Mockito.eq(Prueba.class))).thenThrow(new JsonProcessingException("") {});
String jsonContent = "{'nombre': '123456', 'apellidos': '12'}";
jsonContent = jsonContent.replaceAll("\'", "\"");
this.mvc.perform(post("/postPrueba")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonContent))
.andExpect(status().is5xxServerError());
}
但响应始终是 200 OK。我该怎么做?
最佳答案
doThrow(JsonProcessingException.class).when(object).method(...);
关于java - 如何使用mockito抛出JsonProcessingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59412688/
我正在尝试使用 JMockito 测试以下代码。 代码 @RunWith(MockitoJUnitRunner.class) public class StepFunctionClientTest {
我正在研究一种将 Jackson 反序列化 JSON 到类实例的解决方案,而不破坏整个过程,目前当我执行以下操作时: 如果 Actor.class 是这样的: @JsonInclude(JsonInc
我想编写一个单元测试来测试 JsonProcessingException。此异常可能发生在以下行:mapper.writeValueAsString()。 public void myMethod(
我正在尝试创建一个由模拟对象抛出的 JsonProcessingException。 when(mapper.writeValueAsString(any(Object.class))).thenTh
我是 jackson 的新手。我试图将 POJO(普通的旧 java 对象)序列化为 JSON,但我不断收到 JsonProcessingException。我实在不明白为什么。我错过了什么吗? pu
可能是一个奇怪的问题,但确实我想在我的测试中实现更多覆盖,虽然我针对 JsonProcessingException 进行了编码,但我无法创建生成此异常的有效负载,可能是因为Jackson 非常聪明,
当我尝试将 Jackson 2.1.1 与以下 jar 文件一起使用时(在 Spring 3.2.2 中), jackson-core-2.1.1.jar jackson-annotations-2.
我正在尝试使用 jackson 对 catch block 进行单元测试以将 List 转换为 Json 格式。这是我的代码: public String convert(List list)
我正在使用 templateRest发帖 User反对 Rest Server但我遇到了以下错误: Exception in thread "main" java.lang.NoClassDefFou
这个问题在这里已经有了答案: Which JAR file contains the JsonProcessingException class? (2 个答案) 关闭 8 年前。 首先,很抱歉再次
我有一个由 Tomcat 8 提供支持的 servlet,它调用一个静态方法来使用 Jackson 2.9.6 执行 JSON 解析。 代码执行期间: JSONData parsedJSONData
我是一名优秀的程序员,十分优秀!