1717
1818import static org .mockito .Mockito .mock ;
1919import static org .mockito .Mockito .verify ;
20+ import static org .fest .reflect .core .Reflection .staticField ;
2021
2122import java .lang .reflect .Field ;
2223import java .util .ArrayList ;
2324import java .util .Collections ;
2425import java .util .List ;
2526import java .util .Random ;
2627import java .util .concurrent .Executor ;
28+ import java .util .concurrent .ExecutorService ;
2729import java .util .concurrent .Executors ;
2830import java .util .concurrent .Semaphore ;
2931import java .util .concurrent .TimeUnit ;
3032
3133import org .androidannotations .api .BackgroundExecutor ;
34+ import org .fest .reflect .reference .TypeRef ;
3235import org .junit .After ;
3336import org .junit .Assert ;
3437import org .junit .Before ;
3538import org .junit .Test ;
3639import org .junit .runner .RunWith ;
3740import org .mockito .Mockito ;
41+ import org .mockito .internal .util .MockUtil ;
3842import org .robolectric .Robolectric ;
3943import org .robolectric .RobolectricTestRunner ;
4044
@@ -59,8 +63,41 @@ public void setup() {
5963 }
6064
6165 @ After
62- public void after () {
66+ public void after () throws InterruptedException {
6367 Thread .setDefaultUncaughtExceptionHandler (defaultExceptionHandler );
68+
69+ List <String > tasks = staticField ("tasks" ) //
70+ .ofType (new TypeRef <List <String >>() {
71+ }) //
72+ .in (BackgroundExecutor .class ) //
73+ .get ();
74+
75+ tasks .clear ();
76+
77+ ThreadLocal <String > currentSerial = staticField ("currentSerial" ) //
78+ .ofType (new TypeRef <ThreadLocal <String >>() {
79+ }) //
80+ .in (BackgroundExecutor .class ) //
81+ .get ();
82+
83+ currentSerial .set (null );
84+
85+ Executor executor = staticField ("executor" ) //
86+ .ofType (Executor .class ) //
87+ .in (BackgroundExecutor .class ) //
88+ .get ();
89+
90+ if (new MockUtil ().isMock (executor ) || !(executor instanceof ExecutorService )) {
91+ return ;
92+ }
93+
94+ ExecutorService service = (ExecutorService ) executor ;
95+
96+ service .shutdownNow ();
97+ boolean termination = service .awaitTermination (MAX_WAITING_TIME , TimeUnit .MILLISECONDS );
98+ if (!termination ) {
99+ throw new IllegalStateException ("The Executor could not terminate after the test!" );
100+ }
64101 }
65102
66103 @ Test
0 commit comments