使用 PowerMockito 如何使用一组特定的参数验证调用了构造函数
Using PowerMockito how do I verify a constructor was called, with a specific set of arguments
注意:预料到那些想要指出在其中构建对象的代码的糟糕设计,而不是通过依赖注入或工厂,这很容易被嘲笑;我正在为遗留代码编写测试,将代码重构为更现代的设计不是一种选择。
我有一个命令方法,当它执行时会在 class MyObjectWrapper 中构造三个对象,这依赖于另一个 class MyObject。在测试中,这两个 classes 和 6 个对象都被模拟了。考虑以下代码:
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyCommand.class)
public class MyCommandTest {
@Mock public MyObject objectOne;
@Mock public MyObject objectTwo;
@Mock public MyObject objectThree;
@Mock public MyObjectWrapper wrapperOne;
@Mock public MyObjectWrapper wrapperTwo;
@Mock public MyObjectWrapper wrapperThree;
private MyCommand command;
@Before public void beforeEach() {
command = new MyCommand();
MockitoAnnotations.initMocks(this);
initialiseWrapper(wrapperOne, objectOne, true, false);
initialiseWrapper(wrapperTwo, objectTwo, false, false);
initialiseWrapper(wrapperThree, objectThree, true, true);
}
private void initialiseWrapper(MyObjectWrapper wrapperMock, MyObject objMock, boolean option1, boolean option2) {
wrapperMock = PowerMockito.mock(MyObjectWrapper.class);
PowerMockito.whenNew(MyObjectWrapper.class)
.withParameters(MyObject.class, Boolean.class, Boolean.class)
.withArguments(objMock, option1, option2)
.thenReturn(wrapperMock);
}
@Test public void testConstructoresCalled() throws Exception {
command.execute();
VERIFY constructor with arguments: objectOne, true, false
VERIFY constructor with arguments: objectTwo, false, false
VERIFY constructor with arguments: objectThree, true, true
}
}
我知道我可以确认构造函数被调用了 3 次:
PowerMockito.verifyNew(MyObjectWrapper.class, times(3));
但是我需要确认调用了构造函数,并传入了三个参数。可以这样做吗?
PowerMockito.html#verifyNew returns a ConstructorArgumentsVerification
, so use the returned object, see ConstructorArgumentsVerification
javadoc
注意:预料到那些想要指出在其中构建对象的代码的糟糕设计,而不是通过依赖注入或工厂,这很容易被嘲笑;我正在为遗留代码编写测试,将代码重构为更现代的设计不是一种选择。
我有一个命令方法,当它执行时会在 class MyObjectWrapper 中构造三个对象,这依赖于另一个 class MyObject。在测试中,这两个 classes 和 6 个对象都被模拟了。考虑以下代码:
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyCommand.class)
public class MyCommandTest {
@Mock public MyObject objectOne;
@Mock public MyObject objectTwo;
@Mock public MyObject objectThree;
@Mock public MyObjectWrapper wrapperOne;
@Mock public MyObjectWrapper wrapperTwo;
@Mock public MyObjectWrapper wrapperThree;
private MyCommand command;
@Before public void beforeEach() {
command = new MyCommand();
MockitoAnnotations.initMocks(this);
initialiseWrapper(wrapperOne, objectOne, true, false);
initialiseWrapper(wrapperTwo, objectTwo, false, false);
initialiseWrapper(wrapperThree, objectThree, true, true);
}
private void initialiseWrapper(MyObjectWrapper wrapperMock, MyObject objMock, boolean option1, boolean option2) {
wrapperMock = PowerMockito.mock(MyObjectWrapper.class);
PowerMockito.whenNew(MyObjectWrapper.class)
.withParameters(MyObject.class, Boolean.class, Boolean.class)
.withArguments(objMock, option1, option2)
.thenReturn(wrapperMock);
}
@Test public void testConstructoresCalled() throws Exception {
command.execute();
VERIFY constructor with arguments: objectOne, true, false
VERIFY constructor with arguments: objectTwo, false, false
VERIFY constructor with arguments: objectThree, true, true
}
}
我知道我可以确认构造函数被调用了 3 次:
PowerMockito.verifyNew(MyObjectWrapper.class, times(3));
但是我需要确认调用了构造函数,并传入了三个参数。可以这样做吗?
PowerMockito.html#verifyNew returns a ConstructorArgumentsVerification
, so use the returned object, see ConstructorArgumentsVerification
javadoc