ScheduledExecutorService Mockito

5,951 views
Skip to first unread message

Cars

unread,
Dec 16, 2015, 4:52:14 AM12/16/15
to mockito
How to test Scheduler class using Mockito which implements ScheduledExecutorService  and ScheduledFuture

Brice Dutheil

unread,
Dec 16, 2015, 5:07:26 AM12/16/15
to moc...@googlegroups.com

Hi,

Your question is way too generic for us to even Answer, what is the Scheduler class, what exactly do you want to test.Don’t mock everything and that one should avoid mocking third party libs.

=> https://github.com/mockito/mockito/wiki/How-to-write-good-tests

Cheers
— Brice

On Wed, Dec 16, 2015 at 1:39 AM, Cars <asho...@gmail.com> wrote:

How to test Scheduler class using Mockito which implements ScheduledExecutorService  and ScheduledFuture

--
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 https://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/34a44b67-172d-4a82-8eaa-699971780e49%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Cars

unread,
Dec 17, 2015, 2:50:22 AM12/17/15
to mockito
Following mock Scheduler test method not working.      


@Test
public void  schedulerTest() {

        Runnable mockRunnable = mock(Runnable.class);
        ScheduledExecutorService mockScheduledService = mockScheduledExecutor();
        
        mockScheduledService.scheduleWithFixedDelay(mockRunnable, 1, 1, TimeUnit.DAYS);

        // When
        //monitor.watchScheduledThread(mockScheduledService, mockRunnable, 1, 1, TimeUnit.DAYS);

        // Then
        verify(mockScheduledService).scheduleAtFixedRate(eq(mockRunnable), anyLong(), anyLong(), any(TimeUnit.class));



   private ScheduledExecutorService mockScheduledExecutor() {      
        ScheduledExecutorService executorService = mock(Scheduler.scheduler.getClass());
        given(executorService.scheduleWithFixedDelay(
                any(Runnable.class),
                anyLong(),
                anyLong(),
                any(TimeUnit.class)))             
                .willReturn(mock(Scheduler.future.getClass()));
        return executorService;
    }
)




public class Scheduler {

    public static ScheduledExecutorService scheduledService;
    public static ScheduledFuture<?> future;


    public void scheduler() {
        scheduledService = Executors.newScheduledThreadPool(1);

        final Runnable runnable;
        runnable = new Runnable() {
            @Override
            public void run() {  
                try {
                    if (!future.isDone()) {                      
                            future.cancel(true);
                            scheduler();
                    }
                } catch (Throwable t) {

                }
            }
        };

        future = schedulerService.scheduleWithFixedDelay(
                runnable, x , x, TimeUnit.SECONDS);
    }
}

Eric Lefevre-Ardant

unread,
Dec 17, 2015, 3:51:30 AM12/17/15
to moc...@googlegroups.com
Cars,

There are many issues with the code you gave.
The first problem is that you are ostensibly testing the Scheduler class. However, your test makes no reference to the implementation of the class. It is only referring to the dependencies. Indeed, one could rewrite your test in the following way:
@Test
public void schedulerTest() {
Runnable mockRunnable = mock(Runnable.class);
    ScheduledExecutorService mockScheduledService = mock(ScheduledExecutorService.class);
BDDMockito.given(mockScheduledService.scheduleWithFixedDelay(any(Runnable.class), anyLong(), anyLong(),
any(TimeUnit.class))).willReturn(mock(ScheduledFuture.class));

mockScheduledService.scheduleWithFixedDelay(mockRunnable, 1, 1, TimeUnit.DAYS);

verify(mockScheduledService).scheduleAtFixedRate(eq(mockRunnable), anyLong(), anyLong(), any(TimeUnit.class));
}

It is completely equivalent to the code you have offered. It is however buggy, as you are first configuring the behaviour of "scheduleWithFixedDelay", then verifying the behaviour of "scheduleAtFixedRate" ("at", not "with"). Maybe that is the reason you are contacting this mailing list in the first place?
This test is also useless. It is configuring a mock, calling the mock directly, and then testing that the mock has been called. What's the benefit of that?
I suppose that what you actually want is test the behaviour of the Scheduler class. However, you cannot do that in the way it is coded, as its dependencies are obtained from calls to static methods, which makes it hard to provide custom dependencies.
It seems that you are trying to shoe-horn Mockito to test your existing code. Generally speaking, this is not a great idea, as it is often hard to do; you at the very least need to refactor drastically your production code. 
The philosophy behind Mockito is that you need to start using it before the production code is written, and guide your creation of code based on the tests you write. You might want to read up on Test-Driven Development for more discussion on this. If that is not what you want to do, maybe you need other tools, such as PowerMock (unrelated to Mockito).
HTH
Eric

