I must covered whit Junit4 and Mockito/PowerMockito the While Block and the catch block. The Exception StateException is launched by the getHrest method
public class Fstate{
….
private Date selectedDate;
private Date dateYes;
private Collection<State> sStateList;
private boolean statePlus;
private String stateVisibility;
@EJB
private Muller mull;
….
public void methodState(){
final Calendar calendar=new GregorianCalendar();
this.dateYes=calendar.getTime();
this.selectedDate = this.dateYes;
Collection<State> allState=null;
try{
allState=this.mull.getHrest(this.p, this.b, this.ba, this.so);
Iterator<State> iter=allState.iterator();
while(iter.hasNext()){
State s=iter.next();
String s_string=s.getSfielString();
this.sStateList.add(s_string);
}
} catch (StateException stateException){
WebLogger.fatal(stateException.getMessage(), LOGGER_OUT);
}
this.statePlus=loadStatePlus(this.dateYes);
this.stateVisibility=STATE_PLUS;
}
}
Below, in the first method I want to cover the while execution while in the second method I want to cover the catch. However, this does not generate the expected result, while block and catch not covered!
@Test
public void state_Test(){
try {
Fstate spyFstate = Mockito.spy(Fstate.class);
State mockState=Mockito.mock(Muller.class);
Mockito.when(mockState.getHrest(anyString(), anyString(), anyString(), anyString())).thenReturn(createCollectionOfStates());
Whitebox.setInternalState(spyFstate, "mull",mockState);
spyFstate.methodState();
Mockito.verify(spyFstate, Mockito.times(1)).methodState();
} catch (StateException e) {
System.out.println("StateException in state_Test method!");
}
}
@Test
public void stateException_Test(){
try {
Fstate spyFstate = Mockito.spy(Fstate.class);
State mockState=Mockito.mock(Muller.class);
Mockito.when(mockState.getHrest(anyString(), anyString(), anyString(), anyString())).thenThrow(new StateException("StateException Exception"));
Whitebox.setInternalState(spyFstate, "mull",mockState);
spyFstate.methodState();
Mockito.verify(spyFstate, Mockito.times(1)).methodState();
} catch (DealAnagException e) {
e.printStackTrace();
}
}