0

I have implemented a map in my yaml file as below

  replacement:
        ID:
          0000000016: 9000000011
          0000000013: 8000000014
          0000000002: 9000000000

Config class :-

    @Configuration
@NoArgsConstructor
@AllArgsConstructor
@ConfigurationProperties(prefix = "replacement")
@Getter
@Setter
public class SourceConfig {
     private Map<String, String> ID = new HashMap<>();

}

Test class:-

@ExtendWith(SpringExtension.class)
@EnableConfigurationProperties(value = SourceConfig.class)
@TestPropertySource("classpath:yaml file name")
class SourceConfigTest {

    @Autowired
    private SourceConfig sourceconfig;

    @Test
    void getID() {
        assertThat(sourceconfig).isNotNull();
        assertTrue(sourceconfig.getID().containsKey("0000000016"));
    }
}


When I run the test class sourceconfig.getID() throws a null pointer exception. Please help/suggest

I expected the assert to be true and also fetch my values from map

  • is your yaml file in the test classpath ? – jhamon Jul 10 '23 at 11:44
  • I don't think you can load yaml with ordinary @TestPropertySource. It is for .properties format. – ILya Cyclone Jul 10 '23 at 12:16
  • ok thankyou, so how can we test simply the contents of the map I implemented? is it possible to do in a test class – akshay ithape Jul 10 '23 at 12:35
  • As mentioned by @ILyaCyclone, neither `@PropertySource` nor `@TestPropertySource` support yaml files. Workarounds for this issue are described in the duplicate. Also, be aware that the keys in your YAML file are treated as numbers. You probably want to surround them with quotes, for example `'0000000016'`. – g00glen00b Jul 10 '23 at 13:57

0 Answers0