Skip to content

Commit dbca06a

Browse files
committed
Added proper tests for half-sync-half-async
1 parent ca14e8d commit dbca06a

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

half-sync-half-async/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@
1414
<artifactId>junit</artifactId>
1515
<scope>test</scope>
1616
</dependency>
17+
<dependency>
18+
<groupId>org.mockito</groupId>
19+
<artifactId>mockito-core</artifactId>
20+
<scope>test</scope>
21+
</dependency>
1722
</dependencies>
1823
</project>

half-sync-half-async/src/main/java/com/iluwatar/halfsynchalfasync/AsynchronousService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public <T> void execute(final AsyncTask<T> task) {
5050
task.onPreCall();
5151
} catch (Exception e) {
5252
task.onError(e);
53+
return;
5354
}
5455

5556
service.submit(new FutureTask<T>(task) {
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.iluwatar.halfsynchalfasync;
2+
3+
import org.junit.Test;
4+
import org.mockito.InOrder;
5+
6+
import java.io.IOException;
7+
import java.util.concurrent.LinkedBlockingQueue;
8+
9+
import static org.mockito.Matchers.eq;
10+
import static org.mockito.Mockito.doThrow;
11+
import static org.mockito.Mockito.inOrder;
12+
import static org.mockito.Mockito.mock;
13+
import static org.mockito.Mockito.timeout;
14+
import static org.mockito.Mockito.times;
15+
import static org.mockito.Mockito.verify;
16+
import static org.mockito.Mockito.verifyNoMoreInteractions;
17+
import static org.mockito.Mockito.when;
18+
19+
/**
20+
* Date: 12/12/15 - 11:15 PM
21+
*
22+
* @author Jeroen Meulemeester
23+
*/
24+
public class AsynchronousServiceTest {
25+
26+
@Test
27+
public void testPerfectExecution() throws Exception {
28+
final AsynchronousService service = new AsynchronousService(new LinkedBlockingQueue<>());
29+
final AsyncTask<Object> task = mock(AsyncTask.class);
30+
final Object result = new Object();
31+
when(task.call()).thenReturn(result);
32+
service.execute(task);
33+
34+
verify(task, timeout(2000)).onPostCall(eq(result));
35+
36+
final InOrder inOrder = inOrder(task);
37+
inOrder.verify(task, times(1)).onPreCall();
38+
inOrder.verify(task, times(1)).call();
39+
inOrder.verify(task, times(1)).onPostCall(eq(result));
40+
41+
verifyNoMoreInteractions(task);
42+
}
43+
44+
@Test
45+
public void testCallException() throws Exception {
46+
final AsynchronousService service = new AsynchronousService(new LinkedBlockingQueue<>());
47+
final AsyncTask<Object> task = mock(AsyncTask.class);
48+
final IOException exception = new IOException();
49+
when(task.call()).thenThrow(exception);
50+
service.execute(task);
51+
52+
verify(task, timeout(2000)).onError(eq(exception));
53+
54+
final InOrder inOrder = inOrder(task);
55+
inOrder.verify(task, times(1)).onPreCall();
56+
inOrder.verify(task, times(1)).call();
57+
inOrder.verify(task, times(1)).onError(exception);
58+
59+
verifyNoMoreInteractions(task);
60+
}
61+
62+
@Test
63+
public void testPreCallException() throws Exception {
64+
final AsynchronousService service = new AsynchronousService(new LinkedBlockingQueue<>());
65+
final AsyncTask<Object> task = mock(AsyncTask.class);
66+
final IllegalStateException exception = new IllegalStateException();
67+
doThrow(exception).when(task).onPreCall();
68+
service.execute(task);
69+
70+
verify(task, timeout(2000)).onError(eq(exception));
71+
72+
final InOrder inOrder = inOrder(task);
73+
inOrder.verify(task, times(1)).onPreCall();
74+
inOrder.verify(task, times(1)).onError(exception);
75+
76+
verifyNoMoreInteractions(task);
77+
}
78+
79+
}

0 commit comments

Comments
 (0)