I have a spring boot app.
project deps (plugins section):
id 'org.springframework.boot' version '2.6.1'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
And I want to test on of my services that works differently based on some properties.
I have separated set of properties and its values in different profiles, e.g. profile-1
and profile-2
. Corresponding application-profile-1.yml
and application-profile-2
and placed under src/test/resources
.
Let’s say I am trying to test the service behaviour based on:
reporting:
view:
mode: <value>
where <value>
can be short, full or custom
This is my config that fails:
@SpringBootTest
public OuterTestClass {
@SpyBean
SomeDependency someDependency;
@Autowired
TypeUnderTest subject;
@Nested
@ActiveProfiles("profile-1")
public class Profile1Tests {
@Test
public void test1() {
subject.method();
// verifications
}
}
@Nested
@ActiveProfiles("profile-2")
public class Profile2Tests {
@Test
public void test2() {
subject.method();
// verifications
}
}
}
Execution throws common java.lang.IllegalStateException: Failed to load ApplicationContext ...
exception because my beans could not be instantiated with null properties.
It seems to me that the root cause is that @ActiveProfiles
is not supported on @Nested classes but outer only. It this a right assumption?
Actually what my goal is to avoid spamming enormous amount of standalone test classes with respective @ActiveProfiles
applied on them for readability reasons.
I’ve also tried to apply @TestPropertySource
on the nested classes and it doesn’t work as well.
Seeking for advises on my case.
2