Junit 使用powermock访问超类方法调用时获取NullPointerException
使用powermock访问超类方法调用时获取NullPointerException 运行下面提到的testclass时,在开关(super.getType())的上面提到的行中出现getting NullPointerException错误 运行下面提到的testclass时,在开关(super.getType())的上面提到的行中出现getting NullPointerException错误Junit 使用powermock访问超类方法调用时获取NullPointerException,junit,powermock,Junit,Powermock,使用powermock访问超类方法调用时获取NullPointerException 运行下面提到的testclass时,在开关(super.getType())的上面提到的行中出现getting NullPointerException错误 运行下面提到的testclass时,在开关(super.getType())的上面提到的行中出现getting NullPointerException错误 没有回答,只有两条注释:switch(super.getType())。。。那看起来非常尴尬。在这
没有回答,只有两条注释:
switch(super.getType())
。。。那看起来非常尴尬。在这里叫super有什么意义?如果您在同一个类中重写该方法,在其他方法中调用超级版本,这确实令人惊讶。真奇怪的设计。。。除此之外。。。整个代码看起来不像是为了易于测试而编写的。因此,你现在必须做一些大的飞跃来测试它。我的建议是:退一步,把时间花在“更容易测试”的设计上。首先要避免PowerMock(ito)。没有答案,只有两条注释:switch(super.getType())
。。。那看起来非常尴尬。在这里叫super有什么意义?如果您在同一个类中重写该方法,在其他方法中调用超级版本,这确实令人惊讶。真奇怪的设计。。。除此之外。。。整个代码看起来不像是为了易于测试而编写的。因此,你现在必须做一些大的飞跃来测试它。我的建议是:退一步,把时间花在“更容易测试”的设计上。首先要避免PowerMock(ito)。
public class CommandParamInput extends AbstractCommandParam {
public CommandParamInput(
final ParameterFeedType commandParameterSourceToSet,
final Object definedValueToSet) {
this.commandParameterSource = commandParameterSourceToSet;
this.definedValue = definedValueToSet;
}
public void evaluateValue(final FDPRequest fdpRequest)
throws EvaluationFailedException {
switch (super.getType()) {
case ARRAY:
evaluateComplexValue(fdpRequest);
break;
}
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest({AbstractCommandParam.class,CommandParameterType.class,ParameterFeedType.class,LoggerUtil.class,Logger.class})
public class CommandParamInputTest {
private Logger loggerMock;
private FDPRequest instFDPRequest;
private FDPResponse instFDPResponse;
private FDPCacheable instFDPCacheable;
private AbstractCommandParam cmdParam;
private CommandParamInput spy;
@Mock
AbstractCommandParam absCommandParam;
@Mock
CommandParameterType cmdParameterType;
@Mock
ParameterFeedType parameterFeedType;
@InjectMocks
private CommandParamInput commandParamInput;
@Before
public void init() {
FDPRequestImpl fdoRequestImpl = new FDPRequestImpl();
fdoRequestImpl.setCircle(new FDPCircle(new Long(10),"10","test"));
fdoRequestImpl.setChannel(ChannelType.USSD);
instFDPRequest = fdoRequestImpl;
spy = PowerMockito.spy(new CommandParamInput(parameterFeedType.AUX_REQUEST_PARAM, instFDPCacheable));
}
@Test
public void testEvaluateValue()throws ExecutionFailedException,
EvaluationFailedException, FileNotFoundException, RuleException{
mockCommonObjects();
commonMockExternalCall();
absCommandParam = new CommandParamInput(parameterFeedType.AUX_REQUEST_PARAM, instFDPCacheable);
absCommandParam.setType(cmdParameterType.PARAM_IDENTIFIER);
PowerMockito.doReturn(cmdParameterType.PARAM_IDENTIFIER).when(spy).getType();
// when(absCommandParam.getType()).thenReturn(cmdParameterType.PARAM_IDENTIFIER);
PowerMockito.suppress(PowerMockito.methods(AbstractCommandParam.class, "getType"));
commandParamInput.evaluateValue(instFDPRequest);
}
}