Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android测试包中的新服务_Java_Android_Junit_Android Testing_Android Instrumentation - Fatal编程技术网

Java Android测试包中的新服务

Java Android测试包中的新服务,java,android,junit,android-testing,android-instrumentation,Java,Android,Junit,Android Testing,Android Instrumentation,我想在一个框架中测试一个类,该框架基于意图启动不同的服务。但是,在运行已连接的android测试时,我在androidTest/内部创建TestService时遇到问题。getService方法返回null 提前感谢您的指导和帮助 @RunWith(AndroidJUnit4.class) public class WakefulIntentSenderTest { private static final String SOME_ACTION = "someAction";

我想在一个框架中测试一个类,该框架基于意图启动不同的服务。但是,在运行已连接的android测试时,我在
androidTest/
内部创建
TestService
时遇到问题。getService方法返回
null

提前感谢您的指导和帮助

@RunWith(AndroidJUnit4.class)
public class WakefulIntentSenderTest {
    private static final String SOME_ACTION = "someAction";

    private static class TestService extends Service {
        private boolean mCalled;

        @Override
        public void onCreate() {
            super.onCreate();
        }

        @Override
        public IBinder onBind(final Intent intent) {
            return null;
        }

        @Override
        public int onStartCommand(final Intent intent, final int flags, final int startId) {
            mCalled = true;
            return 0;
        }

        public boolean wasCalled() {
            return mCalled;
        }

        public class TestServiceBinder extends Binder {

        public TestService getService() {
            return TestService.this;
        }
    }

    @Test
    public void testWithBoundService() throws TimeoutException {
        // Create the service Intent.
        Intent serviceIntent =
                new Intent(InstrumentationRegistry.getTargetContext(), TestService.class);

        InstrumentationRegistry.getTargetContext().startService(intent);

        // Bind the service and grab a reference to the binder.
        IBinder binder = mServiceRule.bindService(serviceIntent);

        // Get the reference to the service, or you can call public methods on the binder directly.
        TestService service = ((TestService.TestServiceBinder) binder).getService();

        // Verify that the service is working correctly.
        assertEquals(service.wasCalled(), true);
    }
}
我还有其他问题,TestService是在“Test”包中真正创建的。如果我试图通过应用程序上下文启动TestService,它会给我一个错误,告诉我
无法启动服务意图{cmp=com.example.abc.test/com.example.abc.WakefulIntentSetUnderTest$TestService}U=0:未找到
错误

上面的代码只是为了演示我是否可以启动服务

更多信息。。。当调用
getTargetContext()
时,InstrumentationRegistry将返回
com.example.abc
,当调用
getContext()
时,将返回
com.example.abc.test

我真正想测试的是
com.example.abc
后面的一个类,它使用
PowerManager
启动带有
Wakelock
的服务。但这是我现在的想法,因为我甚至不能从测试包启动服务

不幸的是,将TestService放在主包中也不是我的选择:(


Android提供了获取服务引用的直接方法。

您需要在TestService#onBind中返回TestServiceBinder类的实例

我认为您需要创建一个测试构建变体(而不仅仅是普通的
androidTest
)使用单独的
AndroidManifest.xml
声明此技术的测试服务。请参阅