Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6680109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:26:12+00:00 2026-05-26T04:26:12+00:00

I have a problem with unit tests in Android. My object MyObject has a

  • 0

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
        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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:26:12+00:00Added an answer on May 26, 2026 at 4:26 am

    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:
    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this strange problem in my unit tests. See the following code _pos
I have a problem executing android unit tests against android applications that utilize the
I have an odd problem in VS2008 running unit tests. When I run the
I have a weird problem while building VS2010 solution incl. unit tests on a
I have a set of Unit Tests run in parallel ( this is important,
We have some scripts that we run as part of our unit tests. This
I want to test my request builder in unit tests. But I have problem
I have a problem executing unit tests in Eclipse behind a proxy, as the
I have following problem. I have some unit tests implemented in a foreign assebly
I have problem organizing my unittest based class test for family of tests. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.