使用Dagger,Robolectric和Mockito对CookieManager进行单元测试

时间:2015-07-03 10:21:24

标签: android cookies dependency-injection robolectric dagger

我正在尝试测试我在Cookie管理器中保存的Cookie。出于某种原因,由于an issue in Robolectric,我无法使其发挥作用。所以我决定尝试匕首。

这是我的应用模块应用程序:

public class TSApplication extends Application {

    private static Context mContext;

    private ObjectGraph mObjectGraph;

    @Override
    public void onCreate() {
        super.onCreate();

        mContext = this;

        mObjectGraph = ObjectGraph.create(getModules().toArray());
    }

    public <T> T inject(T object) {
        return mObjectGraph.inject(object);
    }

    protected List<Object> getModules() {
        List<Object> modules = new ArrayList();
        modules.add(new TagHandlerModule());
        return modules;
    }

    public static Context getContext() {
        return mContext;
    }
}

这是我的模块:

@Module(injects = TagHandler.class)
public class TagHandlerModule {

    @Provides
    @Singleton
    CookieManager provideCookieManager() {
        return CookieManager.getInstance();
    }
}

所以我会注射它并使用如下:

@Inject
    CookieManager mCookieManager;

public TagHandler() {
    manager.setCookie(DOMAIN0, "");
            manager.setCookie(DOMAIN1, "");
            CookieStore cookieStore = new BasicCookieStore();
            for (Cookie cookie : cookies) {
                String cookieValue = getCookieDetailString(cookie);
                manager.setCookie(cookie.getDomain(), cookieValue);

                if (cookieIsCurrentDomain(cookie, urlBase)) {
                    cookieStore.addCookie(cookie);
                }
            }
            manager.setAcceptCookie(true);

            CookieSyncManager.getInstance().sync();
}

然后在我的测试文件夹中(在test / java下),我扩展了自定义的应用程序类:

Public class TestTSApplication extends TSApplication {

    @Override
    protected List<Object> getModules() {
        List<Object> modules = super.getModules();
        modules.add(new TagHandlerTestModule());
        return modules;
    }

    public static <T> T injectMocks(T object) {
        TestTSApplication app = (TestTSApplication) RuntimeEnvironment.application;
        return app.inject(object);
    }
}

以及跟随testModule:

@Module(injects = {MyTest.class},
        includes = TagHandlerModule.class,
        overrides = true)
public class TagHandlerTestModule {

    @Provides
    @Singleton
    CookieManager provideMockCookieManager() {
        return mock(CookieManager.class);
    }
}

在MyTest课程中,我有跟踪测试用例:

@Mock
    Context fakeContext;

    @Inject
    CookieManager cookieManager;

    @Before
    public void setup() {
        initMocks(this);
        injectMocks(this);
        try {
            final FileInputStream fileInputStream = new FileInputStream("src/test/cookie_key_json");
            final Context mockContext = mock(Context.class);

            when(fakeContext.createPackageContext(PACKAGE_NAME, 0)).thenReturn(mockContext);

            when(mockContext.openFileInput(FILE_NAME)).thenReturn(fileInputStream);

        } catch (FileNotFoundException e) {
        } catch (PackageManager.NameNotFoundException e) {
        }
    }

    @Test
    public void test1() {
        TaggingFramework.createInstance(fakeContext,
                CPID,
                "TaggingFrameworkTest");

        CookieManager cookieManager = this.cookieManager;

        String str = cookieManager.getCookie(DOMAIN1);

        assertNotNull(str);
    }

由于某种原因,str为空但不应该,因为在设置中我向其添加了三个域,而DOMAIN1就是其中之一。

这些是我的依赖:

dependencies {
    testCompile("junit:junit:${junitVersion}")
    testCompile("org.robolectric:robolectric:${robolectricVersion}")
    testCompile("org.mockito:mockito-core:${mockitoVersion}")
    testCompile('com.squareup.assertj:assertj-android:1.0.0') {
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    compile("com.squareup.dagger:dagger:${daggerVersion}")
    compile("com.squareup.dagger:dagger-compiler:${daggerVersion}")
}

有没有人能给我一些提示?

0 个答案:

没有答案