Reputation: 128
I have project in Android Studio which was created in Eclipse. I migrated it to Gradle and added test. For test I use Robolectric. When I tested library modules in my project everything was ok, but when i start testing application module and call BuilActivity(MyActivity.class).create().get()
console prints me that error:
java.lang.NullPointerException
at org.robolectric.res.builder.DefaultPackageManager.getActivityInfo(DefaultPackageManager.java:173)
at org.robolectric.util.ActivityController.getActivityInfo(ActivityController.java:65)
at org.robolectric.util.ActivityController.attach(ActivityController.java:51)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:121)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:304)
at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:45)
at org.robolectric.util.ActivityController.create(ActivityController.java:118)
at org.robolectric.util.ActivityController.create(ActivityController.java:129)
at test(test.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:251)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
I don't know what cause this error. Anybody have something like that?
EDIT:
In project i use 7 modules. When i start test one module without activite everything works fine, but when i started test module with activity i've got above error stack trace.
Activity
In activity, fragment with viewpager is attached and activity starts data download using volley. This activity extends other class which extends another one, which blah blah blah...
buil.gradle
apply plugin: 'com.android.application'
dependencies {
compile project(':proj-core')
compile project(':proj-gui-base')
testCompile "org.robolectric:robolectric:3.0"
testCompile "org.robolectric:shadows-support-v4:3.0"
testCompile 'junit:junit:4.12'
}
android {
compileSdkVersion 19
buildToolsVersion "23.0.0"
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src', 'src/main/java']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
test {
resources.srcDirs = ['test']
java.srcDirs = ['test', 'test/src/java']
}
robolectric.java.srcDir file('test/src/java')
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Tests
Problem starts in @Before method
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)
public class Test {
Context context;
ShadowApplication shadowApplication;
HomeActivity homeActivity;
@Before
public void setUp() {
RuntimeEnvironment.application.onCreate();
context = RuntimeEnvironment.application.getApplicationContext();
shadowApplication = Shadows.shadowOf(RuntimeEnvironment.application);
homeActivity = Robolectric.setupActivity(HomeActivity.class);
}
@Test
public void test_core_base_popup_popup() {
Popup popup = new Popup(Popup.Type.GENERIC);
PopupQueueManager.addPopup(homeActivity, popup);
PopupQueueManager.displayAllPopups(homeActivity);
assertEquals(popup.getContent(), "xyz");
}
}
Project Structure
I describe only one of 7 modules. Every module have same structure.
proj-android
|
|-proj-core-base
|-assets
|-res
|-src
|- com.package
|-test
|-java
|-Test.java
|-next-module
|-next-next-module
|-etc
Upvotes: 3
Views: 1713
Reputation: 1444
I’m using Android Studio 3.1.1 and also got a NullPointerException when calling Robolectric.buildActivity(…).create().get(). Then I found the following link:
http://robolectric.org/getting-started/
where it is recommended to add the following block to your app’s build.gradle file:
android {
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
With this addition the exception was avoided. I’m not sure if this solution works for your case. Just sharing.
Upvotes: 1
Reputation: 178
I guess you're using an incompatible robolectric and android SDK versions:
With Robolectric 3, you should use :
MyActivity activity = Robolectric.setupActivity(MyActivity.class);
Upvotes: 0