java.lang.IllegalStateException: System services not available to Activities before onCreate()

732 views
Skip to first unread message

Binapani Beria

unread,
Jun 15, 2018, 11:14:46 AM6/15/18
to Robolectric
I have a inner class which extends AsyncTask. In the inner class, I have a method to check whether a particular service is running or not. I want to unit test this function "isMobiControlRunning()". I am not able to do it as I do not know how to shadow the inner class and it's method. 

Regards,
Binapani



Code:
_________________________________________________________________________
protected boolean isMobiControlRunning() {
String collectNamesOfAllRunningServicesRelatedToMobiControl = "";
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo runningService : activityManager.getRunningServices(Integer.MAX_VALUE)) {
String runningServiceName = runningService.service.getClassName();
if (runningServiceName.contains("mobicontrol")) {
collectNamesOfAllRunningServicesRelatedToMobiControl = collectNamesOfAllRunningServicesRelatedToMobiControl+runningServiceName;
}
}
if(collectNamesOfAllRunningServicesRelatedToMobiControl == "")
return false;
return true;
}

Error: 
__________________________________________________________________________
java.lang.IllegalStateException: System services not available to Activities before onCreate()

at android.app.Activity.getSystemService(Activity.java:5915)
at com.tesco.monitoringapplication.StartApplication$MonitorMobiControl.isMobiControlRunning(StartApplication.java:135)
at com.tesco.monitoringapplication.StartApplicationUnitTest.checkOutputOfFunctionIsMobiControlRunningWhenItIsNotRunning(StartApplicationUnitTest.java:126)
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.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:570)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:253)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:130)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:42)
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.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:84)
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:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

Reply all
Reply to author
Forward
0 new messages