Search code examples
javaunit-testingjunityaml

How can I get value from application.yaml in test environment


I need to get a value from my application.yaml in production environment like this:

@Value("${user-service.defaultAdminId}")
    private final String DEFAULT_ADMIN_ID;

This value will be set up in userProfile variable in RegistrationService class:

private UserProfile notClientMapper(UserRegistrationForNotClientDto userRegistrationForNotClientDto) {
        PassportData passportData = passportDataMapper.toEntityDataFromRegistrationForNotClientDto(
                userRegistrationForNotClientDto);
        Client client = clientMapper.mapTo(
                userRegistrationForNotClientDto, passportData);
        UserProfile userProfile = userProfileMapper.toUserProfileFromRegistrationForNotClientDto(
                userRegistrationForNotClientDto, client);

        userProfile.setAdminId(UUID.fromString(DEFAULT_ADMIN_ID));
        userProfile.setQrData(qrDataGenerator.generateQRData());
        passwordEncoder(userRegistrationForNotClientDto.getPassword(), userProfile);
        return userProfile;
    }

Well, my issue is when the application run in production environment everything work, but I also need to pass unit test for this block of code like this:

@Test
    @DisplayName("When correct dto for registration not client then return after success registration for not client dto")
    public void returnAfterSuccessRegistrationForNotClient() {
        UserRegistrationForNotClientDto newUserNotClientDto
                = DtoCreatorForTest.getUserRegistrationForNotClientDto();
        mockTuneUpsCreateAfterSuccessRegistrationDtoForNotClient(newUserNotClientDto);

        UserAfterSuccessRegistrationForNotClientDto expected =
                DtoCreatorForTest.getAfterRegistrationNotClientDto(newUserNotClientDto);
        UserAfterSuccessRegistrationForNotClientDto actual =
                registrationService.registrationForNotClient(newUserNotClientDto);
        Mockito.verify(userProfileRepository, Mockito.times(1)).save(any());
        Assertions.assertEquals(expected, actual);
    }

And in test environment a DEFAULT_ADMIN_ID related to the value in applicatin.yaml is't set up. So, not surprisingly I get a NPE. I'm confused what can I do to fix this problem.

I try to get the value from application.yaml in test environment. Expecting to get that one. Actually getting NPE.


Solution

  • Here's the configuration properties version:

    @ConfigurationProperties(prefix="user-service")
    @Data
    class UserServiceProperties {
     private String defaultAdminId;
    }
    
    @Service
    @RequiredArgsConstructor 
    class RegistrationService {
     private final UserServiceProperties properties;
     
     private UserProfile notClientMapper(UserRegistrationForNotClientDto userRegistrationForNotClientDto) {
      ...
       userProfile.setAdminId(UUID.fromString(properties.getDefaultAdminId()));
      ...
     }
    }
    
    @ExtendWith(MockitoExtension.class)
    class RegistrationServiceTest {
     private final static String defaultId = "some-valid-uuid";
    
     @Mock
     private UserServiceProperties properties;
    
     @InjectMocks
     private RegistrationService registrationService;
    
     @BeforeEach
     void setup() {
      when(properties.getDefaultAdminId()).thenReturn(defaultId);
     }
    }