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.
@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).HTHEric--
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/55e8aa67-2187-4f21-b4ec-9a77109a7377%40googlegroups.com.
ScheduledExecutorService mockScheduledService = mock(Scheduler.scheduledService.getClass()); Can i use like this in your code (Calling static scheduledService from Scheduler class) ?
--
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/c0822457-6bcd-4f86-8244-9534fbfd4656%40googlegroups.com.
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.
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/16074340-e570-4b44-aaaf-de4ae6a3d6a3%40googlegroups.com.
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.
same issue for when()...thenReturn() toowhen(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.
--
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/5bb23b90-5d2f-4fe2-848f-062014305189%40googlegroups.com.
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/caa55de7-1a71-4d2a-9ed2-bc09ff26ac3c%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/CA%2BkiFseViZya%2BJsF%2B37nqVveP5a0_b_TXu5MVbeV-2r7Xj0BcQ%40mail.gmail.com.