gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:03:06 25 4
gpt4 key购买 nike

我有一个复杂的项目,其中包含很少的风格和项目依赖项目。如果您认为 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$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.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'
}

最佳答案

好吧,我从我的一位老板那里学到的一件事是“当你在做一个非常大的项目并且面临一个你无法(轻松)修复它的错误/问题/问题时,尝试在为了复制问题并先在那里修复它”。有了这段经历,我创建了一个新项目并尝试重现这个问题。但是,我很高兴地说,它工作正常。然后我只是比较和修复我的主要项目。

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


更新

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

关于java - 如何在具有 android.support.v7.widget.CardView 的情况下使用 robolectric 测试 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31175076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com