使用powermock访问SuperClass方法调用时获取NullPointerException

时间:2019-04-07 05:30:22

标签: junit powermock

使用powermock访问SuperClass方法调用时获取NullPointerException

在运行以下提到的测试类时,在switch(super.getType())的上述行中出现NullPointerException错误

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;
        }
    }
}

在运行以下提到的测试类时,在switch(super.getType())的上述行中出现NullPointerException错误

@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);    
    }
}

0 个答案:

没有答案
相关问题