This one is a known annoying 2 year old Android bug.
My question is: does anyone know of a workaround for this problem besides changing the Android source code and compiling it again?
Here is my code to complete:
A subclass method of My Service that raises NPE:
private void doStartForeground() {
This is called from overriding the onCreate() method.
And the JUnit testing method:
public void test1() throws InterruptedException { assertTrue(context != null); final Intent service = new Intent(); service.setComponent(new ComponentName(CoreService.PACKAGE_NAME, CoreService.SERVICE_FULL_NAME)); IBinder binder = bindService(service); assertTrue(binder != null); }
Stack trace:
java.lang.NullPointerException at android.app.Service.startForeground(Service.java:631) at com.blablabla.android.core.CoreService.doStartForeground(CoreService.java:82) at com.blablabla.android.core.CoreService.onCreate(CoreService.java:149) at android.test.ServiceTestCase.bindService(ServiceTestCase.java:234) at com.blablabla.android.core.test.MainTest.test1(MainTest.java:37) at java.lang.reflect.Method.invokeNative(Native Method) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:537) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1551)
The service starts correctly and works fine when it does not start through Android JUnit.
m0skit0
source share