如何在 mockmvc 中将模拟对象作为 JSON 发送
2022-09-01 22:49:30
我想通过内容类型JSON的MockMvc在控制器中发送一个模拟对象。但是当我尝试序列化模拟时,错误是:
java.lang.UnsupportedOperationException: Expecting parameterized type, got interface org.mockito.internal.MockitoInvocationHandler.
Are you missing the use of TypeToken idiom?
我的代码如下:
@Test
public void testSomething(){
String xyz = "";
Integer i = 10;
SomeClass inst = mock(SomeClass.class, withSettings().serializable());
when(inst.getProperty1()).then(xyz);
when(inst.getProperty2()).then(i);
Gson gson = new Gson();
String json = gson.toJson(inst); // this is creating error
this.mockmvc.perform(put("/someUrl/").contentType(MediaType.JSON).content(json)).andExpect(status().isOk());
}
有人能告诉我我错过了什么吗?