Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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

Code Block
languagejava
doWork();
Thread.sleep(30_000);
check();

But what if you want to test logic which should be executed once per month? Or only on specific days?

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

Code Block
languagejava
themeConfluence
doWork();
TestTimeProvider.increaseTime(30_000);
check();

 

TimeProvider

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

...

Configuration has 2 properties: *include - list of patterns in glob format, if any of this pattern applies processed class name then this class is transformed. * exclude - list of patterns in glob format, if any of this pattern not applies processed class name then this class is transformed. Overrides include** property.

Default configuration:

 

Code Block
languagebash
exclude = java.util.logging.*,\
          org.apache.log4j.*,\
          org.apache.logging.*,\
          org.slf4j.*,\
          com.devexperts.logging.*,\
          org.junit.*,\
          org.apache.maven.*

 

Maven

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

Code Block
languagexml
 ...
<plugins>
    <!-- maven-dependency-plugin is used to copy "timetest" agent into target directory -->
    <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <id>copy-sample-agent</id>
                <phase>process-test-classes</phase>
                <goals>
                    <goal>copy</goal>
                </goals>
                <configuration>
                    <artifactItems>
                        <artifactItem>
                            <groupId>com.devexperts.timetest</groupId>
                            <artifactId>agent</artifactId>
                            <version>${project.version}</version>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                            <destFileName>timetest.jar</destFileName>
                        </artifactItem>
                    </artifactItems>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <!-- Configure maven-surefire-plugin to use "timetest" agent -->
    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <argLine>-javaagent:${project.build.directory}/timetest.jar</argLine>
        </configuration>
    </plugin>
</plugins>
...

 

Example

 

Code Block
@After
public void tearDown() {
    TestTimeProvider.reset();
}

@Test(timeout = 100)
public void testSleepWithTestTimeProvider() {
    TestTimeProvider.start();
    Thread t = new Thread(() -> {
        // Do smth
        int sum = 0;
        for (int i = 0; i < 100_000; i++)
            sum += i;
        // Sleep
        Thread.sleep(10_000);
    });
    t.start();
    TestTimeProvider.waitUntilThreadsAreFrozen();
    TestTimeProvider.increaseTime(10_000);
    t.join();
}