Class SettingBundleStub

java.lang.Object
org.silverpeas.kernel.test.extension.SettingBundleStub
All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension

public class SettingBundleStub extends Object implements org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback
Provides easy stubbing on the SettingBundle in unit tests. This class is to declare as a field of the unit test class a SettingBundle that will be used by the tests and with which some settings can be preconfigured or modified without having to use a set of physical properties files (a SettingBundle is mapped to a properties file). By annotating the field with the RegisterExtension annotation, the required resources initialization is performed before the execution of a unit test and the resource cleanup is applied once the unit test resumed.
Author:
silveryocha
  • Constructor Details

    • SettingBundleStub

      public SettingBundleStub(Class<?> cls, String fieldName) throws IllegalAccessException
      Parameters:
      cls - the static class containing the setting attribute.
      fieldName - the name of setting attribute.
      Throws:
      IllegalAccessException - if the setting attribute can not be manipulated.
      See Also:
    • SettingBundleStub

      public SettingBundleStub(SettingBundle settingBundle)
      Initializing the setting bundled by this way means that the setting could have already been accessed by another class or instance and the setting bundle instance is registered into a static attribute or an attribute that will be shared between all tests.

      It is not mandatory to the caller to fill all the keys if a default property file has been registered into context. The given keys just overrides the default one.
      If a null value is returned by the stub for a key, then it is looking for the key into the default property file.

      Parameters:
      settingBundle - the setting bundle to stub.
    • SettingBundleStub

      public SettingBundleStub(String bundleName)
      Initializing the setting bundle by this way means that unit or integration test is looking for bundle registry at each test.

      In other words, the setting bundle MUST not be a static attribute (or an attribute) that will be shared between each test.

      The caller MUST register all the needed keys because there is no fallback onto a default property file.

      Parameters:
      bundleName - the bundle name to stub.
  • Method Details

    • put

      public SettingBundleStub put(String key, String value)
      Puts a couple key / value.
      Parameters:
      key - the key.
      value - the value.
      Returns:
      itself.
    • removeAll

      public SettingBundleStub removeAll()
      Removes all registered keys.
      Returns:
      itself.
    • 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
    • clear

      protected void clear() throws IllegalAccessException
      Throws:
      IllegalAccessException