I would like to point out that, as far as I can tell, you're not doing a real unit test.
It seems that you have at least two components. So let's say you have two, component A and component B. You would like to test both, but you first have to call A, which calls an asynchronous process that does something and then triggers B. Is this correct? If so, the whole sequence is not a unit test. A true unit test would be to call A and then verify its results. To do that, you might have to have a test version of the asynchronous process. To test B, you'd have to set up a fixture that mocks the results of the asynchronous process, then you'd have to call B. The classes in android.test.* are not a substitute for a full test harness. Instead, they're extensions of junit.framework.TestCase that allow you to run JUnit within Android. To do testing of more than one component at a time, you have to write your own test harness, and notice that it has to be an Android app. On Oct 3, 2:28 pm, Doug <[email protected]> wrote: > On Oct 2, 9:28 pm, Alex <[email protected]> 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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

