Testing
AndHow makes testing with multiple configurations easy
Let's write some tests for the ReportGenerator from the Properties examples. Here is that class:
This class doesn't really do anything, but lets assume its a lambda function that is launched to run a complicated report.
Using andhow.properties on the test classpath
AndHow automatically finds and loads the andhow.properties
file at the root of the classpath. Simply place an andhow.properties
file at the root of the test classpath to create a configuration used for testing. This is standard feature of how Maven and many other build tools work and will result in a shared configuration for all tests.
Best Practice: Use Property default values for good business-related defaults. Don't use default values local workstation or test environment configuration values.
Since its easy to provide a test configuration file or a local configuration file, don't be tempted to use Property default values for these purposes. Unless configured to a A Property with a default value already has a default value, so there will be no warning if that value
Using the AndHow JUnit Extensions
The JUnit extensions simplify testing multiple configuratino scenarios in your tests. They can be included in a Maven project like this:
The next examples use the extensions.
Customize Property values for a test
Lets say that you need test a scenario for a specific zip code in the 'west' region:
The @KillAndHowBeforeThisTest
is one of the AndHow JUnit extensions. It can be placed on an individual test method to reset AndHow to its unconfigured state before the test runs. When the test is done, the original AndHow state is restored (which may be the un-initialized state).
AndHow.findConfig()
grabs the configuration of AndHow before it initializes and loads Property values. addFixedValue()
effectively hard-codes a specific Property value via the FixedValueLoader.
Assuming no environment vars., system properties or other configuration sources provide named values that match the app's Properties, Property values for the test above would come from:
The 'fixed values' for
ZIP
andREGION
andhow.properties
on the test classpath (if there is one) for other propertiesandhow.properties
on the main classpath if there is no file on the test classpath
'Killing' and resetting AndHow isn't allowed in production. AndHow Property values are constants and once initialized at startup, do not change. The @KillAndHow...
annotations uses reflection to bend the rules to make testing easier.
Custom .properties file for one or more tests
If a test scenario involves setting lots of Property values or is needed for multiple tests, a separate .properties file can be used. Here is an example of one way to do that for an entire test class:
@KillAndHowBeforeEachTest
on the test class is the same as putting @KillAndHowBeforeThisTest
on each test. All tests in this class will use the west_region.properties
set in the @BeforeEach
method. Since AndHow is reset before each test, we can even further customize AndHow's configuraiton before an individual test, as in the zipCode90212
test.
If an entire test class needs to run with the same configuration for all tests and/or you don't want AndHow to re-initialize for each test to save a bit of execution time, this can be done slightly differently:
The @KillAndHowBeforeAllTests
JUnit extension resets AndHow a single time before the test class executes its tests. The @BeforeAll
method can be used to initialize AndHow as you want it for all the tests. Now all the tests in this class will share the same configuration.
The AndHow.instance() call in @BeforeAll
is not necessary, but is a good idea. Without that call, AndHow leaves the setup method uninitialized. One of the test methods could still modify the configuraton (similar to the previous example's zipCode90212()
method), leading to confusion. Once AndHow is initialized, it will throw a clear exception if any attempt is made to modify it's configuration.
Last updated
Was this helpful?