Mock constructor with mockito(使用 mockito 模拟构造函数)
问题描述
我想将构造函数模拟为方法.
I want to mock a constructor into method.
public String generaID() {
GeneraIDParaEntidadCliente aux = new GeneraIDParaEntidadCliente(nombre, registro);
entidad.setID(aux.generaID);
}
在我的测试中,我想做这样的事情:
In my test I want do something like this :
when(new GeneraIDParaEntidadCliente(anyString(), any(Entidad.class)).thenReturn(generaIdMock)
但是给我这个error org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
知道为什么吗?
推荐答案
可以使用 PowerMock 来模拟构造函数.
You can use PowerMock to mock constructors.
如果由于某种原因不能使用 PowerMock,最可行的解决方案是将工厂注入到包含此方法的任何类.然后,您将使用工厂创建您的 GeneraIDParaEntidadCliente
对象并模拟工厂.
If you can't use PowerMock for some reason, the most workable solution is to inject a factory to whatever class contains this method. You would then use the factory to create your GeneraIDParaEntidadCliente
object and mock the factory.
这篇关于使用 mockito 模拟构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 mockito 模拟构造函数


基础教程推荐
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13