2013-06-09 8 views
10

In meiner Android-Anwendung habe ich Volley eingerichtet.NPE, wenn Robolectric ShadowApplication mit Volley und Dolch

Robolectric.application wird initialisiert und alle anderen Tests laufen reibungslos. Ich bekomme diesen Fehler, wenn ich versuche, eine verspottete HTTP-Antwort zu erhalten.

Dies ist mein Test:

@RunWith(MyRobolectricTestRunner.class) 
public class ApiTests { 

    @Inject 
    protected Api api; 

    @Before 
    public void setUp() { 
     ObjectGraph.create(new AndroidModule(Robolectric.application), new TestApplicationModule()).inject(this); 
    } 

    @Test 
    public void shouldGetErrorList() throws Exception { 
     Project project = new Project("test", "test", "test", DateTime.now()); 
     addPendingProjectsErrorsResponse("response.json"); //adding response to FakeHttpLayer 

     api.getProjectErrors(project, new Listener<ProjectErrors>() { 
       @Override 
       public void onResponse(ProjectErrors response) { 
        assertNotNull(response); 
       } 
      }, new ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        throw new RuntimeException(error); 
       } 
      } 
     ); 
    } 
} 

Dieser Fehler ist erhalte ich:

Exception in thread "Thread-3" java.lang.NullPointerException 
    at org.robolectric.shadows.ShadowLooper.getMainLooper(ShadowLooper.java:59) 
    at android.os.Looper.getMainLooper(Looper.java) 
    at org.robolectric.Robolectric.getUiThreadScheduler(Robolectric.java:1301) 
    at org.robolectric.shadows.ShadowSystemClock.now(ShadowSystemClock.java:15) 
    at org.robolectric.shadows.ShadowSystemClock.uptimeMillis(ShadowSystemClock.java:25) 
    at org.robolectric.shadows.ShadowSystemClock.elapsedRealtime(ShadowSystemClock.java:30) 
    at android.os.SystemClock.elapsedRealtime(SystemClock.java) 
    at com.android.volley.VolleyLog$MarkerLog.add(VolleyLog.java:114) 
    at com.android.volley.Request.addMarker(Request.java:174) 
    at com.android.volley.CacheDispatcher.run(CacheDispatcher.java:92) 

Antwort

-2

ich denselben Fehler hatte und vermeiden es durch meine eigenen (und hässlich) Systemuhr Schatten verwenden.

Schatten Klasse:

@Implements(value = SystemClock.class, callThroughByDefault = true) 
public static class MyShadowSystemClock { 
    public static long elapsedRealtime() { 
     return 0; 
    } 
} 

Testcode:

@Test 
@Config(shadows = { MyShadowSystemClock.class, ... }) 
public void myTest() { 
} 
-2

Eine andere Lösung wäre Volley-Protokollierung zu deaktivieren, indem

VolleyLog.DEBUG = false; 

in Ihrem setUp Methode aufrufen.

Verwandte Themen