2013-09-23 22 views
9

Sto riscontrando problemi con il codice Handler con Robolectric. Ad esempio:Robolectric Run Handler post

Come si esegue il codice nel Runnable immediatamente? Non viene eseguito prima dell'esecuzione del test.

+0

Hai chiamato Client.doSomeStuff() sul thread principale o thread in background? – Poly

risposta

8

credo che questo dovrebbe fare un lavoro:

Robolectric.runUiThreadTasks(); 

o se ci diversi compiti previsti:

Robolectric.runUiThreadTasksIncludingDelayedTasks(); 
+0

** reference runUiThreadTasks non risolto ** –

11

Per Robolectric versione 3.0 si dovrebbe usare: org.robolectric.Robolectric.flushForegroundThreadScheduler
o
org.robolectric.shadows.ShadowLooper.runUiThreadTasks
org.robolectric.shadows.ShadowLooper.runUiThreadTasksIncludingDelayedTasks

Riferimento: 2.4 to 3.0 Upgrade Guide

+0

Impossibile risolvere il simbolo ** ShadowLooper ** –

3

In Robolectrie 3.0 si può fare

HandlerThread thread = new HandlerThread("test"); 
thread.start(); 
Handler handler = new Handler(thread.getLooper()); 
handler.post(new Runnable() {run(){ 
    int a = 0; 
}}; 
((ShadowLooper) ShadowExtractor.extract(thread.getLooper())).idle(); // this will execute line int a = 0;