Android ServiceTestCase for IntentService

发布于 2024-11-19 16:32:00 字数 633 浏览 3 评论 0原文

我目前正在为 Android 应用程序编写单元测试,并偶然发现了以下问题:

我使用 ServiceTestCase 来测试 IntentService ,如下所示:

@Override
public void setUp() throws Exception {
    super.setUp();      
}

public void testService()
{
    Intent intent = new Intent(getSystemContext(), MyIntentService.class);
    super.startService(intent);     
    assertNotNull(getService());        
}

但是我注意到我的 IntentService 已创建(意味着 onCreate 被调用),但我从未收到对 onHandleIntent(Intent Intent) 的调用

有没有人已经测试过IntentServiceServiceTestCase 类?

谢谢!

I'm currently writing unit tests for an android application and stumbled into the following issue:

I use the ServiceTestCase to test an IntentService like this:

@Override
public void setUp() throws Exception {
    super.setUp();      
}

public void testService()
{
    Intent intent = new Intent(getSystemContext(), MyIntentService.class);
    super.startService(intent);     
    assertNotNull(getService());        
}

However I noticed that my IntentService is created (means that onCreate is called) but I never receive a call into onHandleIntent(Intent intent)

Has anyone already tested an IntentService with the ServiceTestCase class?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

緦唸λ蓇 2024-11-26 16:32:00

这有点晚了,但我只是为此苦苦挣扎。您可以通过创建一个类来简单地覆盖服务的 onStart 来解决此问题,以便它直接调用 onHandleIntent 。因此,举例来说,如果您有一个 LocationUpdaterService,您可以创建一个假类来重写 onStart 函数,如下所示:

