Page tree
Skip to end of metadata
Go to start of metadata

time-test is a lightweight framework for testing time-based functionality.



time-test is a framework for testing time-based functionality.

Sometimes you have code with similar logic: "wait for 30 seconds and then do something if no changes have been detected".

If you want to test such logic you can write something like this:

Thread.sleep(30_000 + 200);

However, such tests are undesirable to be in the project. Also what if you want to test logic which should be executed once per month? Or what if this "something doing" hasn't done before the check starts?

time-test provides an easy way to test such logic. For example, the code above can be written more clear:

TestTimeProvider.waitUntilThreadsAreFrozen(200 /*ms*/);

And this test doesn't do superfluous work.

time-test instruments byte-code and change time-based methods invocations (such as System.currentTimeMillis, Object.wait, Unsafe.park) to our own implementation.


This interface provides an implementation of time-based methods. The default implementation forwards all calls to standard system methods.

Use XXXTimeProvider.start() to start using it. Don't forget to reset TimeProvider to default. Use XXXTimeProvider.reset() for this.


DummyTimeProvider throws UnsupportedOperationException on all method calls. It should be used for testing functionality which does not depend on time.


TestTimeProvider provides full access on time. Use TestTimeProvider.setTime(millis) and TestTimeProvider.inscreaseTime(millis) to change current time. Use TestTimeProvider.waitUntilThreadsAreFrozen to wait until all threads complete their work.

In order to work properly TestTimeProvider defines if it is executed in the testing code or not on every time-based operation invocation (including Object.notity() and similars). For this purpose an entry point to your code have to be specified (see timetest.testingCode property). After that, if your code starts a thread it will be marked as ours too (time-test traces Thread.start() invocations for this purpose). However, there are some problems if you use shared scheduler like ForkJoinPool. In order to work with it expand timetest.testingCode property.


You can pass your own configuration in properties file or set these properties as system parameters ( The file should be in the application classpath.

  • timetest.testingCode - defines the entry points of the testing code in glob format. Several globs can be separated by comma. Default value: com.devexperts.*Test (all classes with Test suffix).
  • timetest.nonTestingCode - defines the scope of code which have to be processed like non-testing onecode in glob format. It can be helpful to print real timestamps in logging instead of virtual ones. Default value: com.devexperts.logging.*.
  • timetest.log.level defines internal logging level. Possible values: DEBUG, INFO (default value), WARN, ERROR.
  • timetest.log.file defines path of file to be used for logging. By default logs are printed to the standard output.
  • timetest.cache.dir [experimental] defines directory to be used for transformed classes caching. This feature is unstable, use it on your own risk.
  • timetest.include defines the transformation scope using globs. For example, setting the value to*,* informs time-test to transform classes from these packages only. By default all classes are included.
  • timetest.exclude defines the classes which should be excluded from transformation. The syntax is similar to timetest.include option. Default value: org.apache.maven.*,org.junit.*,com.devexperts.test.*


Time-test is implemented as java agent and you should copy it to build directory and configure maven-surefire-plugin to use it for tests.

    <!-- maven-dependency-plugin is used to copy "timetest" agent into target directory -->
    <!-- Configure maven-surefire-plugin to use "timetest" agent -->

Usage example

public void setUp() {

public void tearDown() {

@Test(timeout = 100)
public void testSleepWithTestTimeProvider() {
    Thread t = new Thread(() -> {
        // Do smth
        int sum = 0;
        for (int i = 0; i < 100_000; i++)
            sum += i;
        // Sleep


If you need help, you have a question, or you need further details on how to use time-test, you can refer to the following resources:

You can use the following e-mail to contact us directly:

  • No labels