简体   繁体   中英

Proper configuration of Spring Boot 2 and JUnit 5

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.

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

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

@SpringBootApplication
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = { <array of classes goes here> })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM