AsyncTest

This simple Java library helps you write better tests for asynchronous systems

License

License

Categories

Categories

Net
GroupId

GroupId

net.codebox
ArtifactId

ArtifactId

asynctest
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

AsyncTest
This simple Java library helps you write better tests for asynchronous systems
Project URL

Project URL

https://github.com/codebox/asynctest
Source Code Management

Source Code Management

https://github.com/codebox/asynctest

Download asynctest

How to add to project

<!-- https://jarcasting.com/artifacts/net.codebox/asynctest/ -->
<dependency>
    <groupId>net.codebox</groupId>
    <artifactId>asynctest</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/net.codebox/asynctest/
implementation 'net.codebox:asynctest:1.0.1'
// https://jarcasting.com/artifacts/net.codebox/asynctest/
implementation ("net.codebox:asynctest:1.0.1")
'net.codebox:asynctest:jar:1.0.1'
<dependency org="net.codebox" name="asynctest" rev="1.0.1">
  <artifact name="asynctest" type="jar" />
</dependency>
@Grapes(
@Grab(group='net.codebox', module='asynctest', version='1.0.1')
)
libraryDependencies += "net.codebox" % "asynctest" % "1.0.1"
[net.codebox/asynctest "1.0.1"]

Dependencies

compile (1)

Group / Artifact Type Version
junit : junit jar 4.11

test (1)

Group / Artifact Type Version
org.hamcrest : hamcrest-all jar 1.3

Project Modules

There are no modules declared in this project.

AsyncTest

This simple Java library helps you write better tests for asynchronous systems.

Here is a common integration testing anti-pattern:

sendRequest();
Thread.sleep(5000);
checkThatResponseIsCorrect();

If the response is returned quickly then the test will still wait for 5 seconds before moving on, making your test suite slow. On the other hand, if the response is returned correctly but takes slightly longer than 5 seconds then the test will fail, which may not be what you want.

Using the asynctest library, you could re-write the test like this:

sendRequest();
assertThatEventually().thisTaskWillSucceed(this::checkThatResponseIsCorrect);

Behind the scenes the library will run the checkThatResponseIsCorrect method repeatedly, stopping when the method completes without throwing any Exceptions, or when the time limit of 15 seconds is reached.

To specify a different time limit, you can do this:

sendRequest();
assertThatWithin(Duration.ofSeconds(5)).thisTaskWillSucceed(this::checkThatResponseIsCorrect);

To assert that an Exception does get thrown, just change the code to this:

assertThatWithin(Duration.ofSeconds(5)).thisTask(this::sendRequest).willThrow(HttpException.class);

When an assertion fails you will see a comprehensive error message explaining what happened, for example:

java.lang.AssertionError: Operation did not throw org.apache.commons.httpclient.HttpException 
within the timeout interval of 5 seconds. The task ran 5 times. There were no AssertionErrors. 
No other Exceptions were thrown.

Versions

Version
1.0.1
1.0.0