java - How to test an activity with robolectric while it has android.support.v7.widget.CardView? -
i have complex project contains few flavors , project dependency projects. if consider my_project project, contains project_1 main project, project_2 , project_3 acting dependency project_1. i've put tests under project_2/src/test/java
.
i'm trying write few simple methods test splash screen based on.
this code 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 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 invisible default.", cardview); } }
this output when run ./gradlew test
command:
com.something.splash.splashactivitytest > checkactivitynotnull failed android.view.inflateexception @ splashactivitytest.java:34 (marked <=== above) caused by: java.lang.reflect.invocationtargetexception @ splashactivitytest.java:34 caused by: java.lang.noclassdeffounderror @ splashactivitytest.java:34 caused by: java.lang.classnotfoundexception @ splashactivitytest.java:34 com.something.splash.splashactivitytest > testme failed android.view.inflateexception @ splashactivitytest.java:34 caused by: java.lang.reflect.invocationtargetexception @ splashactivitytest.java:34 caused by: java.lang.noclassdeffounderror @ splashactivitytest.java:34 com.something.splash.splashactivitytest > checkvisibilityofcardview failed android.view.inflateexception @ splashactivitytest.java:34 caused by: java.lang.reflect.invocationtargetexception @ splashactivitytest.java:34 caused by: java.lang.noclassdeffounderror @ splashactivitytest.java:34 3 tests completed, 3 failed :project_2:testdebug failed failure: build failed exception. * went wrong: execution failed task ':project_2:testdebug'. > there failing tests. see report at: file:///users/admin/desktop/android/my_folder/project_2/build/reports/tests/debug/index.html * try: run --stacktrace option stack trace. run --info or --debug option more log output. build failed total time: 14.546 secs
and index.html
shows these details:
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 @ android.view.layoutinflater.createview(layoutinflater.java:620) @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:696) @ android.view.layoutinflater.rinflate(layoutinflater.java:755) . . . caused by: java.lang.classnotfoundexception: android.support.v7.cardview.r$styleable @ java.net.urlclassloader$1.run(urlclassloader.java:372) @ java.net.urlclassloader$1.run(urlclassloader.java:361) . . .
sorry long explanation guess mandatory. spent day , still couldn't figure out problem , why f.cardview cannot recognized while application works fine when build project. idea appreciated. thanks.
i have these lines in project_2
gradle file:
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, thing learnt 1 of bosses "when working on big project , face bug/problem/issue cannot fix (easily), try create new project in order replicate issue , fix on there first". having experience in mind, created new project , tried replicate issue. however, i'm happy that, it's working fine. compare , fix main project.
for how have same problem, have pushed sample project @ repo: https://github.com/hesamedin/robolectrictester/tree/master/app/src/test
hope helps.
==============
update
i found there no problem when have project , flavor. however, problem occurs when have flavors. following pages helped me in order fix issue. please have @ these pages (second 1 particularly):
Comments
Post a Comment