mikaelpe...@gmail.com
unread,Mar 28, 2013, 11:28:55 AM3/28/13Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to moc...@googlegroups.com
Hi,
I am having problems with testing the following using mockito. ( see classes below). It does not check that the polling is done in the while loop.
Any ideas on how I can accompish this?
br,
//mike
Class to Test
=========
/**
*
*
* @author mike
*
*/
public class Install {
private ExecutorService executor;
public Install(){
executor = Executors.newFixedThreadPool(1);
}
boolean checkInstall(Software sw,long timeout,Status expectedState,Progress expectedProgress){
boolean isSuccessful = false;
Future<Boolean> future = executor.submit(new InstallTask(sw,expectedState,expectedProgress));
try {
isSuccessful = future.get(timeout, TimeUnit.MILLISECONDS);
} catch (Exception e) {
future.cancel(true);
}
executor.shutdown();
return isSuccessful;
}
/**
* Timed task that checks state and progress.
*
*
* @author mike
*
*/
class InstallTask implements Callable<Boolean>{
private boolean condition = false;
private Status state;
private Progress progress;
private Software sw;
private Status expectedState;
private Progress expectedProgress;
public InstallTask (Software sw ,Status expectedState, Progress expectedProgress){
this.expectedState = expectedState;
this.expectedProgress = expectedProgress;
}
@Override
public Boolean call(){
while(!condition){
try{
setState(sw.getState());
setProgress(sw.getProgress());
}catch(Exception e){
throw new RuntimeException(e);
}
//Wait
isConditionFullfilled();
}
return condition;
}
public void isConditionFullfilled(){
if(expectedState == getState() && expectedProgress == getProgress()){
condition = true;
}
}
private Status getState() {
return state;
}
private void setState(Status state) {
this.state = state;
}
private Progress getProgress() {
return progress;
}
private void setProgress(Progress progress) {
this.progress = progress;
}
}
}
The test class
==========
/**
* @author mike
*
*/
public class InstallTest {
private ExecutorService executorServiceMock;
private Install install;
private Software swMock;
@Before
public void setUp() throws Exception {
executorServiceMock = Mockito.mock(ExecutorService.class);
swMock= Mockito.mock(Software.class);
install= new Install();
}
@SuppressWarnings("unchecked")
@Test
public void testCheckInstall() throws Exception {
final int threadTimeOut = 100;
//expectations
Future<Boolean> futureMock = (Future<Boolean>) Mockito.mock(Future.class);
Mockito.when(executorServiceMock.submit(Mockito.isA(InstallTask.class))).thenReturn(futureMock);
Mockito.when(futureMock.get(Mockito.anyInt(), Mockito.isA(TimeUnit.class))).thenReturn(true);
Mockito.doNothing().when(executorServiceMock).shutdown();
//run
upgradeConfirm.checkUpgradeConfirm(cppUpgradeMock, threadTimeOut, Status.INSTALL_COMPLETED, Progress.IDLE);
}