简体   繁体   English

如何在具有android.support.v7.widget.CardView的情况下使用robolectric测试活动?

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

I have complex project that contains few flavors and project dependency projects. 我有一个复杂的项目,其中包含很少的口味和项目依赖项目。 If you consider MY_PROJECT is the project, it contains PROJECT_1 as main project, PROJECT_2 and PROJECT_3 are acting as dependency for PROJECT_1. 如果您认为MY_PROJECT是项目,则它包含PROJECT_1作为主项目,PROJECT_2和PROJECT_3充当PROJECT_1的依赖项。 I've put my tests under PROJECT_2/src/test/java . 我将测试放在PROJECT_2/src/test/java

I'm trying to write few simple methods to test my splash screen based on. 我正在尝试编写一些简单的方法来基于其测试我的启动画面。

This is the code I have written: 这是我编写的代码:

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);
    }
}

This is the output when I run ./gradlew test command: 这是我运行./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

and finally index.html shows these details: 最后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$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    .
    .
    .

Sorry for long explanation but I guess it was mandatory. 抱歉,冗长的解释,但我想这是强制性的。 I spent a day and still couldn't figure it out what is the problem and why this f.cardView cannot be recognized while application works fine when I build the project. 我花了一天的时间,仍然无法弄清楚问题出在哪里,为什么在构建项目时应用程序可以正常运行时无法识别此f.cardView。 Any idea would be appreciated. 任何想法将不胜感激。 Thanks. 谢谢。

I have these lines in my PROJECT_2 gradle file: 我的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'
}

Okay, a thing that I learnt from one of my bosses is "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". 好的,我从我的一位老板那里学到的东西是:“当您在处理一个非常大的项目时,遇到无法轻松修复的错误/问题/问题时,请尝试创建一个新项目以进行复制问题,然后先在那儿解决”。 Having this experience in my mind, I created a new project and tried to replicate the issue. 考虑到这种经验,我创建了一个新项目并尝试复制该问题。 However, I'm happy to say that, it's working fine. 但是,我很高兴地说,它运行良好。 Then I just to compare and fix my main project. 然后,我只是比较并修复我的主要项目。

For those how have same problem, I have pushed my sample project at this repo: https://github.com/Hesamedin/RobolectricTester/tree/master/app/src/test 对于那些同样有问题的人,我在此仓库中推送了我的示例项目: https : //github.com/Hesamedin/RobolectricTester/tree/master/app/src/test

Hope it helps. 希望能帮助到你。

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

UPDATE UPDATE

I found that there is no problem when I have a project and a flavor. 我发现有项目和风格时没有问题。 However, problem occurs when I have flavors. 但是,当我有口味时会出现问题。 Following pages helped me in order to fix my issue. 以下页面帮助我解决了我的问题。 Please have a look at these pages (second one particularly): 请查看以下页面(特别是第二页):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有<CardView>标签,类似于<android.support.v7.widget.CardView> ? - Is there a <CardView> tag, similar to the <android.support.v7.widget.CardView>? 没有找到类“android.support.v7.widget.CardView” - Didn't find class “android.support.v7.widget.CardView” 膨胀android.support.v7.widget.CardView会引发错误 - Inflating android.support.v7.widget.CardView throws Error 充气 android.support.v7.widget.CardView 时出错 - Error inflating android.support.v7.widget.CardView 无法将android.support.v7.widget.CardView导入Eclipse - Trouble importing android.support.v7.widget.CardView into Eclipse 错误膨胀类 android.support.v7.widget.CardView - Error inflating class android.support.v7.widget.CardView 如何修复错误膨胀类Android.support.v7.widget.CardView - How to fix Error inflating class Android.support.v7.widget.CardView 如何解决:错误膨胀类android.support.v7.widget.CardView - How to Resolve : Error inflating class android.support.v7.widget.CardView 当我运行应用程序时,android.support.v7.widget.CardView不显示 - android.support.v7.widget.CardView not displaying when I run my application 找不到 android.support.v7.widget.CardView(Android Studio 1.1 预览版) - android.support.v7.widget.CardView could not be found (Android Studio 1.1 Preview)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM