5

Androidで有効なjunitテストスイートを構築しようとしています。

私はJunitを初めて使用するため、ServiceTestCaseクラスの使用方法がわかりません。

getService()メソッドを機能させる方法がわかりません。nullを返します。だから私はstartServiceを介してそれを開始することにしました。それは動作しません。

手伝っていただけませんか ?

ありがとう

4

2 に答える 2

16

これはあなたがあなたのサービスをテストするために必要なものです

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を確認して ください

于 2010-02-19T23:53:55.010 に答える
0

受け入れられた答えはもう機能しません。

ActivityInstrumentationTestCase2やServiceTestCaseなどのTestCaseは廃止され、ActivityTestRuleまたはServiceTestRuleが優先されます。

ATSLリンク

彼らは実際のドキュメントを更新するのを忘れたようです。

于 2016-06-03T22:11:35.303 に答える