我正在为我的 Android 项目编写一个非常简单的单元测试用例,测试用例简单地执行一个在后台进行网络操作的 AsyncTask。我正在为我的测试用例扩展 AndroidTestCase 类:
public class MyTest extends AndroidTestCase{
//use CountDownLatch to perform wait-notify behavior
private CountDownLatch signal;
@Override
public void setUp() throws Exception{
super.setUp();
//I use CountDownLatch to perform wait-notify behaviour
signal = new CountDownLatch(1);
}
@Override
public void runTest() throws Exception{
String params = "some params";
//MyAsyncTask does the networking tasks
new MyAsyncTask().execute(params);
//wait until MyAsyncTask is done
try {
signal.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private class MyAsyncTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
//Do networking task e.g. access a remote database
}
@Override
protected void onPostExecute(String result){
super.onPostExecute(result);
//this never get called when run this test case, why?
Log.i("Debug","post execute");
signal.countDown();
}
}
}
正如您在上面看到的,我使用的是 CountDownLatch执行等待通知行为。
MyAsicTask 启动后,我调用 signal.await() 来等待 MyAsyncTask 完成。
在 MyAsyncTask 的 onPostExecute() 回调中,我调用 signal.countDown() 来通知任务完成。
但是当我运行这个测试用例时,onPostExecute 从未被调用,为什么以及如何修复它?
========更新==========
在 runTest() 的最后一行添加 Thread.sleep(30*1000) 后,我可以看到更多来自网络操作的日志。它证明 teardown() 在我的网络操作完成之前被调用。我在 tearDown() 中什么也没做,android 测试框架。自动调用它。
似乎我使用 CountDownLatch 的wait-notify 不起作用...为什么?
最佳答案
因为我遇到了同样的问题...这就是为我解决的问题。 我没有让它扩展 AndroidTestCase,而是让它扩展了 InstrumentationTestCase。
然后我像这样启动 asyncTask:
runTestOnUiThread(new Runnable()
{
@Override
public void run()
{
startAsyncTask();
}
});
关于java - AndroidTestCase 中永远不会调用 AsyncTask 的 onPostExecute(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19635308/
设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
当我执行>rvminstall1.9.2时一切顺利。然后我做>rvmuse1.9.2也很顺利。但是当涉及到ruby-v时..sam@sjones:~$rvminstall1.9.2/home/sam/.rvm/rubies/ruby-1.9.2-p136,thismaytakeawhiledependingonyourcpu(s)...ruby-1.9.2-p136-#fetchingruby-1.9.2-p136-#downloadingruby-1.9.2-p136,thismaytakeawhiledependingonyourconnection...%Total%Rece
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht