junit 如何在测试环境中从application.yaml中获取价值

5rgfhyps  于 2023-03-30  发布在  其他
关注(0)|答案(2)|浏览(142)

我需要在生产环境中从我的application.yaml中获取一个值,如下所示:

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

此值将在RegistrationService类的userProfile变量中设置:

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;
    }

好吧,我的问题是当应用程序在生产环境中运行时,一切都正常,但我还需要通过如下代码块的单元测试:

@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);
    }

在测试环境中,没有设置与applicatin.yaml中的值相关的DEFAULT_ADMIN_ID。因此,毫不奇怪,我得到了一个NPE。我很困惑,我该怎么做才能解决这个问题。
我尝试在测试环境中从application.yaml中获取值。期望得到那个。实际上得到了NPE。

6ljaweal

6ljaweal1#

下面是配置属性版本:

@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);
 }
}
tktrz96b

tktrz96b2#

您可以在构造函数中设置值,而不是将注解附加到字段。

@Service
class RegistrationService {
 private final UserProfileRepository userProfileRepository;
 private final String defaultAdminId;

 public RegistrationService(UserProfileRepository userProfileRepository,
                            @Value("${user-service.defaultAdminId}") String defaultAdminId) {
  this.userProfileRepository = userProfileRepository;
  this.defaultAdminId = defaultAdminId;
 }
}

然后,您可以在测试中自由选择管理员ID,而不必从application.yaml访问实际值。

class RegistrationServiceTest {
 private final static String defaultId = "some-valid-uuid";

 private UserProfileRepository userProfileRepository;

 private RegistrationService registrationService;

 @BeforeEach
 void setup() {
  userProfileRepository = mock(UserProfileRepository.class); // or use @Mock etc
  registrationService = new RegistrationService(userProfileRepository, defaultId);
 }
}

相关问题