test-clock

A windable clock for tests.

Лицензия

Лицензия

Группа

Группа

com.mercateo
Идентификатор

Идентификатор

test-clock
Последняя версия

Последняя версия

1.0.2
Дата

Дата

Тип

Тип

jar
Описание

Описание

test-clock
A windable clock for tests.
Ссылка на сайт

Ссылка на сайт

https://github.com/Mercateo/test-clock
Организация-разработчик

Организация-разработчик

Mercateo AG
Система контроля версий

Система контроля версий

https://github.com/Mercateo/test-clock.git

Скачать test-clock

Как подключить последнюю версию

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

Зависимости

compile (1)

Идентификатор библиотеки Тип Версия
org.projectlombok : lombok jar 1.18.8

test (4)

Идентификатор библиотеки Тип Версия
junit : junit jar 4.12
org.mockito : mockito-core jar 2.27.0
org.assertj : assertj-core jar 3.12.2
com.google.guava : guava-testlib jar 27.1-jre

Модули Проекта

Данный проект не имеет модулей.

TestClock

A windable clock for tests.

Why using Clock at all?

You might ask why to use java.time.Clock. Why not System.currentTimeMillis() or TimeZone.getDefault() or new Date()? I'd like to cite the documentation from oracle

Use of a Clock is optional. All key date-time classes also have a now() factory method that uses the system clock in the default time zone. The primary purpose of this abstraction is to allow alternate clocks to be plugged in as and when required. Applications use an object to obtain the current time rather than a static method. This can simplify testing.

in example

You have a very simple watch

public class MyWatch {
    public String now() {
        return OffsetDateTime.now().toString();
    }
}

Now you want to test it's behavior: Does it return the current time? How? The following approach to test this watch works for exactly one point in time

    @Test
    public void testMyWatch() {
        MyWatch myWatch = new MyWatch();
        String now = myWatch.now();
        assertThat(now).isEqualTo("2018-11-07T10:12:12.414+01:00");
    }

That watch isn't testable. But if you use Clock it becomes testable, see com.mercateo.test.clock.example.timer.MyWatchTest

Why using this TestClock?

In case of over-time behavior you might want to test this behavior as well. How do you test it? Will you call Thread.sleep(...) in your test?

doSomething();
Thread.sleep(5000);
checkSomething();

see com.mercateo.test.clock.example.MyCacheTest.testGet_cache_expired() This test will need at least 5 seconds to be finished just because of idling 5 seconds.

What if you have a cache that expires after 1 hour? Do you want to Thread.sleep( 1 hr )?

Why don't you just fast forward in that test?

TestClock clock = ...
doSomething(clock);
clock.fastForward(Duration.ofSeconds(5));
checkSomething();

This test will not idle for 5 seconds but will only take as long as your business code runs. see com.mercateo.test.clock.example.MyCacheTest.testGet_cache_expired_with_TestClock()

Howto use

add dependency to your pom.xml

    <dependency>
        <groupId>com.mercateo</groupId>
        <artifactId>test-clock</artifactId>
        <version>1.0.2</version>
        <scope>test</scope>
    </dependency>

use TestClock in your test

    OffsetDateTime seed = OffsetDateTime.of(2018, 10, 19, 9, 27, 55, 0, ZoneOffset.UTC);
    TestClock clock = TestClock.fixed(seed)

    doSomething(clock);

    clock.fastForward(Duration.ofSeconds(3));

    checkSomething();
com.mercateo
the procurement platform for your business

Версии библиотеки

Версия
1.0.2
1.0.0
0.0.10