質問

I am having a problem related to Roboguice and JUNIT. Actually I am doing a very little test to make it run, code:

import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import roboguice.RoboGuice;
import android.app.Application;
import android.content.Context;

public class FansworldClientTest {
    TaskClient client;
    @Mock
    Application application;
    @Mock
    Context context;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        client = RoboGuice.getInjector(application).getInstance(TaskClient.class);
    }
    @Test
    public void getTimestampTest() {
        Assert.assertNotNull(application);
        Assert.assertNotNull(context);
        Assert.assertNotNull(client);
    }
    @After
    public void teardown() {
        RoboGuice.util.reset();
    }
}

And the mvn test command throws me this:

java.lang.NullPointerException: null at roboguice.RoboGuice.setBaseApplicationInjector(RoboGuice.java:116) at roboguice.RoboGuice.getBaseApplicationInjector(RoboGuice.java:59) at com.app.api.client.TaskClientTest.setup(TaskClientTest.java:38)

I am using Roboguice 2.0, Junit 4.8.2 and Mockito 1.9.5. I am also trying doing this:

MockitoAnnotations.initMocks(this);
RoboGuice.setBaseApplicationInjector(application, RoboGuice.DEFAULT_STAGE);
RoboGuice.getInjector(application).getInstance(FansworldClient.class);

And same result. This is taken from the same Astroboy2Test.class of official Roboguice project.

THANKS!!

役に立ちましたか?

解決

You cannot use mocks in conjunction with Roboguice, it's better to let Robolectric provide the context you need here.

There is an excellent tutorial how to set up all this from Moritz Post:

http://eclipsesource.com/blogs/2012/09/25/advanced-android-testing-with-roboguice-and-robolectric/

Based on this work I posted an article a while ago how to even use mocks in functional tests:

http://www.thomaskeller.biz/blog/2012/10/18/runtime-replace-implementations-with-roboguice-in-functional-tests/

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top