[SPRING] 통합 테스트 케이스를 통한 스프링 부트, yml 속성 읽기
SPRING통합 테스트 케이스를 통한 스프링 부트, yml 속성 읽기
안녕하세요, 저는 Spring Boot를 사용하고 있습니다. Bean에 .yml 파일의 값을 주입하고 싶습니다. 통합 테스트 케이스를 작성했지만 통합 테스트 케이스에서 값을 주입하지 않는 것처럼 보입니다.
문제는 URL 값이며 keyspaceApp는 null입니다.
콩
@ConfigurationProperties(prefix="cassandra")
public class TestBean {
@Value("${urls}")
private String urls;
@Value("${keyspaceApp}")
private String app;
public void print() {
System.out.println(urls);
System.out.println(app);
}
public String getUrls() {
return urls;
}
public void setUrls(String urls) {
this.urls = urls;
}
}
통합 테스트 케이스
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestBean.class)
@IntegrationTest
public class CassandraClientTest {
@Autowired
private TestBean bean;
@Test
public void test() {
bean.print();
}
}
신청서 파일
cassandra:
urls: lllaaa.com
keyspaceApp: customer
createDevKeyspace: true
해결법
-
==============================
1.이거 한번 해봐:
이거 한번 해봐:
@SpringApplicationConfiguration(classes = TestBean.class, initializers = ConfigFileApplicationContextInitializer.class)
JavaDocs에서 :
* {@link ApplicationContextInitializer} that can be used with the * {@link ContextConfiguration#initializers()} to trigger loading of * {@literal application.properties}.
그것은 그것이 application.properties와 함께 작동하지만, 그것도 application.yml과 함께 작동해야한다고 생각한다.
-
==============================
2.내가 일하는 방법은 다음과 같습니다.
내가 일하는 방법은 다음과 같습니다.
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(initializers=ConfigFileApplicationContextInitializer.class) public class MyTestClass { @Autowired private ConfigurableApplicationContext c; @Test public void myTestMethod() { String value = c.getEnvironment().getProperty("myapp.property") ... } }
-
==============================
3.resources 폴더에 'application-test.yml'이 있다면.
resources 폴더에 'application-test.yml'이 있다면.
이것을 시도 할 수 있습니다 :
import org.springframework.test.context.ActiveProfiles; @ActiveProfiles("test")
그것으로부터 Java Docs :
* {@code ActiveProfiles} is a class-level annotation that is used to declare * which <em>active bean definition profiles</em> should be used when loading * an {@link org.springframework.context.ApplicationContext ApplicationContext} * for test classes. * * <p>As of Spring Framework 4.0, this annotation may be used as a * <em>meta-annotation</em> to create custom <em>composed annotations</em>.
-
==============================
4.SpringApplicationConfiguration은 Spring [Spring Boot v1.4.x]에서 더 이상 사용되지 않으며 [Spring Boot v1.5.x]에서 제거되었습니다. 그래서 이것은 업데이트 된 대답입니다.
SpringApplicationConfiguration은 Spring [Spring Boot v1.4.x]에서 더 이상 사용되지 않으며 [Spring Boot v1.5.x]에서 제거되었습니다. 그래서 이것은 업데이트 된 대답입니다.
MyTestClass 클래스는 다음과 같습니다.
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @ContextConfiguration(classes = MyConfiguration.class, initializers = ConfigFileApplicationContextInitializer.class) public class MyTestClass { @Autowired private MyYmlProperties myYmlProperties; @Test public void testSpringYmlProperties() { assertThat(myYmlProperties.getProperty()).isNotEmpty(); } }
MyYmlProperties 클래스는 다음과 같습니다.
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; @Configuration @ConfigurationProperties(prefix = "my") public class MyYmlProperties { private String property; public String getProperty() { return property; } public void setProperty(String property) { this.property = property; } }
내 application.yml은 다음과 같다 :
my: property: Hello
마지막으로 MyConfiguration은 정말 비어 있습니다. :-) 원하는 것을 채울 수 있습니다.
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; @Configuration @EnableConfigurationProperties(value = MyYmlProperties.class) public class MyConfiguration { }
-
==============================
5.다른 방법이 있습니다 : [Spring Boot v1.4.x]
다른 방법이 있습니다 : [Spring Boot v1.4.x]
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @BootstrapWith(SpringBootTestContextBootstrapper.class) public class CassandraClientTest { @Autowired private TestBean bean; @Test public void test() { bean.print(); } }
이는 'application.properties'파일이있는 경우에만 작동합니다.
예 : 프로젝트 :
src / main / resources / application.properties [파일은 비어있을 수 있지만 필수입니다! ] src / main / resources / application.yml [여기 당신의 실제 설정 파일입니다]
from https://stackoverflow.com/questions/27390085/spring-boot-read-yml-properties-via-integration-test-case by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] 스프링 EL 변수 목록 (0) | 2019.02.14 |
---|---|
[SPRING] 봄 - intum 문자열로 열거 형을 저장 / 검색하는 몽고 (0) | 2019.02.14 |
[SPRING] 415 지원되지 않는 Spring 어플리케이션의 POST 요청 용 MediaType (0) | 2019.02.14 |
[SPRING] / api-url에는 스프링 부트 보안에 빈 필터 목록이 있습니다. (0) | 2019.02.14 |
[SPRING] 새 스프링 데이터 JDK8 Jsr310JpaConverters가 자동으로 작동하지 않습니까? (0) | 2019.02.14 |