Android AsyncTask testing with Android Test Framework

前端 未结 7 1375
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 09:42

I have a very simple AsyncTask implementation example and am having problem in testing it using Android JUnit framework.

It works just fine when I instantiate and e

7条回答
  •  一整个雨季
    2020-11-27 10:09

    Most of those solutions require a lot of code to be written for every test or to change your class structure. Which I find very difficult to use if you have many situations under test or many AsyncTasks on your project.

    There is a library which eases the process of testing AsyncTask. Example:

    @Test
      public void makeGETRequest(){
            ...
            myAsyncTaskInstance.execute(...);
            AsyncTaskTest.build(myAsyncTaskInstance).
                        run(new AsyncTest() {
                            @Override
                            public void test(Object result) {
                                Assert.assertEquals(200, (Integer)result);
                            }
                        });         
      }       
    }
    

    Basically, it runs your AsyncTask and test the result it returns after the postComplete() has been called.

提交回复
热议问题