Spring Boot Test doesn't load complex configuration properties

527 Views Asked by At

I have the following yaml configuration in a Spring Boot application:

document-types:
  de_DE:
    REPORT: ["Test"]

This is loaded using the following class and works perfectly fine when the SpringBootApplication is started (you can debug the DemoApplication.java to verify):

@Component
@ConfigurationProperties
@Data
public class DocumentTypeConfiguration {

    private Map<String, Map<String, List<String>>> documentTypes;

}

But when I execute the following test, the documentTypes aren't loaded (it's null even though the someSrting value is properly set)

@SpringBootTest(classes = {DocumentTypeConfiguration.class, DocumentTypeService.class})
class DocumentTypeServiceTest {

    @Autowired
    private DocumentTypeConfiguration documentTypeConfiguration;

    @Value("${test}")
    private String someSrting;

    @Autowired
    private DocumentTypeService documentTypeService;

    @Test
    void testFindDocumentType() {
        String documentType = "Test";
        String result = documentTypeService.getDocumentType(documentType);
        String expected = "this";
        assertEquals(expected, result);
    }

}

Any idea what I might be doing wrong? Or maybe SpringBootTest doesn't support complex types for properties?

Source code and a test can be find here: https://github.com/nadworny/spring-boot-test-properties

1

There are 1 best solutions below

0
On

This annotation was missing in the test: @EnableConfigurationProperties

So the test class looks like this:

@SpringBootTest(classes = {DocumentTypeConfiguration.class, DocumentTypeService.class})
@EnableConfigurationProperties(DocumentTypeConfiguration.class)
class DocumentTypeServiceTest {

    @Autowired
    private DocumentTypeConfiguration documentTypeConfiguration;

    @Value("${test}")
    private String someSrting;

    @Autowired
    private DocumentTypeService documentTypeService;

    @Test
    void testFindDocumentType() {
        String documentType = "Test";
        String result = documentTypeService.getDocumentType(documentType);
        String expected = "this";
        assertEquals(expected, result);
    }

}