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 6910835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:51:58+00:00 2026-05-27T08:51:58+00:00

I want to test some code that relies on a network transmission. The code

  • 0

I want to test some code that relies on a network transmission. The code makes a request and supplies a callback – when the request completes, the callback is fired. I’d like to mock out the network transmission, and use Thread.sleep to simulate some latency… but of course that will make the whole test pause.

So far I’ve been making new threads and using CountDownLatches throughout the test to stop the test from ending before the callback is fired. My mock network object makes a new thread, sleeps on that thread, and then fires the callback. That’s actually working pretty well, but the problem is that any assertion errors in the callback are not reported to the original junit thread – instead, I’m getting exception text on the console, where it’s much harder to understand and use.

I’m hoping there’s either:

  1. A way to pipe assertEquals output from spawned threads into the main JUnit output collector, or
  2. A totally different and better way to test threaded code in JUnit, or
  3. Some way to simulate asynchronous code in a single thread

Thanks for any ideas!

  • 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-27T08:51:59+00:00Added an answer on May 27, 2026 at 8:51 am

    When I had to implement asynchronous mechanism similar to yours I created abstract class AsyncTestCase that holds test error and provides special method waitForCallback(). When asynchronous task finds error in expected results it puts the error into this variable. The setter calls notify() on object that is used by waitForCallback(). So, when callback arrives it immediately causes test to awake. Then I can call all assertions including that one that was stored by asynchronous task.

    And do not forget to put timeout on your tests to prevent them from sleeping forever:

    @Test(timeout=10000)
    public void mytest() {
        callAsyncTask();
        waitForAsyncTask();       // from base class
        assertAsyncTaskResult();  // from base class
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to extensively test some pieces of C code for memory leaks. On
I'm trying to test some python code that uses urllib2 and lxml. I've seen
I'm trying to test some streaming file upload/download code that I've just written, but
I have some code that I use with Entity Framework like class Person{ pubic
I have some code that currently uses a ST monad for evaluation. I like
I'm trying to test some legacy code, using Mockito. I want to stub a
I have a some code that embeds a return_to URL into a redirect (like
I have some code that adds properties to an object like this: var MyObject
I have some code that I want to build. The code uses boost::ptr_map class
i want to test some code to make sure it handles NAN , INF

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.