Class DefaultEnvironment

  • All Implemented Interfaces:
    KiwiEnvironment

    public class DefaultEnvironment
    extends Object
    implements KiwiEnvironment
    A default implementation of the KiwiEnvironment interface. Normal usage is to define a private KiwiEnvironment and initialize it to a new instance of this class, for example in a constructor:

    Then wherever you would normally call things like System.currentTimeMillis(), use the corresponding method from KiwiEnvironment instead, e.g. env.currentTimeMillis().

    For testing environment-related code, inject a mock instance via a constructor. A common pattern is to provide a separate constructor that accepts a KiwiEnvironment specifically for test code to use; often this should be made package-private (default scope). Other constructors will generally call this constructor. For example:

      private KiwiEnvironment env;
    
      public Foo() {
          this(new DefaultKiwiEnvironment());
      }
    
      Foo(KiwiEnvironment env) {
          this.env = env;
      }