我有一个复杂的项目,包含很少的风格和项目依赖项目。如果您认为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'
}
答案 0 :(得分:1)
好吧,我从我的一位老板那里学到的一件事是“当你在一个非常大的项目上工作并遇到一个你无法解决的错误/问题/问题时(轻松),尝试创建一个新的项目为了复制问题并在那里修复它“。 在我的脑海中有这样的经历,我创建了一个新项目并尝试复制该问题。但是,我很高兴地说,它工作正常。然后我只是比较和修复我的主要项目。
对于那些有同样问题的人,我已经将我的示例项目推到了这个仓库:https://github.com/Hesamedin/RobolectricTester/tree/master/app/src/test
希望它有所帮助。
==============
我发现当我有一个项目和味道时没有问题。但是,当我有口味时会出现问题。以下页面帮助我解决了我的问题。请看一下这些页面(特别是第二页):