Approach how to test a task

1,649 views
Skip to first unread message

mikaelpe...@gmail.com

unread,
Mar 28, 2013, 11:28:55 AM3/28/13
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&lt;Boolean&gt; futureMock = (Future&lt;Boolean&gt;) 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);
        
        
        
    }

Eric Lefevre-Ardant

unread,
Mar 28, 2013, 1:28:46 PM3/28/13
to moc...@googlegroups.com
Hm... it is not clear to me what you want to test precisely. It doesn't seem particularly difficult to check whether the state has changed.

Also, could you maybe (significantly) shorten the source code? After a couple of screens, I couldn't concentrate.



--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages