Junit Test의 기본 Spring-Boot application.properties 설정 재정의
기본 속성이 application.properties
클래스 경로 (src / main / resources / application.properties) 의 파일에 설정된 Spring-Boot 응용 프로그램이 있습니다.
test.properties
파일에 선언 된 속성 (src / test / resources / test.properties)으로 JUnit 테스트의 일부 기본 설정을 재정의하고 싶습니다.
일반적으로 Junit 테스트를위한 전용 구성 클래스가 있습니다.
package foo.bar.test;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import(CoreConfig.class)
@EnableAutoConfiguration
public class TestConfig {
}
먼저 @PropertySource("classpath:test.properties")
TestConfig 클래스에서 사용 하면 트릭을 수행 할 것이라고 생각했지만 이러한 속성은 application.properties 설정을 덮어 쓰지 않습니다 (Spring-Boot Reference Doc- 23. Externalized Configuration 참조 ).
그런 다음 -Dspring.config.location=classpath:test.properties
테스트를 호출 할 때 사용하려고했습니다 . 성공했지만 각 테스트 실행에 대해이 시스템 속성을 설정하고 싶지 않습니다. 따라서 코드에 넣었습니다.
@Configuration
@Import(CoreConfig.class)
@EnableAutoConfiguration
public class TestConfig {
static {
System.setProperty("spring.config.location", "classpath:test.properties");
}
}
불행히도 다시는 성공하지 못했습니다.
내가 간과해야했던 application.properties
JUnit 테스트의 설정 을 무시하는 방법에 대한 간단한 해결책이 있어야합니다 test.properties
.
의 @TestPropertySource
값을 재정의 하는 데 사용할 수 있습니다 application.properties
. javadoc에서 :
테스트 특성 소스를 사용하여 시스템 및 애플리케이션 특성 소스에 정의 된 특성을 선택적으로 대체 할 수 있습니다.
예를 들면 다음과 같습니다.
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ExampleApplication.class)
@TestPropertySource(locations="classpath:test.properties")
public class ExampleApplicationTests {
}
메타 주석 을 사용 하여 구성을 외부화 할 수도 있습니다 . 예를 들면 다음과 같습니다.
@RunWith(SpringJUnit4ClassRunner.class)
@DefaultTestAnnotations
public class ExampleApplicationTests {
...
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@SpringApplicationConfiguration(classes = ExampleApplication.class)
@TestPropertySource(locations="classpath:test.properties")
public @interface DefaultTestAnnotations { }
src/test/resources/application.properties
다음 주석을 사용하면 스프링 부트가 자동으로로드 됩니다.
@RunWith(SpringRunner.class)
@SpringBootTest
So, rename test.properties
to application.properties
to utilize auto configuration.
If you *only* need to load the properties file (into the Environment) you can also use the following, as explained here
@RunWith(SpringRunner.class) @ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class)
[Update: Overriding certain properties for testing]
- Add
src/main/resources/application-test.properties
. - Annotate test class with
@ActiveProfiles("test")
.
This loads application.properties
and then application-test.properties
properties into application context for the test case, as per rules defined here.
Demo - https://github.com/mohnish82/so-spring-boot-testprops
Another approach suitable for overriding a few properties in your test, if you are using @SpringBootTest
annotation:
@SpringBootTest(properties = {"propA=valueA", "propB=valueB"})
TLDR:
So what I did was to have the standard src/main/resources/application.properties
and also a src/test/resources/application-default.properties
where i override some settings for ALL my tests.
Whole Story
I ran into the same problem and was not using profiles either so far. It seemed to be bothersome to have to do it now and remember declaring the profile -- which can be easily forgotten.
The trick is, to leverage that a profile specific application-<profile>.properties
overrides settings in the general profile. See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties.
Otherwise we may change the default property configurator name, setting the property spring.config.name=test
and then having class-path resource src/test/test.properties
our native instance of org.springframework.boot.SpringApplication
will be auto-configured from this separated test.properties, ignoring application properties;
Benefit: auto-configuration of tests;
Drawback: exposing "spring.config.name" property at C.I. layer
ref: http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
spring.config.name=application # Config file name
You can also create a application.properties file in src/test/resources where your JUnits are written.
'IT' 카테고리의 다른 글
2의 다음 거듭 제곱으로 반올림 (0) | 2020.05.28 |
---|---|
Node.js를 사용하여 JSON 파일로 데이터 쓰기 / 추가 (0) | 2020.05.28 |
Visual Studio에서 다른 폴더로 프로젝트 이동 (0) | 2020.05.28 |
Xcode에서 벡터 이미지는 어떻게 작동합니까 (예 : pdf 파일)? (0) | 2020.05.28 |
mongodb에서 ISODate를 사용한 날짜 쿼리가 작동하지 않는 것 같습니다. (0) | 2020.05.28 |