我正在使用Java 1.8.0_131,Mockito 2.8.47和PowerMock 1.7.0。我的问题与PowerMock无关,它被发布到Mockito.when(...)匹配器。Mockito匹配器匹配与仿制药和供应商的方法
我需要一个解决方案来嘲笑了这种方法,通过我的课下的测试,称为:
public static <T extends Serializable> PersistenceController<T> createController(
final Class<? extends Serializable> clazz,
final Supplier<T> constructor) { … }
的方法是从类下的测试,称为像这样:
PersistenceController<EventRepository> eventController =
PersistenceManager.createController(Event.class, EventRepository::new);
对于测试,我首先创建我的模拟对象,当上述方法被调用时应该返回:
final PersistenceController<EventRepository> controllerMock =
mock(PersistenceController.class);
这很容易。问题是方法参数的匹配器,因为该方法将泛型与供应商结合使用作为参数。下面的代码编译,并预期返回null:
when(PersistenceManager.createController(any(), any()))
.thenReturn(null);
当然,我不想返回null。我想返回我的模拟对象。由于泛型不能编译。不是时候的匹配,所以匹配不工作,则返回null
when(PersistenceManager.createController(Event.class, EventRepository::new))
.thenReturn(controllerMock);
这将编译但参数在我的:为了符合我必须写这样的类型。我不知道如何编写匹配我的参数的匹配器,并返回我的模拟对象。你有什么主意吗?
非常感谢您 马库斯
您可以发布[MCVE]再现问题: 您可以使用
Matcher.<...>any()
语法,它指定? – 2017-07-15 07:50:46对不起,@janos,为我的后续跟进。我无法在我的项目上工作几天。今天我用了你的语法和'ArgumentMatcher',我的问题解决了!非常感谢你! –