JUnit5 Test not loading properties from 'application-test.yml'

Viewed 345

I am trying to write unit tests for my service class, which has dependency to a configuration properties class:

MyService.java

@Service
@RequiredArgsConstructor
public class MyService {
  private final MyConfigProperties myConfigurationProperties;
}

MyConfigProperties.java

@ConfigurationProperties("prefix")
@Data
public class MyConfigProperties {
  int aValue;
  List<String> aList;
}

application-test.yml

prefix:
  aValue: 1
  aList: a, b, c

MyServiceTest.java

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = MyService.class)
@EnableConfigurationProperties(value = MyConfigProperties.class)
@TestPropertySource(properties = { "spring.config.location=classpath:application-test.yml" })
class MyServiceTest {
  @Autowired
  MyService myservice;

  @Test
  void testSth(){
    //some test
  }
}

When I debug the test, I see that the myConfigurationProperties injected with default values such as aValue=0 and aList=null.

What am I missing?

0 Answers
Related