Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to test asynchronous tasks in Android

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to test asynchronous tasks in Android". In daily operation, I believe many people have doubts about how to test asynchronous tasks in Android. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to test asynchronous tasks in Android". Next, please follow the editor to study!

Testing in Android (broadly defined) is an extension of a collection of unit tests. Involves initializing, shutting down tests, including setUp () and tearDown () operations, using reflection to infer different testing methods (we can use comments to specify priorities and perform all tests since JUnit4). A typical test structure is as follows:

Public class MyManagerTest extends ActivityTestCase {public MyManagerTest (String name) {super (name);} protected void setUp () throws Exception {super.setUp ();} protected void tearDown () throws Exception {super.tearDown ();} public void testDummyTest () {fail ("Failing test");}}

This is a very obvious example: in actual development, we want to test things such as HTTP responses, SQL storage, and so on. At Sixt we follow a Manager/Model approach: each Model contains the representation of an entity (car, customer, etc.). Each Manager aggregates a set of functions using a different model (for example, our LoginManager may need a model with which users interact with it).

Most Manager centrally execute HTTP requests to get data from the background. For example, we use the following code to perform the user's login:

MLoginManager.performLoginWithUsername ("username", "password", new OnLoginListener () {@ Override public void onFailure (Throwable throwable) {fail ();} Override public void onSuccess (User customer) {/ /.. })

When applied to our own test set, we just let the result fail when we get an unexpected result. We can see why we called fail () in the onFailure () function. Next, I can pass the test even if I use the wrong user name. After thinking about it, the test seems to be executed in code order, but it doesn't wait until the result of the callback function is returned before holding the line down.

This is obviously not a good idea. Because today's programs often get data from the background through asynchronous tasks and callback methods. Trying the UIThread test still doesn't work.

*, I find the following method can work. Just use a simple CountDownLatch signal object to implement the wait-notify mechanism (you can also use syncronized (lock) {. Lock.notify ();}, it's just that the code is not beautiful)

Then the previous code looks like this:

Final CountDownLatch signal = new CountDownLatch (1); mLoginManager.performLoginWithUsername ("username", "password", new OnLoginListener () {@ Override public void onFailure (Throwable throwable) {fail (); signal.countDown ();} Override public void onSuccess (User customer) {signal.countDown ();}}); signal.await () At this point, the study on "how to test asynchronous tasks in Android" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report