환경변수로 등록한 녀석을 test code에서 적용하기

leverest96·2023년 1월 4일
0

Trouble Shooting

목록 보기
7/20
post-thumbnail

문제 인지

Profile Service에서 method에 대한 test code를 짜던 도중 test code에서 해당 service에서 필드로 등록된 환경변수로 등록한 녀석을 데려와야하는데 데려오지 못하는 경우가 발생했다.

문제 원인

해당 configuration에서 field로 환경변수를 가져온 property를 가지고 있으나 test code에서는 보통 @InjectMocks으로 해당 configuration을 데려오기 때문에 해당 property를 따로 주입하지 못하는 경우가 발생했다.

이 때, property에 대한 녀석을 configuration에서만 가져올 수 있는 이유는 property는 Application.java에서 bean으로 등록이 되기 때문에 bean으로 등록된 class에 대해서만 DI가 가능하기 때문이다.

문제 해결

따라서, 해당 property를 주입하면서 configuration을 가져오기 위해서는 @InjectMocks를 사용하지 않고 @BeforeEach 메소드에서 직접 호출해줘야한다.

  • 이전

    @InjectMocks
    private ProfileService profileService;
  • 이후

    private ProfileService profileService;
    
    @BeforeEach
        void beforeEach() {
            ...
    
            awsStorageProperties = new AwsStorageProperties(FactoryPreset.PICTURE);
    
            profileService = new ProfileService(
                    profileRepository,
                    awsS3Manager,
                    awsStorageProperties,
                    modelMapper
            );
        }
profile
응애 난 애기 개발자

0개의 댓글