https://github.com/diffplug/durian-globals
Globals are useful. Durian makes them testable too.
https://github.com/diffplug/durian-globals
Last synced: 8 months ago
JSON representation
Globals are useful. Durian makes them testable too.
- Host: GitHub
- URL: https://github.com/diffplug/durian-globals
- Owner: diffplug
- License: apache-2.0
- Created: 2019-11-27T23:50:15.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-06-06T00:38:40.000Z (over 5 years ago)
- Last Synced: 2025-04-23T08:08:14.152Z (8 months ago)
- Language: Java
- Homepage:
- Size: 242 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
#
DurianGlobals: Easy-to-test singletons
[](https://search.maven.org/search?q=g:com.diffplug.durian-globals)
[](https://tldrlegal.com/license/apache-license-2.0-(apache-2.0))
[](CHANGELOG.md)
[](https://javadoc.jitpack.io/com/github/diffplug/durian-globals/durian-globals-agg/release~1.0.0/javadoc/)
[](https://gitter.im/diffplug/durian)
[](https://jitci.com/gh/diffplug/durian-globals)
## Usage
Provides an easy way to initialize a singleton exactly once:
```java
public class Singleton {
public static Singleton instance() {
return Globals.getOrSetTo(Singleton.class, Singleton::new);
}
protected Singleton() {}
...
}
```
In a testing environment, you can wipe the globals to get a clean state or to replace the standard implementation with a testing one.
```java
public class SingletonTest {
static class SingletonDev extends Singleton {
...
}
@Test
public void someTest() {
try (AutoCloseable wipeGlobals = GlobalsDev.wipe()) {
SingletonDev dev = new SingletonDev();
GlobalsDev.install(Singleton.class, dev);
...
}
}
}
```
The "trick" is that `GlobalsDev` is shipped in a different artifact than the rest, so you can be sure that your `Globals` can only be changed in tests, and never in production code:
```gradle
dependencies {
implementation 'com.diffplug.durian-globals:durian-globals:1.0.0'
testImplementation 'com.diffplug.durian-globals:durian-globals.dev:1.0.0'
}
```
## Built-ins
There are some globals that people frequently want control over during testing.
### Time
You can use [`public static long Time.now()`](https://javadoc.jitpack.io/com/github/diffplug/durian-globals/durian-globals-agg/release~1.0.0/javadoc/com/diffplug/common/globals/Time.html) as a replacement for `System.currentTimeMillis()`. And in a test, you can replace it with [`TimeDev`](https://javadoc.jitpack.io/com/github/diffplug/durian-globals/durian-globals-agg/release~1.0.0/javadoc/com/diffplug/common/globals/TimeDev.html).
```java
@Test
public void someTimeDependentTest() {
try (AutoCloseable wipeGlobals = GlobalsDev.wipe()) {
TimeDev time = TimeDev.install();
time.setUTC(LocalDate.parse("2019-03-30"));
... // exercise code that uses `Time.now()`
}
}
```
## Requirements
DurianGlobals requires nothing but Java 8+.
## In the wild
- Integration testing the [spotless-changelog](https://github.com/diffplug/spotless-changelog) gradle plugin
- [build setup](https://github.com/diffplug/spotless-changelog/blob/c6c66eb12e5ce8a23d42fcb34ef3753382ee4cb4/build.gradle#L52-L54)
- [integration code](https://github.com/diffplug/spotless-changelog/blob/11fb5f5cd13921f8ecf8151092fdbad44b6f3004/spotless-changelog-plugin-gradle/src/test/java/com/diffplug/spotless/changelog/gradle/ChangelogPluginTest.java#L33-L34)
## Acknowledgements
* Built by [gradle](http://gradle.org/).
* Tested by [junit](http://junit.org/).
* Maintained by [DiffPlug](http://www.diffplug.com/).