--
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 https://groups.google.com/group/mockito.

Cars

unread,
Dec 17, 2015, 10:41:17 AM12/17/15
to mockito
ScheduledExecutorService mockScheduledService = mock(Scheduler.scheduledService.getClass());  Can i use like this in your code  (Calling static scheduledService from Scheduler class) ?



On Wednesday, December 16, 2015 at 1:52:14 AM UTC-8, Cars wrote:

Eric Lefevre-Ardant

unread,
Dec 17, 2015, 10:47:38 AM12/17/15
to moc...@googlegroups.com
Technically, yes, but why not simply use mock(ScheduledExecutorService.class)? That would make the test more readable.

--
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 https://groups.google.com/group/mockito.

Cars

unread,
Dec 17, 2015, 12:54:45 PM12/17/15
to mockito
 public static ScheduledExecutorService scheduledService;   

scheduledService initialization is inside scheduler() method and It may give NullPointerException.


On Wednesday, December 16, 2015 at 1:52:14 AM UTC-8, Cars wrote:

Cars

unread,
Dec 17, 2015, 3:08:04 PM12/17/15
to mockito
 BDDMockito.given(mockScheduledService.scheduleWithFixedDelay(any(Runnable.class), anyLong(), anyLong(),
any(TimeUnit.class))).willReturn(mock(ScheduledFuture.class));

It gives org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 4 matchers expected, 2 recorded.

On Wednesday, December 16, 2015 at 1:52:14 AM UTC-8, Cars wrote:

Juan Pablo Gardella

unread,
Dec 17, 2015, 3:13:29 PM12/17/15
to moc...@googlegroups.com
Try with

ScheduledFuture s = mock(ScheduledFuture.class)

 BDDMockito.given(mockScheduledService.scheduleWithFixedDelay(any(Runnable.class), anyLong(), anyLong(),
any(TimeUnit.class))).willReturn(s);


--
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 https://groups.google.com/group/mockito.

Cars

unread,
Dec 17, 2015, 4:14:55 PM12/17/15
to mockito
ScheduledFuture s = mock(ScheduledFuture.class)

 BDDMockito.given(mockScheduledService.scheduleWithFixedDelay(any(Runnable.class), anyLong(), anyLong(),
any(TimeUnit.class))).willReturn(s);

same issue.  org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 4 matchers expected, 2 recorded.

On Wednesday, December 16, 2015 at 1:52:14 AM UTC-8, Cars wrote:

Cars

unread,
Dec 17, 2015, 4:32:42 PM12/17/15
to mockito
same issue for when()...thenReturn() too

when(mockScheduledService.scheduleWithFixedDelay(any(Runnable.class), anyLong(), anyLong(),
any(TimeUnit.class))).thenReturn(s);

 org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 4 matchers expected, 2 recorded.

On Wednesday, December 16, 2015 at 1:52:14 AM UTC-8, Cars wrote:

Juan Pablo Gardella

unread,
Dec 17, 2015, 5:10:52 PM12/17/15
to moc...@googlegroups.com
mockito version?

--
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 https://groups.google.com/group/mockito.

Cars

unread,
Dec 17, 2015, 6:57:31 PM12/17/15
to mockito
EasyMock version 1.10.19


On Wednesday, December 16, 2015 at 1:52:14 AM UTC-8, Cars wrote:

Juan Pablo Gardella

unread,
Dec 17, 2015, 9:41:24 PM12/17/15
to moc...@googlegroups.com
EasyMock or Mockito? Could you verify if with 1.9.5 the issue is happen too?

EasyMock version 1.10.19
--
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 https://groups.google.com/group/mockito.

Brice Dutheil

unread,
Dec 24, 2015, 5:11:31 AM12/24/15
to moc...@googlegroups.com
Maybe check the code import statements.

-- Brice

Reply all
Reply to author
Forward
0 new messages