Trying to test yaml properties mapping to a pojo in Springboot 2.7.11 but the pojo keeps coming up as null.
src/test/resources/application-test.yml
configItems:
items:
- field1: name
field2: address
- field1: name
field2: address
ConfigItems.java
@Configuration
@ConfigurationProperties(prefix = "configItems")
@Getter
@Setter
public class ConfigItems {
private List<Item> items;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public static class Item {
private String field1;
private String field2;
}
}
MapFromYamTest.java
@ExtendWith(SpringExtension.class)
@SpringBootTest
@EnableConfigurationProperties(value = ConfigItems.class)
@ActiveProfiles("test")
public class MapFromYamlTest {
@Autowired
private ConfigItems configItems;
@Test
public void everythingMappedCorrectly() {
assertNotNull(configItems, "Config is null");
}
}
I've tried a number of annotation variations on the test hence why it is now using both the ExtendWith and SpringBootTest annotations. I've tried one or the other. I've tried variations of ContextConfiguration in addition to the other annotations. I just don't get why the assertion keeps failing...
The problem was with my @Test annotation...
I commented out almost the entire project and any dependencies. Finally took a look at my imports after it was still failing with nothing else remaining. I'm using Junit 5 as is obvious from the @ExtendWith annotation but what IntelliJ did was auto import
import org.junit.Test
which is junit 4
and what I needed was import org.junit.jupiter.api.Test
instead which is unit 5.
Unbelievable...