Testing asynchronous code with JUnit

Riley Lark :

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!

AlexR :

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
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

JUnit testing an asynchronous method with Mockito

From Dev

junit testing for asynchronous callback method

From Dev

Testing asynchronous code in Elixir

From Dev

Testing asynchronous RxJava code - Android

From Dev

Testing and mocking asynchronous code that uses async with statement

From Dev

Jest , testing Asynchronous JS code always failing

From Dev

Testing Asynchronous Code (useEffect + fetch) In React Components

From Dev

Asynchronous unit testing in Java/Junit - a very simple yet unsuccessful example

From Java

Why is my code failing the JUnit testing for IllegalArgumentException?

From Dev

Asynchronous Javascript code testing using Jest works when it is not supposed to

From Dev

How to wait when testing asynchronous code in Angular 8 with Jasmine?

From Dev

Testing Asynchronous Code in Jest: done() not being called as expected

From Dev

Code coverage concern on promise/asynchronous unit testing using nockjs and jest

From Dev

Difficulty testing asynchronous function

From Dev

Testing Asynchronous Callbacks with Jasmine

From

Asynchronous Testing With Stream Processing

From Dev

Unit testing asynchronous operations

From Dev

Ember Error while testing: You will need to wrap any code with asynchronous side-effects in a run

From Java

efficient junit testing of graphs?

From Java

Testing server with JUnit

From Dev

How to do the jUnit testing

From Dev

JUnit exceptions handling testing

From Dev

JUnit-testing of NatTable

From Dev

LoadingCache mocking for JUnit testing

From Dev

Testing Lombok annotations with Junit

From Java

@EntityListeners Injection + jUnit Testing

From Java

Spring RestController + Junit Testing

From Dev

junit event driven testing

From Dev

JUnit testing with mockito

Related Related

  1. 1

    JUnit testing an asynchronous method with Mockito

  2. 2

    junit testing for asynchronous callback method

  3. 3

    Testing asynchronous code in Elixir

  4. 4

    Testing asynchronous RxJava code - Android

  5. 5

    Testing and mocking asynchronous code that uses async with statement

  6. 6

    Jest , testing Asynchronous JS code always failing

  7. 7

    Testing Asynchronous Code (useEffect + fetch) In React Components

  8. 8

    Asynchronous unit testing in Java/Junit - a very simple yet unsuccessful example

  9. 9

    Why is my code failing the JUnit testing for IllegalArgumentException?

  10. 10

    Asynchronous Javascript code testing using Jest works when it is not supposed to

  11. 11

    How to wait when testing asynchronous code in Angular 8 with Jasmine?

  12. 12

    Testing Asynchronous Code in Jest: done() not being called as expected

  13. 13

    Code coverage concern on promise/asynchronous unit testing using nockjs and jest

  14. 14

    Difficulty testing asynchronous function

  15. 15

    Testing Asynchronous Callbacks with Jasmine

  16. 16

    Asynchronous Testing With Stream Processing

  17. 17

    Unit testing asynchronous operations

  18. 18

    Ember Error while testing: You will need to wrap any code with asynchronous side-effects in a run

  19. 19

    efficient junit testing of graphs?

  20. 20

    Testing server with JUnit

  21. 21

    How to do the jUnit testing

  22. 22

    JUnit exceptions handling testing

  23. 23

    JUnit-testing of NatTable

  24. 24

    LoadingCache mocking for JUnit testing

  25. 25

    Testing Lombok annotations with Junit

  26. 26

    @EntityListeners Injection + jUnit Testing

  27. 27

    Spring RestController + Junit Testing

  28. 28

    junit event driven testing

  29. 29

    JUnit testing with mockito

HotTag

Archive