ServiceTestCase< T> .getService?LT、ServiceTestCase、getService、GT

2023-09-05 01:07:26 作者:闹够了就滚

我试图建立一个有效的JUnit测试套件的机器人。

I'm trying to build a valid junit testing suite on android.

由于我是新来的Junit我无法弄清楚如何使用ServiceTestCase类。

Since i'm new to Junit i can't figure out how to use the ServiceTestCase class.

我无法弄清楚如何获得的getService()方法的工作。它曾经返回我空。所以我决定通过startService启动它。这不可行。

I can't figure it out how to get the getService() method working. it ever returns me null . So i decided to start it via startService. It does not work.

你能帮帮我吗?

感谢

推荐答案

这就是你需要测试你的服务

This is what you need to test your service

public class MyServiceTests extends ServiceTestCase<MyService> {

private static final String TAG = "MyServiceTests";

public MyServiceTests() {
    super(MyService.class);
}

/**
 * Test basic startup/shutdown of Service
 */
@SmallTest
public void testStartable() {
    Intent startIntent = new Intent();
    startIntent.setClass(getContext(), MyService.class);
    startService(startIntent);
    assertNotNull(getService());
}

/**
 * Test binding to service
 */
@MediumTest
public void testBindable() {
    Intent startIntent = new Intent();
    startIntent.setClass(getContext(), MyService.class);
    IBinder service = bindService(startIntent);
    assertNotNull(service);
}
}

我已经写了一些文章关于Android的测试和测试驱动开发,你会发现有用的,检查http://dtmilano.blogspot.com/search/label/test%20driven%20development.