TestProject 中的 Android 上下文

发布于 2024-12-18 21:05:49 字数 943 浏览 0 评论 0原文

我试图使用android的JUnit框架来实现自动化测试场景。我使用 CountDownLatch 因为我想包含一个异步场景。

我已经使用 ServiceTestCase 为我的服务编写了一个测试用例文件。该服务使用线程自动启动异步操作。执行异步操作后我得到结果。我试图断言这个结果。现在,如果操作不成功,我会尝试再次执行异步操作。现在发生的情况是,如果操作不成功,我尝试再次重做该操作,在这种情况下,我得到的上下文为空。

是不是当我从测试用例启动服务时,一段时间后上下文对象变为空?什么时候垃圾被收集?

我正在使用的服务是远程服务。它会造成问题吗?

public class MyServiceTest extends ServiceTestCase<MyService>{
final CountDownLatch signal = new CountDownLatch(1);
Solo solo;
public MyServiceTest() {
    super(MyService.class);

}

public void testEndToEnd(){


    Intent intent = new Intent(MyService.class.getName());
    startService(intent);

    try {
        signal.await(60, TimeUnit.SECONDS);
    }
    catch(InterruptedException e) {
        e.printStackTrace();
    }

//want to put some assert statement
}    

在服务

的 onCreate 中,我实例化一个 Network 类,它将执行繁重的网络操作。如果操作失败发送广播通知。现在我已经注册了一个广播接收器,它将接收通知并重新启动网络操作。在此广播接收器中,我需要即将为空的广播上下文。

I was trying to achieve automation testing scenario using JUnit Framework of android. I used CountDownLatch as I wanted to include one asyncronous scenario.

I have written a testcase file using ServiceTestCase for my service. The service automatically starts an asynchronous operation using a thread. After doing async operation I get the result. This result i am trying to assert. Now if the operation is unsuccessful i try to do the async operation again. Now what is happening is the if operation is unsuccessful i try to redo the operation again in this case i am getting context as null.

Is it that when i start the service from testcase, After some time the context object becomes null? When is it garbage collected?

The service which i am using is a remote service. Is it creating a problem?

public class MyServiceTest extends ServiceTestCase<MyService>{
final CountDownLatch signal = new CountDownLatch(1);
Solo solo;
public MyServiceTest() {
    super(MyService.class);

}

public void testEndToEnd(){


    Intent intent = new Intent(MyService.class.getName());
    startService(intent);

    try {
        signal.await(60, TimeUnit.SECONDS);
    }
    catch(InterruptedException e) {
        e.printStackTrace();
    }

//want to put some assert statement
}    

}

In onCreate of service I am instantiate a Network class which will do heavy network operation. if the operation is failed send a broadcast notification. Now I have registered one BroadcastReceiver which will receive the notification and and restart the network operation. In this broadcastReciever i need the broadcast context which is coming null..

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文