My ETL works flow like this
Given
- SpringBoot 3.3.3
- Maven 3.9.x
- JDK 17
- Intentionally Remove Tomcat-embedded (for specified purpose)
- pom.xml
<!--No maven-surefire-plugin was set-->
- application.yml
spring:
profiles:
active: '@spring.profiles.active@'
- application-dev.yml
spring:
main:
web-application-type: none
- DataSource.java
@Configuration
public class DataSourceConfiguration {
@Autowired
Environment environment;
@Value("${spring.profiles.active}")
String activeProfile;
//.....
@Primary
@Bean
@ConfigurationProperties("spring.datasource.img.configuration")
public DataSource imgDataSource(@Qualifier("imgDataSourceProperties") DataSourceProperties imgDataSourceProperties) {
return activeProfile.equalsIgnoreCase("dev")
? // do dev data source initilization
: // do other data source initialization;
}
Then
- it works with Mock Test, API call if
- run it with
mvn spring-boot:run -Drun.jvmArguments=-Dspring.profiles.active=dev
or run it in server with Environment variablespring.profile.active=dev
- run it with
Issue
Now I am going to create an integration test , I set a break point in DataSource.java and check the value of String activeProfile;
I found the value is always String of '@spring.profiles.active@'
.
Given the server log
The
[main] INFO [c.a.x.s.h.s.CheckEmailServiceTest:660] - The following 1 profile is active: "dev"
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
// , properties="spring.profiles.active=dev"
// when the properties was comment-out , it works , However other properties with @Value need to configured , otherwise it cannot be auto-loaded
)
@ActiveProfiles("dev") // why it does not translate to @spring.profiles.active@ //
@EnableAutoConfiguration // with or without it , it does not change the behavior of loading @ActiveProfiles
@ContextConfiguration
@ExtendWith(SpringExtension.class) // with or without it , it does not change the behavior of loading @ActiveProfiles
@Slf4j
class CheckEmailServiceTest {
private CheckEmailService checkEmailServiceUnderTest;
@BeforeEach
void setUp() {
checkEmailServiceUnderTest = new CheckEmailService();
}
}
Questions
-
How can I pass value to
spring.profiles.active
application.yml without using hard-code command line? seems@ActiveProfiles("dev")
does not work (from log file, “dev” was shown, but how cannot it load / ETL to java field of DataSource.java) ? -
May I use something like annotation way to run the test case ?