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

  • SEARCH
  • Home
  • 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 3931502
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:21:28+00:00 2026-05-19T23:21:28+00:00

I am ultimately trying to unit test a custom base adapter, but I am

  • 0

I am ultimately trying to unit test a custom base adapter, but I am stuck at one point: I am unable to figure out how to get the UI thread to process the message I have just passed to it via sendEmptyMessage(...).

In my Activity I have a “addFoo(Foo foo)” method that updates an array and then calls “sendEmptyMessage(1)“, and I have a handleMessage method that calls the ListView’s adapter to tell it the data has changed.

I’ve boiled the unit test down to, roughly:

public void testAddFoo() {
  Foo foo = new Foo();
  assertTrue(mActivity.addFoo(foo));
  assertTrue(mActivity.getHandler().hasMessages(1));
  assertFalse(mActivity.getHandler().hasMessages(1));
  assertTrue(mActivity.fooListNotEmpty());
}

Naturally it is failing on that assertFalse() (this is a contrived example, simplifying it as best as I can).

What call can I make in to mActivity or its handler or looper to get it to process all pending messages? I have tried some suggestions I’ve read about calling Looper.loop(), within the UI thread, but those are stabs in the dark and they failed.

FWIW, I’m pretty sure that the handleMessage code is correct because if I call it directly (inside @UiThreadTest) like so:

@UiThreadTest
public void testAddFoo() {
  Foo foo = new Foo();
  assertTrue(mActivity.addFoo(foo));
  Message msg = Message.obtain();
  msg.what = 1;
  mActivity.handleMessage(msg);
  assertTrue(mActivity.fooListNotEmpty());
}

the test ends up working as expected.

  • 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-19T23:21:28+00:00Added an answer on May 19, 2026 at 11:21 pm

    If you are implementing Handler.Callback in your activity then you should just do

    assertTrue(mActivity.hasMessages(1));
    

    otherwise do not implement the callback and use

    mHandler = new Handler() {
    
    // override Handler methods as required
    // ie handleMessage(Message msg)
    
    };
    

    and

    assertTrue(mActivity.mHandler.hasMessages(1));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get an object element from my webpage using getElementById (ultimately so
I'm trying to get into Unit testing for the obvious positives it introduces, and
I'm ultimately trying to validate a FileField by extension type. But I'm having trouble
Good Evening everyone, I've been trying to figure out the most efficient way to
I am trying to get my head around a LINQ issue. The ultimate goal
This code produces a FileNotFoundException, but ultimately runs without issue: void ReadXml() { XmlSerializer
One of the bullet point features for Windows Vista Enterprize and Ultimate is the
This is yet another Unit Testing question. I'm trying to draw knowledge from a
I'm ultimately trying to install PEAR so I can easily install PHPUnit. I want
I am running Ubuntu and am ultimately trying to connect Tomcat to my MySQL

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.