你好,我有一个与此类似的代码,我需要为此编写junit测试,但是我不断收到nullpointeException,任何机构都可以为此编写测试吗?
public class myState{
@Inject
private StateData stateData;
public String getAbbreviation(){
try{
state selectedState= StateData.getSelectedState().getState();
formatedAbbrebiation=selectedState.getStline()+""+selectedState.getRegion();
}
catch(Exception e){
formatedAbbrebiation="abbrebiation not available"
}
return formatedAbbrebiation;
}
这是我尝试过的:
StateData stateData=Mockito.mock(StateData.class);
Mockito.when(stateData.getSelectedState().getState()).thenReturn(selectedState)
Mockito.when(selectedState.getStLine())).thenThrow(new Exception());