*/
public class Options {
- @Option(name = "SMTP_HOSTNAME")
- public final String smtpHostname = "localhost";
+ @Option(name = "SMTP_HOSTNAME", defaultValue = "localhost")
+ public final String smtpHostname = null;
@Option(name = "ERROR_EMAIL_SENDER", required = true)
public final String errorEmailSender = null;
@Option(name = "ERROR_EMAIL_RECIPIENT", required = true)
public final String errorEmailRecipient = null;
- @Option(name = "STATE_DIRECTORY")
- public final String stateDirectory = "states";
+ @Option(name = "STATE_DIRECTORY", defaultValue = "states")
+ public final String stateDirectory = null;
- @Option(name = "CHAIN_DIRECTORY")
- public final String chainDirectory = "chains";
+ @Option(name = "CHAIN_DIRECTORY", defaultValue = "chains")
+ public final String chainDirectory = null;
}
}
field.setAccessible(true);
try {
- field.set(optionsObject, value.orElse(null));
+ field.set(optionsObject, value.orElse(option.defaultValue()));
} catch (IllegalAccessException iae1) {
/* swallow. */
}
requiredTestOptions.getOptionOne();
}
+ @Test
+ public void parserSetsDefaultValueIfValueIsMissing() {
+ Mockito.when(environment.getValue("foo")).thenReturn(Optional.empty());
+ DefaultTestOptions options = parser.parseEnvironment(DefaultTestOptions::new);
+ MatcherAssert.assertThat(options.optionOne, Matchers.is("something"));
+ }
+
/**
* Test class with options used by {@link Parser}.
*
}
+ /**
+ * Test class with options used by {@link Parser}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+ private static class DefaultTestOptions {
+
+ @Option(name = "foo", defaultValue = "something")
+ public final String optionOne = null;
+
+ }
+
}