public class LocationUpdaterServiceFake extends LocationUpdaterService {

@Override
public void onStart(Intent intent, int startId) {
    onHandleIntent(intent);
    stopSelf(startId);
}

LocationUpdaterService 是 IntentService 的子类,因此当您编写测试,只需使用像这样的 LocationUpdaterServiceFake

public class LocationUpdateServiceTest extends ServiceTestCase<LocationUpdaterServiceFake> {

public LocationUpdateServiceTest()
{
    super(LocationUpdaterServiceFake.class);
}

public void testNewAreaNullLocation()
{
    Intent intent = new Intent();
    intent.setAction(LocationUpdaterService.ACTION_NEW_AREA);

    startService(intent);
}

}

现在,每当您调用 startService 时,它都会绕过 IntentService 中的线程代码,只调用您的onHandleIntent函数

This is a bit late, but I just struggled with this. You could solve this by creating a class that simply overrides the onStart of you service so it calls onHandleIntent directly. So for instance, if you have a LocationUpdaterService, you could create a fake class that overrides the onStart function like this:

public class LocationUpdaterServiceFake extends LocationUpdaterService {

@Override
public void onStart(Intent intent, int startId) {
    onHandleIntent(intent);
    stopSelf(startId);
}

LocationUpdaterService is a subclass of IntentService, so when you write your tests, just use the LocationUpdaterServiceFake class like this

public class LocationUpdateServiceTest extends ServiceTestCase<LocationUpdaterServiceFake> {

public LocationUpdateServiceTest()
{
    super(LocationUpdaterServiceFake.class);
}

public void testNewAreaNullLocation()
{
    Intent intent = new Intent();
    intent.setAction(LocationUpdaterService.ACTION_NEW_AREA);

    startService(intent);
}

}

Now whenever you call startService, it will bypass the threading code in IntentService and just call your onHandleIntent function

最初的梦 2024-11-26 16:32:00

我刚刚开始测试自己的 IntentService ,事实证明这有点令人头疼。

仍在尝试解决问题,但对于您似乎没有收到对方法 onHandleIntent() 的调用的情况,(我不太了解 junit< 背后的技术细节/code> 所以请原谅我使用的术语)这应该是因为一旦您对 startService 的调用返回,基于您的代码的测试框架实际上会拆除或结束测试方法。没有足够的时间来触发 onHandleIntent

我通过在测试用例中添加无限循环来验证上述理论 - 只有这样我才能在 onHandleIntent 中看到记录的日志语句。

I just got started into testing my own IntentService and it's proving to be a bit of a headache.

Still trying to work things out but for the scenario where it seems that you do not receive a call to your method onHandleIntent(), (I'm not very good with the technicalities behind junit so forgive my use of terminology) it should be because the test framework, based on your code, actually tears down or end the test method once your call to startService returns. There is insufficient time for onHandleIntent to be triggered.

I verified the above theory by adding an infinite loop within my test case - only then can I see my log statements in onHandleIntent logged.

仙女山的月亮 2024-11-26 16:32:00

你只需要添加一个:

Thread.sleep(XXXXXXX); 

选择startService后面的XXXX,然后它会让线程进入onHandleIntent方法。

You just have to add a:

Thread.sleep(XXXXXXX); 

Choose the XXXX after the startService, then it will let the thread go into the onHandleIntent method.

深空失忆 2024-11-26 16:32:00

在 Android Studio 1.1 中,使用运行/调试配置运行测试时 |扩展 IntentService 的任何被测单元代码 (UUT) 上的 Android 测试工具,ServiceTestCase.java (JUnit?) 代码不会调用 UUT 中的 onHandleIntent(Intent Intent) 方法。 ServiceTestCase仅调用onCreate,因此问题出在测试代码中。

protected void startService(Intent intent) {
    if (!mServiceAttached) {
        setupService();
    }
    assertNotNull(mService);

    if (!mServiceCreated) {
        mService.onCreate();
        mServiceCreated = true;
    }
    mService.onStartCommand(intent, 0, mServiceId);

    mServiceStarted = true;
}

在我的文件 smSimulatorTest.java 中:

public class smSimulatorTest extends ServiceTestCase<smSimulator> 

此时,我正在测试框架中寻找通过 Intents 测试 UUT 的其他解决方案,因为这就是 IntentService 的实例化方式。

http://developer.android.com/reference/android/app/IntentService.html - 要使用它,请扩展 IntentService 并实现 onHandleIntent(Intent)。 IntentService 将接收 Intent,启动工作线程,并根据需要停止服务。

我和其他人一样,按照上述文档的指示将代码放入 onHandleintent() 中,但是,ServiceTestCase 仅测试上面显示的 onStart 和 onStartCommand。

In Android Studio 1.1, when running tests using the Run/Debug Configuration | Android Tests facility on any unit under test code (UUT) that extends IntentService, the ServiceTestCase.java (JUnit?) code does not call onHandleIntent(Intent intent) method in the UUT. ServiceTestCase only calls onCreate so the problem is in the test code.

protected void startService(Intent intent) {
    if (!mServiceAttached) {
        setupService();
    }
    assertNotNull(mService);

    if (!mServiceCreated) {
        mService.onCreate();
        mServiceCreated = true;
    }
    mService.onStartCommand(intent, 0, mServiceId);

    mServiceStarted = true;
}

In my file smSimulatorTest.java:

public class smSimulatorTest extends ServiceTestCase<smSimulator> 

At this point, I'm looking for other solutions in the testing framework that test UUTs through Intents since this is how IntentService is instantiated.

http://developer.android.com/reference/android/app/IntentService.html - To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.

I, like others, put my code in the onHandleintent() as directed by the above documentation, however, ServiceTestCase only tests onStart and onStartCommand has shown above.

违心° 2024-11-26 16:32:00

这是我现在的方法:

  1. 调用服务的启动 Intent 指定要测试的 Service 方法

    <前><代码>
    公共无效test_can_do_the_work(){
    意图startIntent = new Intent();
    startIntent.putExtra("IN_TEST_MODE", "TEST_SPECIFIC_METHOD");
    startIntent.setClass(getContext(), MyServiceToTest.class);
    启动服务(启动意图);
    断言NotNull(getService()); // 您的断言服务特定断言
    }

  2. 在服务onStart中,我们检查是否传递了具体的Extra,并调用该方法进行测试。当处理意图触发时,这不会执行。

    <前><代码>
    @覆盖
    公共无效onStart(Intent意图,int startId){
    super.onStart(意图, startId);
    String in_test_mode = Intent.getStringExtra("TEST_SPECIFIC_METHOD");
    如果(in_test_mode!= null){
    doServiceWork();
    }
    }

This is my approach for now:

  1. The start Intent that invokes the service specifies the Service method to test

    
    public void test_can_do_the_work() {
        Intent startIntent = new Intent();
        startIntent.putExtra("IN_TEST_MODE", "TEST_SPECIFIC_METHOD");
        startIntent.setClass(getContext(), MyServiceToTest.class);
        startService(startIntent);
        assertNotNull(getService()); // Your assertion Service specific assertion
    }
    
  2. In the service onStart, we check for the specific Extra passed and call the method to test. This won't execute when Handle intent fired.

    
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        String in_test_mode = intent.getStringExtra("TEST_SPECIFIC_METHOD");
        if(in_test_mode != null){
            doServiceWork();
        }
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文