Sunday, October 3, 2010

[android-developers] Re: unit testing asynchronous API calls ?

On Oct 2, 9:28 pm, Alex <aleksm...@gmail.com> wrote:
> I was wondering what is the best  way to accomplish this ?

I can suggest a solution, though it may not be the best.

You probably going to have to set up some kind of listener interface
to be implemented in your unit test that triggers after the result is
processed by the UI thread. Otherwise, you'll have to figure out
another way to detect when the processing is complete.

In you make a listener the responds to completion, you'll have to then
signal junit that it completed so it can assert what it needs to
assert.

An easy way to do that is:
- Define a CountDownLatch that you create before executing AsyncTask
- After executing, call await on the latch (with a timeout if you need
it)
- In the callback listener, call countDown on the latch to notify the
test that it's done

Basically, you just need to make the junit thread to wait for the
results, and you can use any standard multithreading technique to do
it.

But if all you need to do is validate the results of a task without
needing to run it on the UI thread, you could just try to call the
methods on the AsyncTask directly (don't use execute()) and detect the
results when the methods complete normally.

Doug

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate