6

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.

5
  • What does your test class look like? Feb 28, 2018 at 6:49
  • test class added to post ^
    – Eric
    Feb 28, 2018 at 13:54
  • If what you are ultimately aiming at is just loading properties for tests from a properties file that is specific to the test context, Spring Boot comes with an easy profiles mechanism for that purpose: docs.spring.io/spring-boot/docs/current/reference/html/… Feb 28, 2018 at 14:33
  • Yes, that's exactly what I'm trying to do, but can't get it to work. It keeps trying to read the files as XML instead of .properties or .yml. I've added snakeyaml to the test Classpath.
    – Eric
    Feb 28, 2018 at 14:55
  • Hm, at least in the code you posted, the getSdkKey() getter that you are calling in LDClientConfiguration.ldClient() doesn't exist. Did you just omit it here or is it absent in your actual code, too? Feb 28, 2018 at 20:27

3 Answers 3

1

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.

0

In Java:

  1. Add annotations on test class
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  1. Have an application.yaml in your test resources folder
  2. Test the values read in the property file
-4

This set of annotations on my test classes seems to work:

@SpringBootApplication
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = { <array of classes goes here> })
1
  • 1
    @SpringBootApplication should NOT be added to your Test Class. Mar 23, 2018 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.