在Spring Boot测试期间替换@Configuration中的@Value属性

Mar*_*zer 7 testing spring-boot

脚本

我有一个Spring Boot应用程序,带有带@Configuration注释的Spring配置类,其中包含一些带@Value注释的字段.对于测试,我想用自定义测试值替换这些字段值.

不幸的是,这些测试值不能使用简单的属性文件,(String)常量或类似方法覆盖,而是必须使用一些自定义编写的属性来解析Java类(例如TargetProperties.getProperty("some.username")).

我的问题是,当我添加自定义PropertySourceConfigurableEnvironment我的测试配置中,它已经为时已晚,因为这PropertySource将被添加之后的如RestTemplate已创建.

我如何可以覆盖@Value注解领域一个@Configuration与获得性类编程通过自定义的Java代码之前,什么都被初始化?

生产配置类

@Configuration
public class SomeConfiguration {

    @Value("${some.username}")
    private String someUsername;

    @Value("${some.password}")
    private String somePassword;

    @Bean
    public RestTemplate someRestTemplate() {
        RestTemplate restTemplate = new RestTemplate();

        restTemplate.getInterceptors().add(
            new BasicAuthorizationInterceptor(someUsername, somePassword));

        return restTemplate;
    }

}
Run Code Online (Sandbox Code Playgroud)

测试配置类

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class SomeTest {

    @SpringBootConfiguration
    @Import({MySpringBootApp.class, SomeConfiguration.class})
    static class TestConfiguration {

        @Autowired
        private ConfigurableEnvironment configurableEnvironment;

        // This doesn't work:

        @Bean
        @Lazy(false)
        // I also tried a @PostConstruct method
        public TargetPropertiesPropertySource targetPropertiesPropertySource() {
            TargetPropertiesPropertySource customPropertySource =
                new TargetPropertiesPropertySource();
            configurableEnvironment.getPropertySources().addFirst(customPropertySource);
            return customPropertySource;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

ale*_*xbt 12

您可以@SpringBootTest使用以下properties参数直接在注释中覆盖属性:

@SpringBootTest(properties = {"some.username=user", "some.password=pwd"},
                webEnvironment = SpringBootTest.WebEnvironment.NONE)
Run Code Online (Sandbox Code Playgroud)


Ama*_*har 10

您可以使用 @TestPropertySource

@TestPropertySource(
    properties = {
        "some.username=validate",
        "some.password=false"
    }
)
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ApplicationTest {
    //...
}
Run Code Online (Sandbox Code Playgroud)


Jou*_*ner 6

您可以在生产情况下使用构造函数注入,这允许它手动设置配置:

@Configuration
public class SomeConfiguration {

    private final String someUsername;
    private final String somePassword;

    @Autowired
    public SomeConfiguration(@Value("${some.username}") String someUsername,
       @Value("${some.password}") String somePassword) {
       this.someUsername = someUsername;
       this.somePassword = somePassword;
    }
...
)
}

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class SomeTest {

    private SomeConfiguration config;

    @Before
    public init() {
      config = new SomeConfiguration("foo", "bar");
    }
}
Run Code Online (Sandbox Code Playgroud)