I have a Spring Boot application where I have a couple application properties that are only needed for integration tests, not for the actual application. I’ve added these properties to a resource file in the integration tests scope and then reference those values from variables in a @TestConfiguration
class that overrides a matching @Configuration
class in the main app. If I build my project and then immediately run the tests, everything works fine. But if I make code changes anywhere within the project, the next time I try to run the tests, I get “cannot find symbol” errors for the variables that are only in the test configuration.
I’ve tried creating a matching configuration with those properties in the main app and just set them to empty values, thinking maybe that would eliminate the error, as the test configuration should be picked up when running the tests. But the result is that the empty values from the main configuration file are then read by the tests.
I’ve also tried using a test configuration that has a unique name and put my test-specific variables in there. That produces the same result, where the symbols can’t be found until rebuilding.
Examples:
# main application.properties
props:
prop-a: some value
# integration test application.properties
props:
prop-a: override some value for tests
test-only-prop: value only needed for tests
// main app sources
@Configuration
class ConfigClass {
@Value("${props.prop-a}")
public String propA;
}
// first approach
// integration test sources
@TestConfiguration
class ConfigClass {
@Value("${props.prop-a}")
public String propA;
@Value("${props.test-only-prop}")
public String testOnlyProp;
}
// test class
@Import(ConfigClass.class)
class IntegrationTests {
@Autowired
ConfigClass configValues;
@Test
void testThings() {
var propA = configValues.propA; // this always works and uses overridden value
var testOnlyProp = configValues.testOnlyProp; // after code changes, this only works after rebuild
}
}
// second approach
// integration test sources
@TestConfiguration
class TestOnlyConfigClass {
@Value("${props.test-only-prop}")
public String testOnlyProp;
}
// test class
@Import({ConfigClass.class, TestOnlyConfigClass.class})
class IntegrationTests {
@Autowired
ConfigClass configValues;
@Autowired
TestOnlyConfigClass testOnlyConfigValues;
@Test
void testThings() {
var propA = configValues.propA; // this always works and uses overridden value
var testOnlyProp = testOnlyConfigValues.testOnlyProp; // after code changes, this only works after rebuild
}
}
Is there something I’m doing wrong or failing to understand? Why do I need to rebuild after every code change in order to have the tests be able to read the test-only property?
If there’s nothing wrong with the code and it turns out that I’m just not understanding things correctly, is there a way to force an automatic rebuild every time integration tests are run? It’s just annoying that I have to rebuild every time because I often forget and walk away since my tests take a while, only to come back and find that the tests never ran because of this issue.