如何在 android.support.v7.widget.CardView 的情况下用 robolectric 测试 activity?

How to test an activity with robolectric while it has android.support.v7.widget.CardView?

我有一个复杂的项目,其中包含很少的风格和项目依赖项目。如果您认为 MY_PROJECT 是项目,它包含 PROJECT_1 作为主项目,PROJECT_2 和 PROJECT_3 作为 PROJECT_1 的依赖项。我已将测试放在 PROJECT_2/src/test/java 下。

我正在尝试编写一些简单的方法来测试我的启动画面基于。

这是我写的代码:

package com.something.splash;

import android.os.Build;
import android.view.View;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import com.FLAVOUR_1.BuildConfig;
import com.FLAVOUR_1.R;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.KITKAT, manifest = "../PROJECT_1/src/main/AndroidManifest.xml")
public class SplashActivityTest
{
    private SplashActivity mSplashActivity;

    @Test
    public void testMe() {
        assertTrue(1 > 0);
    }

    @Before
    public void setup()  {
        this.mSplashActivity = Robolectric.buildActivity(SplashActivity.class).create().get();  <==== PROBLEM
    }

    @Test
    public void checkActivityNotNull() throws Exception {
        assertNotNull("How come SplashActivity is null???", this.mSplashActivity);
    }

    @Test
    public void checkVisibilityOfCardView() throws Exception {
        View ivBranding = this.mSplashActivity.findViewById(R.id.ivBranding);
        Assert.assertEquals(ivBranding.getVisibility(), View.VISIBLE);

        View cardView = this.mSplashActivity.findViewById(R.id.splash_error_layout);
        assertNull("CardView MUST be invisible by default.", cardView);
    }
}

这是我 运行 ./gradlew test 命令时的输出:

com.SOMETHING.splash.SplashActivityTest > checkActivityNotNull FAILED
    android.view.InflateException at SplashActivityTest.java:34 (MARKED WITH <=== ABOVE)
        Caused by: java.lang.reflect.InvocationTargetException at SplashActivityTest.java:34
            Caused by: java.lang.NoClassDefFoundError at SplashActivityTest.java:34
                Caused by: java.lang.ClassNotFoundException at SplashActivityTest.java:34

com.SOMETHING.splash.SplashActivityTest > testMe FAILED
    android.view.InflateException at SplashActivityTest.java:34
        Caused by: java.lang.reflect.InvocationTargetException at SplashActivityTest.java:34
            Caused by: java.lang.NoClassDefFoundError at SplashActivityTest.java:34

com.SOMETHING.splash.SplashActivityTest > checkVisibilityOfCardView FAILED
    android.view.InflateException at SplashActivityTest.java:34
        Caused by: java.lang.reflect.InvocationTargetException at SplashActivityTest.java:34
            Caused by: java.lang.NoClassDefFoundError at SplashActivityTest.java:34

3 tests completed, 3 failed
:PROJECT_2:testDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':PROJECT_2:testDebug'.
> There were failing tests. See the report at: file:///Users/admin/Desktop/android/MY_FOLDER/PROJECT_2/build/reports/tests/debug/index.html

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 14.546 secs

最后 index.html 显示这些详细信息:

    android.view.InflateException: XML file build/intermediates/res/debug/layout/activity_splash.xml line #-1 (sorry, not yet implemented): Error inflating class android.support.v7.widget.CardView
        at android.view.LayoutInflater.createView(LayoutInflater.java:620)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    .
    .
    .
Caused by: java.lang.ClassNotFoundException: android.support.v7.cardview.R$styleable
    at java.net.URLClassLoader.run(URLClassLoader.java:372)
    at java.net.URLClassLoader.run(URLClassLoader.java:361)
    .
    .
    .

抱歉解释得太长了,但我想这是强制性的。我花了一天时间仍然无法弄清楚问题是什么以及为什么在我构建项目时应用程序运行正常时无法识别这个f.cardView。任何想法将不胜感激。谢谢。

我的 PROJECT_2 gradle 文件中有这些行:

apply plugin: 'com.android.library'
android androidConfiguration

android {
    sourceSets {
        main {
            res.srcDirs = ['src/main/res', 'src/main/res-flags']
        }
    }
}

dependencies {        
    ...
    compile 'com.android.support:recyclerview-v7:22.1.1'
    compile 'com.android.support:cardview-v7:22.1.1'
    compile 'com.google.android.gms:play-services-gcm:7.3.0'
    compile 'com.google.android.gms:play-services-maps:7.3.0'

    // Roboelectric testing
    testCompile 'junit:junit:4.12'
    testCompile 'org.robolectric:robolectric:3.0-rc3'
}

好吧,我从我的一位老板那里学到的一件事是 "When you are working on a very big project and face a bug/problem/issue that you cannot fix it (easily), try to create a new project in order to replicate the issue and fix it over there first"。 有了这段经历,我创建了一个新项目并尝试重现这个问题。但是,我很高兴地说,它工作正常。然后我只是比较和修复我的主要项目。

对于那些有同样问题的人,我已经在这个 repo 推送了我的示例项目:https://github.com/Hesamedin/RobolectricTester/tree/master/app/src/test

希望对您有所帮助。

==============

更新

我发现当我有项目有味道的时候是没有问题的。但是,当我有口味时会出现问题。以下页面帮助我解决了我的问题。请查看这些页面(特别是第二页):