【问题标题】:Unit Test Android Service单元测试 Android 服务
【发布时间】:2015-11-25 19:21:05
【问题描述】:

我正在尝试使用“ServiceTestCase”框架测试我的服务,如下所示:

@RunWith(AndroidJUnit4.class)
public class testTts extends ServiceTestCase<TtsLbEngine> {

    public testTts() {
        super(TtsLbEngine.class);
    }

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

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }


    @Test
    public void testOnCreate() throws Exception {
        Intent intent = new Intent(getSystemContext(), TtsLbEngine.class);
        intent.setClass(getSystemContext(), TtsLbEngine.class);
        startService(intent);
        assertNotNull(getService());

    }
}

问题是服务是Null。 能否请教。

【问题讨论】:

    标签: android unit-testing junit4


    【解决方案1】:

    我设法解决了这个问题。 请在下面找到如何做到这一点:

    public class testService extends ServiceTestCase<myService> {
    
    
        public testService() {
            super(myService.class);
        }
    
        @Before
        public void setUp() throws Exception {
            super.setUp();
        }
    
        @After
        public void tearDown() throws Exception {
            super.tearDown();
        }
    
    
        @Test
        public void testService() throws Exception {
            Intent startIntent = new Intent();
    
            startIntent.setClass(getContext(), myService.class);
            startService(startIntent);
    
            myService s = getService();
    
            assertNull(s);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-10
      • 2011-02-03
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多