使用 PowerMock 模拟在被测 class 的构造函数中调用的私有方法
Using PowerMock to mock a private method called in the constructor of the class under test
我不知道 Powermock 是否可行。
我需要使用 Powermock 来模拟在我需要测试的 class 的构造函数中调用的私有方法。
所以我有一个这样的测试class:
@RunWith(PowerMockRunner.class)
@PrepareForTest(XMLTransaction.class)
public class CloseSummaryOrCloseTrailerResponseTest {
public final static String URL="WL_APPSERVER";
private XMLTransaction xmlTransaction;
@Before
public void initMocks() throws Exception {
xmlTransaction = PowerMockito.spy(new XMLTransaction(URL));
PowerMockito.doAnswer(new org.mockito.stubbing.Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null; //does nothing
}
}).when(xmlTransaction.getClass(), "initialize");
PowerMockito.doNothing().when(xmlTransaction.getClass(), "initialize");
}
@Test
public void whenCloseSummaryResponseNoErrorExpectCorrectXmlMsgProduced () throws Exception
{
//set the mock object here
try {
String actualXmlScannerMsg = xmlTransaction.closeSummaryResponseToXMLNoErrors(mockCloseTrailerResponse);
Assert.assertNotNull(actualXmlScannerMsg);
Assert.assertEquals(msgNoCarReturnCharCloseSummaryResponse, actualXmlScannerMsg);
}
catch(JsonProcessingException jEx)
{
Assert.fail("JsonProcessingException: " + jEx.getMessage());
}
catch(Exception ex)
{
Assert.fail("Exception occurred: " + ex.getMessage());
}
}
}
创建间谍时出现空指针异常。
构造函数 new XMLTransaction(URL) 调用 initialize 方法,这是我什么都不做的方法。
有什么办法可以解决这个问题。如果我使用默认构造函数,则不会创建 class。
我想通了...
我创建了一个默认构造函数并将初始化方法中实例化的所有 类 设置为 null。
从 initMock()
中删除了这个
PowerMockito.doAnswer(new org.mockito.stubbing.Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null; //does nothing
}
}).when(xmlTransaction.getClass(), "initialize");
PowerMockito.doNothing().when(xmlTransaction.getClass(), "initialize");
我不知道 Powermock 是否可行。 我需要使用 Powermock 来模拟在我需要测试的 class 的构造函数中调用的私有方法。 所以我有一个这样的测试class:
@RunWith(PowerMockRunner.class)
@PrepareForTest(XMLTransaction.class)
public class CloseSummaryOrCloseTrailerResponseTest {
public final static String URL="WL_APPSERVER";
private XMLTransaction xmlTransaction;
@Before
public void initMocks() throws Exception {
xmlTransaction = PowerMockito.spy(new XMLTransaction(URL));
PowerMockito.doAnswer(new org.mockito.stubbing.Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null; //does nothing
}
}).when(xmlTransaction.getClass(), "initialize");
PowerMockito.doNothing().when(xmlTransaction.getClass(), "initialize");
}
@Test
public void whenCloseSummaryResponseNoErrorExpectCorrectXmlMsgProduced () throws Exception
{
//set the mock object here
try {
String actualXmlScannerMsg = xmlTransaction.closeSummaryResponseToXMLNoErrors(mockCloseTrailerResponse);
Assert.assertNotNull(actualXmlScannerMsg);
Assert.assertEquals(msgNoCarReturnCharCloseSummaryResponse, actualXmlScannerMsg);
}
catch(JsonProcessingException jEx)
{
Assert.fail("JsonProcessingException: " + jEx.getMessage());
}
catch(Exception ex)
{
Assert.fail("Exception occurred: " + ex.getMessage());
}
}
}
创建间谍时出现空指针异常。 构造函数 new XMLTransaction(URL) 调用 initialize 方法,这是我什么都不做的方法。
有什么办法可以解决这个问题。如果我使用默认构造函数,则不会创建 class。
我想通了... 我创建了一个默认构造函数并将初始化方法中实例化的所有 类 设置为 null。 从 initMock()
中删除了这个PowerMockito.doAnswer(new org.mockito.stubbing.Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null; //does nothing
}
}).when(xmlTransaction.getClass(), "initialize");
PowerMockito.doNothing().when(xmlTransaction.getClass(), "initialize");