1

I have a validated configuration properties class.

@Data
@Configuration
@ConfigurationProperties(prefix = "notifications")
@Validated
public class NotificationConfig {
    @NotNull
    @Valid
    private NotificationMessageConfig message = new NotificationMessageConfig();
    @NotNull
    @Valid
    private SmsConfig sms = new SmsConfig();
    @NotNull
    @Valid
    private EmailConfig email = new EmailConfig();
    @NotNull
    @Valid
    private NotificationTimerConfig timer = new NotificationTimerConfig();
}

In integration tests, I am trying to autowire the bean of the class. On runtime, I have an autowired bean but this bean has null values instead of bound configuration. Note! If I remove @Validated annotation, I will have a valid bean but the config will not be validated.

@Slf4j
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@RequiredArgsConstructor
public class NotificationsIT extends BaseIntegrationTest {

    @Autowired
    private Notifications notification;
    @Autowired
    private Banks banks;
    @Autowired
    private NotificationConfig notificationConfig;

   @BeforeEach
    public void init() {
        super.init();
    // notificationConfig autowired but fields have null values
        defaultNotificationConfig = clone(notificationConfig);
    }
}
Arho Huttunen
  • 1,081
  • 9
  • 16
  • Check the 5th header of this page : https://www.baeldung.com/spring-boot-testing-configurationproperties – Ajay Negi Jan 15 '21 at 16:03
  • @AjayNegi Good article, but it is not my case( I have to use @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) To use one application instance for all integration tests... – Maksym Kmets Jan 16 '21 at 09:00
  • But I tried some things from the article like @ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class) @EnableConfigurationProperties(value = ServerConfig.class) It did't help, unfortuanetly – Maksym Kmets Jan 16 '21 at 09:05
  • @WesternGun maybe you can help.... You posted some familiar information here. https://stackoverflow.com/questions/31745168/how-to-test-classes-with-configurationproperties-and-autowired – Maksym Kmets Jan 16 '21 at 09:15
  • @MaksymKmets I think, you have the same issue like in https://stackoverflow.com/questions/55471653/spring-validated-annotation-prevents-configurationproproperties-value-injectio – saver Jan 26 '21 at 22:34
  • @saver is the issuer is familiar to [discribe in proposed question](https://stackoverflow.com/questions/55471653/spring-validated-annotation-prevents-configurationproproperties-value-injectio) – Maksym Kmets May 20 '21 at 21:36

0 Answers0