Using Spring Boot 2.0.0.RC2.
I've written a Configuration class:
@Configuration
@ConditionalOnProperty("launchdarkly.sdkKey")
public class LDClientConfiguration {
@Bean
public LDClientInterface ldClient(LDClientConfigurationProperties props) {
return new LDClient(props.getSdkKey(), props.getLDConfig());
}
}
And a ConfigurationProperties class:
@Component
@ConfigurationProperties(prefix = "launchdarkly")
public class LDClientConfigurationProperties {
private String sdkKey;
// more attributes
public void setSdkKey(String sdkKey) {
this.sdkKey = sdkKey;
}
// more setters
public LDConfig getLDConfig() {
LDConfig.Builder builder = new LDConfig.Builder();
// set builder w/ attributes
return builder.build();
}
}
I'm trying to test this, reading the config from src/test/resources/application-test.yml:
launchdarkly:
sdkKey: <redacted>
I have the following dependencies:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
And junit-platform-surefire-provider is configured in maven-surefire-plugin v2.19.1
I can't seem to figure out the proper annotations for my Test classes to read the config from src/test/resources/application-test.yml.
I've read https://junit.org/junit5/docs/current/user-guide but still can't seem to get the annotations correct. Any help would be appreciated.
edit: test class
@SpringJUnitConfig(SpringBootContextLoader.class)
public class LDClientConfigurationPropertiesTest {
@Autowired
private LDClientConfigurationProperties props;
@Test
public void test() {
LDConfig config = props.getLDConfig();
assertThat(config, notNullValue());
}
}
If I annotate the class with @ExtendWith(SpringExtension.class)
and @SpringBootTest
then it tries to load com/company/spring/launchdarkly/LDClientConfigurationTest-context.xml
and then com/company/spring/launchdarkly/LDClientConfigurationTestContext.groovy
but doesn't look for a yml file.
The proper set of annotations for Spring Boot 2 and JUnit 5 integration tests are (in Kotlin):
@ExtendWith(SpringExtension::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("dev") // optional
@TestInstance(TestInstance.Lifecycle.PER_CLASS) // optional
You can get the port the service is running on via the @LocalServerPort
annotation on a field.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With