Class SystemPropertiesExtension

java.lang.Object
io.github.cjstehno.testthings.junit.SystemPropertiesExtension
All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension

public class SystemPropertiesExtension extends Object implements org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback
A test extension used to update the System properties with a configured set of properties, resetting it back to the original values after each test.

In order to provide the property values to be injected, you must provide either a Properties or Map<String,String> object named "SYSTEM_PROPERTIES" on the test class as a static field.

Alternately, you may specify the field name containing your properties using the ApplyProperties annotation on the test method.

Before each test method is executed, the configured properties will be injected into the System properties; however, the original values will be stored and replaced after the test method has finished.

NOTE: Due to the global nature of the System properties, the test methods under this extension are locked so that only one should run at a time - that being said, if you run into odd issues, try executing these tests in a single-threaded manner (and/or report a bug if you feel the functionality could be improved).

  • Constructor Details

    • SystemPropertiesExtension

      public SystemPropertiesExtension()
  • Method Details

    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
      Throws:
      Exception
    • afterEach

      public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
      Throws:
      Exception
    • asProperties

      public static Properties asProperties(Map<String,String> map)
      A helper method that may be used to convert a Map to a Properties object containing the same data.
      Parameters:
      map - the map of property data
      Returns:
      a Properties object containing the map data