我需要模拟两个对象
Holder<MyClass1> h1 = new Holder<>();
Holder<MyClass2> h2 = new Holder<>();
我使用Mockito和PowerMock
Holder<MyClass1> mock1 = PowerMockito.spy(new Holder<MyClass1>());
Holder<MyClass2> mock2 = PowerMockito.spy(new Holder<MyClass2>());
PowerMockito.whenNew(Holder.class).withNoArguments().thenReturn(mock1);
如何指定通用类型来模拟两个不同的对象?我需要2个模拟测试。谢谢。
一个选项是使用通配符'?'
,as:
Holder<?> holder = new Holder<>();
Holder<?> mock1 = PowerMockito.spy(holder);
Holder<?> mock2 = PowerMockito.spy(holder);
PowerMockito.whenNew(Holder.class).withNoArguments().thenReturn(mock1);