开发者

Android unit tests with multiple threads

开发者 https://www.devze.com 2023-04-12 13:56 出处:网络
I have a problem with unit tests in Android. My object MyObject has a method start() like this : public void start() {

I have a problem with unit tests in Android.

My object MyObject has a method start() like this :

public void start() {
    final Handler onStartHandler = new Handler();
    new Thread() {
        @Override
       开发者_Go百科 public void run() {
            super.run();

            onStartHandler.post(new Runnable() {
                @Override
                public void run() {
                    mIsRunning = true;
                    onStart();
                }
            });
        }
    }.start();
}

And I want to test that onStart() is called. So I tried something like that :

public void testOnStartIsCalled() {
    assertFalse("onStart() should not be called", mMyObject.isRunning());
    mMyObject.start();
    assertTrue("onStart() should be called", mMyObject.isRunning());
    mMyObject.stop();
    assertFalse("onStop() should be called", mMyObject.isRunning());
}

But it doesn't work, I guess it's because it's in a Handler and a new Thread.

My test class extends AndroidTestCase. What should I do ? What is the best practice for this case ?

Regards.


When I deal with testing some multi-threaded code I try to let the program take as much of its natural flow as possible. Additionally, I avoid the use of sleep statements since you don't get any guarantees that the sleep interval you've chosen is enough to allow the subject of your test to finish what it's doing; you often end up having to choose sleep intervals that are too large and it forces a much slower execution of your test cases.

I would recommend that you try to add some code into the class you're testing, in this case MyObject, which call a listener whenever something happens. It seems that you already have callback methods for onStart() and onStop()(if those are events/callbacks), so those should be getting invoked and you should use them to control the flow of your test. When you get an onStart() event, you should then call stop() and wait for an onStop() event.

Update

First and foremost, you have redundant code:

public void start() {
    final Handler onStartHandler = new Handler();
    new Thread() {
        @Override
        public void run() {
            super.run();

            onStartHandler.post(new Runnable() {
                @Override
                public void run() {
                    mIsRunning = true;
                    onStart();
                }
            });
        }
    }.start();
}

Either start a new thread to call onStart() or schedule the runnable on the Handler's thread queue.

Version 1- remove the handler and just let the code be executed in a new thread:

public void start() {
    new Thread() {
        @Override
        public void run() {
            super.run();
            mIsRunning = true;
            onStart();
        }
    }.start();
}

Version 2- only use the handler to asynchronously execute the callback:

public void start() {
    final Handler onStartHandler = new Handler();

    onStartHandler.post(new Runnable() {
        @Override
        public void run() {
            mIsRunning = true;
            onStart();
        }
    });
}

And second: I noticed is that if you don't have a Looper, then whatever you post with the Handler will be ignored (thus it will never be called). For more information on the Looper-Handler pattern see the article: Android Guts: Intro to Loopers and Handlers. The Looper and the Handler are supposed to be attached to the same thread (usually the main thread). Additionally, if you're creating the Handler on a separate thread as your Looper, then you'll run into the same problem: anything you post with the Handler will be ignored.

Here are a few more good questions and articles on loopers and handlers:

  • Just do IT: looper and handler in android
  • Handler-Looper implementation in Android

The relationships between Looper, Handler and MessageQueue is shown below:

Android unit tests with multiple threads


The problem here is that you are calling onStart() which invokes a new thread, and then immediately ask if it is started. There is startup time for the new thread and while that is happening, your test is asking if it is started -- it's not YET.

I bet if you waited by using Thread.sleep(), or a loop, you'd find it is started "eventually".

What is it you're actually trying to test?

If you need the new thread, you might want to read up on threads, synchronize, etc. http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号