DurianGlobals (Dev API)

Developer API for testable globals (don't ship!)

License

License

GroupId

GroupId

com.diffplug.durian-globals
ArtifactId

ArtifactId

durian-globals.dev
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

DurianGlobals (Dev API)
Developer API for testable globals (don't ship!)
Project URL

Project URL

https://github.com/diffplug/durian-globals
Source Code Management

Source Code Management

https://github.com/diffplug/durian-globals

Download durian-globals.dev

How to add to project

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

Dependencies

compile (1)

Group / Artifact Type Version
com.diffplug.durian-globals : durian-globals jar 1.0.0

Project Modules

There are no modules declared in this project.

DurianGlobals: Easy-to-test singletons

Maven central Apache 2.0

Changelog Javadoc Live chat JitCI

Usage

Provides an easy way to initialize a singleton exactly once:

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.

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:

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() as a replacement for System.currentTimeMillis(). And in a test, you can replace it with TimeDev.

@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

Acknowledgements

com.diffplug.durian-globals

DiffPlug

View, edit, compare. Anything, from anywhere.

Versions

Version
1.0.0
0.1.4
0.1.3
0.1.1