我得到了这个异常- java.lang.IllegalArgumentException: No serializer found for class org.mockito.internal.creation.bytebuddy.ByteBuddyCrossClassLoaderSerializationSupport and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.mockito.codegen.Object$MockitoMock$["mockitoInterceptor"]->org.mockito.internal.creation.bytebuddy.MockMethodInterceptor["serializationSupport"])
我正在编写的Testcase如下所示
@Mock
Object object;
@Mock
ResponseType response;
@Test
public void handleRequest() {
ObjectMapper mapper = new ObjectMapper();
when(mapper.convertValue(object, ResponseType.class)).thenReturn(response)
new Handler().handleRequest(object);
}
我试着测试的方法:
public class Handler{
public ResponseType handleRequest(Object object) {
ObjectMapper mapper = new ObjectMapper();
ResponseType response = mapper.convertValue(object, ResponseType.class);
return response;
}
}
我看到我可以禁用SerializationFeature.FAIL_ON_EMPTY_BEANS,但是我无法控制基类,我只能编写一个测试用例。有人能告诉我我做错了什么,以及我能给测试用例添加什么吗?我无法在测试用例中实例化一个ObjectMapper,因为它必须是一个模拟,我尝试在ObjectMapper上使用一个间谍并禁用SerializationFeature.FAIL_ON_EMPTY_BEANS,但两者都不起作用。我对Mockito也很陌生,所以我不知道怎样才能继续前进。任何帮助都将不胜感激,谢谢
发布于 2022-09-15 22:58:16
测试代码有两个问题。您正在调用真正的ObjectMapper方法,而不是模拟它(1),并且在实际代码(2)中没有使用创建的对象(尽管它不是一个模拟)。
在下面的行中(我添加了换行符和注释,但与代码相同),您正在创建ObjectMapper
的实例(而不是模拟),在尝试模拟它的行为时,实际上调用了创建对象的真正方法:
ObjectMapper mapper = new ObjectMapper();
when(
// here an actual convertValue method is called on the mapper object
// which causes the exception you're getting
mapper.convertValue(object, ResponseType.class)
).thenReturn(response)
如果您使用的方法不是在模拟时调用实际方法(在使用间谍时很有用):
doReturn(response)
.when(mapper)
.convertValue(object, ResponseType.class);
您仍然会得到一个错误,但这一次是:
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to when() is not a mock!
Example of correct stubbing:
doThrow(new RuntimeException()).when(mock).someMethod();
这是因为您正在创建的mapper
不是一个Mockito模拟/间谍,所以它不能被模仿。要解决需要使用mock(...)
方法或@Mock
注释(记得初始化/打开它们)的问题,请执行以下操作:
ObjectMapper mapper = mock(ObjectMapper.class);
when(mapper.convertValue(object, ResponseType.class))
.thenReturn(response);
现在,您得到的初始错误和NotAMockException
都没有抛出,但是模拟没有任何效果。
在您的实际代码中:
public ResponseType handleRequest(Object object) {
ObjectMapper mapper = new ObjectMapper();
...
}
您正在使用ObjectMapper
创建new
。这个对象根本没有连接到在测试中创建的模拟-它没有被注入。要解决这个问题,最好将ObjectMapper
实例存储在字段中,并使用类创建(注入)初始化它--无论是在实际代码中还是在测试中:
class Handler {
private ObjectMapper objectMapper;
Handler(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
public ResponseType handleRequest(Object object) {
// use the objectMapper here
}
}
因此,您可以将测试中的模拟ObjectMapper
传递给测试对象构造函数:
@Test
public void handleRequest() {
ObjectMapper mapper = mock(ObjectMapper.class);
when(mapper.convertValue(object, ResponseType.class)).thenReturn(response);
new Handler(mapper).handleRequest(object);
}
其他方法可以是使用提供ObjectMapper
的工厂,也可以在测试中模拟,或者(不推荐)使用Mockito -与它的mockConstruction方法(自从Mockito 3.5.0)。
https://stackoverflow.com/questions/73736799
复制相似问题