我正在尝试测试一个创建不同类对象的私有void方法。
目前,代码片段会抛出空指针异常。是否因为SendMail类的对象未被模拟而导致错误?
待测方法 -
private void sendActivationCode(MailInfoDTO mailInfoBn) throws SystemException {
**SendMail sm = new SendMail();**
try {
sm.setTo(mailInfoBn.getMailTo());
sm.setFrom(mailInfoBn.getMailFrom());
sm.setMailHost(PropertiesHelper.getProperty("smtp_server"));
sm.setPopServerHost(PropertiesHelper.getProperty("smtp_server"));
sm.setRequireAuth(true);
sm.setPopUserName(PropertiesHelper.getProperty("smtp_auth_user"));
sm.setPopPassword(PropertiesHelper.getProperty("smtp_auth_pass"));
sm.setMessageBody(mailInfoBn.getMessageBody());
sm.setSubject(mailInfoBn.getMessageSubject());
sm.setDebug(false);
sm.setKeepCopyInOS(false);
sm.sendSMTP();
} catch (Exception ex) {
throw new SystemException(ex);
}
}
测试类代码段 -
ProfileNotificationBO profileNotificationBOSpy = PowerMockito.spy(profileNotificationBO);
PowerMockito.doNothing().when(profileNotificationBOSpy, "sendActivationCode", Mockito.any(MailInfoDTO.class));
Stacktrace -
junit.framework.AssertionFailedError: Failed with following error: com.common.exceptions.SystemException: java.lang.NullPointerException
at com.profile.api.bo.ProfileNotificationBO.sendActivationCode(ProfileNotificationBO.java:791)
at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1873)
at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:773)
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:638)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:93)
at com.profile.api.bo.TestProfileNotificationBO.testGenerateActivationCodeWhenMblNotRegistered(TestProfileNotificationBO.java:1148)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
Caused by: java.lang.NullPointerException
at com.profile.api.bo.ProfileNotificationBO.sendActivationCode(ProfileNotificationBO.java:778)
at com.profile.api.bo.TestProfileNotificationBO.testGenerateActivationCodeWhenMblNotRegistered(TestProfileNotificationBO.java:1166)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
答案 0 :(得分:0)
通过删除不必要的代码段解决了该问题。
ProfileNotificationBO profileNotificationBOSpy = PowerMockito.spy(profileNotificationBO);
PowerMockito.doNothing().when(profileNotificationBOSpy, "sendActivationCode", Mockito.any(MailInfoDTO.class));
需要模拟SendMail类以传递测试用例。