Skip to content

Commit

Permalink
refactor: #76 테스트 관련 Reflection 사용 코드 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
Wo-ogie committed Mar 12, 2024
1 parent 577a61d commit 8739482
Show file tree
Hide file tree
Showing 13 changed files with 870 additions and 509 deletions.
10 changes: 2 additions & 8 deletions src/test/java/com/ajou/hertz/config/TestSecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.mockito.BDDMockito.*;

import java.lang.reflect.Constructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Set;
Expand All @@ -23,6 +22,7 @@
import com.ajou.hertz.domain.user.constant.RoleType;
import com.ajou.hertz.domain.user.dto.UserDto;
import com.ajou.hertz.domain.user.service.UserQueryService;
import com.ajou.hertz.util.ReflectionUtils;

@Import({
SecurityConfig.class,
Expand All @@ -45,13 +45,7 @@ public void securitySetUp() throws Exception {
}

private UserDto createUserDto() throws Exception {
Constructor<UserDto> userResponseConstructor = UserDto.class.getDeclaredConstructor(
Long.class, Set.class, String.class, String.class, String.class,
String.class, LocalDate.class, Gender.class, String.class, String.class,
LocalDateTime.class
);
userResponseConstructor.setAccessible(true);
return userResponseConstructor.newInstance(
return ReflectionUtils.createUserDto(
1L,
Set.of(RoleType.USER),
"[email protected]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.assertj.core.api.Assertions.*;

import java.lang.reflect.Constructor;
import java.time.LocalDate;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -49,6 +48,7 @@
import com.ajou.hertz.domain.user.constant.RoleType;
import com.ajou.hertz.domain.user.entity.User;
import com.ajou.hertz.domain.user.repository.UserRepository;
import com.ajou.hertz.util.ReflectionUtils;

@DisplayName("[Integration] Repository - Instrument")
@ActiveProfiles("test")
Expand All @@ -73,7 +73,7 @@ public InstrumentRepositoryTest(
// given
InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_DESC;
InstrumentFilterConditions filterConditions =
createInstrumentFilterConditions(InstrumentProgressStatus.SELLING);
createEmptyInstrumentFilterConditions(InstrumentProgressStatus.SELLING);
User user = userRepository.save(createUser());
List<Instrument> savedInstruments = sut.saveAll(List.of(
createBassGuitar(user),
Expand All @@ -92,7 +92,7 @@ public InstrumentRepositoryTest(
void 베이스_기타_목록을_조회한다() throws Exception {
// given
InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_DESC;
InstrumentFilterConditions filterConditions = createInstrumentFilterConditions();
InstrumentFilterConditions filterConditions = createEmptyInstrumentFilterConditions();
User user = userRepository.save(createUser());
List<Instrument> savedInstruments = sut.saveAll(List.of(
createElectricGuitar(user),
Expand All @@ -111,7 +111,7 @@ public InstrumentRepositoryTest(
void 어쿠스틱_클래식_기타_목록을_조회한다() throws Exception {
// given
InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_DESC;
InstrumentFilterConditions filterConditions = createInstrumentFilterConditions();
InstrumentFilterConditions filterConditions = createEmptyInstrumentFilterConditions();
User user = userRepository.save(createUser());
List<Instrument> savedInstruments = sut.saveAll(List.of(
createElectricGuitar(user),
Expand All @@ -132,7 +132,7 @@ public InstrumentRepositoryTest(
void 이펙터_목록을_조회한다() throws Exception {
// given
InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_DESC;
InstrumentFilterConditions filterConditions = createInstrumentFilterConditions();
InstrumentFilterConditions filterConditions = createEmptyInstrumentFilterConditions();
User user = userRepository.save(createUser());
List<Instrument> savedInstruments = sut.saveAll(List.of(
createElectricGuitar(user),
Expand All @@ -151,7 +151,7 @@ public InstrumentRepositoryTest(
void 앰프_목록을_조회한다() throws Exception {
// given
InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_DESC;
InstrumentFilterConditions filterConditions = createInstrumentFilterConditions();
InstrumentFilterConditions filterConditions = createEmptyInstrumentFilterConditions();
User user = userRepository.save(createUser());
List<Instrument> savedInstruments = sut.saveAll(List.of(
createElectricGuitar(user),
Expand All @@ -170,7 +170,7 @@ public InstrumentRepositoryTest(
void 음향_장비_목록을_조회한다() throws Exception {
// given
InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_ASC;
InstrumentFilterConditions filterConditions = createInstrumentFilterConditions();
InstrumentFilterConditions filterConditions = createEmptyInstrumentFilterConditions();
User user = userRepository.save(createUser());
List<Instrument> savedInstruments = sut.saveAll(List.of(
createElectricGuitar(user),
Expand All @@ -190,12 +190,7 @@ private Address createAddress() {
}

private User createUser() throws Exception {
Constructor<User> userConstructor = User.class.getDeclaredConstructor(
Long.class, Set.class, String.class, String.class, String.class,
String.class, LocalDate.class, Gender.class, String.class, String.class
);
userConstructor.setAccessible(true);
return userConstructor.newInstance(
return ReflectionUtils.createUser(
null,
Set.of(RoleType.USER),
"[email protected]",
Expand All @@ -210,13 +205,7 @@ private User createUser() throws Exception {
}

private ElectricGuitar createElectricGuitar(User seller) throws Exception {
Constructor<ElectricGuitar> electricGuitarConstructor = ElectricGuitar.class.getDeclaredConstructor(
Long.class, User.class, String.class, InstrumentProgressStatus.class, Address.class, Short.class,
Integer.class, Boolean.class, String.class, ElectricGuitarBrand.class, ElectricGuitarModel.class,
Short.class, GuitarColor.class
);
electricGuitarConstructor.setAccessible(true);
return electricGuitarConstructor.newInstance(
return ReflectionUtils.createElectricGuitar(
null,
seller,
"Test electric guitar",
Expand All @@ -234,13 +223,7 @@ private ElectricGuitar createElectricGuitar(User seller) throws Exception {
}

private BassGuitar createBassGuitar(User seller) throws Exception {
Constructor<BassGuitar> bassGuitarConstructor = BassGuitar.class.getDeclaredConstructor(
Long.class, User.class, String.class, InstrumentProgressStatus.class, Address.class,
Short.class, Integer.class, Boolean.class, String.class,
BassGuitarBrand.class, BassGuitarPickUp.class, BassGuitarPreAmplifier.class, GuitarColor.class
);
bassGuitarConstructor.setAccessible(true);
return bassGuitarConstructor.newInstance(
return ReflectionUtils.createBassGuitar(
null,
seller,
"Test electric guitar",
Expand All @@ -258,15 +241,7 @@ private BassGuitar createBassGuitar(User seller) throws Exception {
}

private AcousticAndClassicGuitar createAcousticAndClassicGuitar(User seller) throws Exception {
Constructor<AcousticAndClassicGuitar> acousticAndClassicGuitarConstructor =
AcousticAndClassicGuitar.class.getDeclaredConstructor(
Long.class, User.class, String.class, InstrumentProgressStatus.class, Address.class,
Short.class, Integer.class, Boolean.class, String.class,
AcousticAndClassicGuitarBrand.class, AcousticAndClassicGuitarModel.class,
AcousticAndClassicGuitarWood.class, AcousticAndClassicGuitarPickUp.class
);
acousticAndClassicGuitarConstructor.setAccessible(true);
return acousticAndClassicGuitarConstructor.newInstance(
return ReflectionUtils.createAcousticAndClassicGuitar(
null,
seller,
"Test electric guitar",
Expand All @@ -284,13 +259,7 @@ private AcousticAndClassicGuitar createAcousticAndClassicGuitar(User seller) thr
}

private Effector createEffector(User seller) throws Exception {
Constructor<Effector> effectorConstructor = Effector.class.getDeclaredConstructor(
Long.class, User.class, String.class, InstrumentProgressStatus.class, Address.class,
Short.class, Integer.class, Boolean.class, String.class,
EffectorType.class, EffectorFeature.class
);
effectorConstructor.setAccessible(true);
return effectorConstructor.newInstance(
return ReflectionUtils.createEffector(
null,
seller,
"Title",
Expand All @@ -306,13 +275,7 @@ private Effector createEffector(User seller) throws Exception {
}

private Amplifier createAmplifier(User seller) throws Exception {
Constructor<Amplifier> amplifierConstructor = Amplifier.class.getDeclaredConstructor(
Long.class, User.class, String.class, InstrumentProgressStatus.class, Address.class,
Short.class, Integer.class, Boolean.class, String.class,
AmplifierType.class, AmplifierBrand.class, AmplifierUsage.class
);
amplifierConstructor.setAccessible(true);
return amplifierConstructor.newInstance(
return ReflectionUtils.createAmplifier(
null,
seller,
"Title",
Expand All @@ -329,13 +292,7 @@ private Amplifier createAmplifier(User seller) throws Exception {
}

private AudioEquipment createAudioEquipment(User seller) throws Exception {
Constructor<AudioEquipment> audioEquipmentConstructor = AudioEquipment.class.getDeclaredConstructor(
Long.class, User.class, String.class, InstrumentProgressStatus.class, Address.class,
Short.class, Integer.class, Boolean.class, String.class,
AudioEquipmentType.class
);
audioEquipmentConstructor.setAccessible(true);
return audioEquipmentConstructor.newInstance(
return ReflectionUtils.createAudioEquipment(
null,
seller,
"Title",
Expand All @@ -349,18 +306,13 @@ private AudioEquipment createAudioEquipment(User seller) throws Exception {
);
}

private InstrumentFilterConditions createInstrumentFilterConditions() throws Exception {
Constructor<InstrumentFilterConditions> instrumentFilterConditionsConstructor =
InstrumentFilterConditions.class.getDeclaredConstructor();
instrumentFilterConditionsConstructor.setAccessible(true);
return instrumentFilterConditionsConstructor.newInstance();
private InstrumentFilterConditions createEmptyInstrumentFilterConditions() throws Exception {
return ReflectionUtils.createInstrumentFilterConditions(null);
}

private InstrumentFilterConditions createInstrumentFilterConditions(InstrumentProgressStatus progressStatus) throws
Exception {
Constructor<InstrumentFilterConditions> instrumentFilterConditionsConstructor =
InstrumentFilterConditions.class.getDeclaredConstructor(InstrumentProgressStatus.class);
instrumentFilterConditionsConstructor.setAccessible(true);
return instrumentFilterConditionsConstructor.newInstance(progressStatus);
private InstrumentFilterConditions createEmptyInstrumentFilterConditions(
InstrumentProgressStatus progressStatus
) throws Exception {
return ReflectionUtils.createInstrumentFilterConditions(progressStatus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import java.lang.reflect.Constructor;
import java.time.LocalDateTime;

import org.junit.jupiter.api.DisplayName;
Expand All @@ -24,6 +23,7 @@
import com.ajou.hertz.common.auth.service.AuthService;
import com.ajou.hertz.common.kakao.service.KakaoService;
import com.ajou.hertz.config.ControllerTestConfig;
import com.ajou.hertz.util.ReflectionUtils;
import com.fasterxml.jackson.databind.ObjectMapper;

@DisplayName("[Unit] Controller - Auth")
Expand Down Expand Up @@ -93,23 +93,11 @@ private void verifyEveryMocksShouldHaveNoMoreInteractions() {
}

private LoginRequest createLoginRequest() throws Exception {
Constructor<LoginRequest> loginRequestConstructor =
LoginRequest.class.getDeclaredConstructor(String.class, String.class);
loginRequestConstructor.setAccessible(true);
return loginRequestConstructor.newInstance(
"[email protected]",
"1q2w3e4r!"
);
return ReflectionUtils.createLoginRequest("[email protected]", "1q2w3e4r!");
}

private static KakaoLoginRequest createKakaoLoginRequest() throws Exception {
Constructor<KakaoLoginRequest> kakaoLoginRequestConstructor =
KakaoLoginRequest.class.getDeclaredConstructor(String.class, String.class);
kakaoLoginRequestConstructor.setAccessible(true);
return kakaoLoginRequestConstructor.newInstance(
"authorization-code",
"https://redirect-uri"
);
return ReflectionUtils.createKakaoLoginRequest("authorization-code", "https://redirect-uri");
}

private JwtTokenInfoDto createJwtTokenInfoDto() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.*;

import java.lang.reflect.Constructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Set;
Expand All @@ -25,6 +24,7 @@
import com.ajou.hertz.domain.user.constant.RoleType;
import com.ajou.hertz.domain.user.dto.UserDto;
import com.ajou.hertz.domain.user.service.UserQueryService;
import com.ajou.hertz.util.ReflectionUtils;

@DisplayName("[Unit] Service - Auth")
@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -90,13 +90,7 @@ private void verifyEveryMocksShouldHaveNoMoreInteractions() {
}

private LoginRequest createLoginRequest() throws Exception {
Constructor<LoginRequest> loginRequestConstructor =
LoginRequest.class.getDeclaredConstructor(String.class, String.class);
loginRequestConstructor.setAccessible(true);
return loginRequestConstructor.newInstance(
"[email protected]",
"encoded-password"
);
return ReflectionUtils.createLoginRequest("[email protected]", "encoded-password");
}

private JwtTokenInfoDto createJwtTokenInfoDto() {
Expand All @@ -107,13 +101,7 @@ private JwtTokenInfoDto createJwtTokenInfoDto() {
}

private UserDto createUserDto(long id) throws Exception {
Constructor<UserDto> userResponseConstructor = UserDto.class.getDeclaredConstructor(
Long.class, Set.class, String.class, String.class, String.class,
String.class, LocalDate.class, Gender.class, String.class, String.class,
LocalDateTime.class
);
userResponseConstructor.setAccessible(true);
return userResponseConstructor.newInstance(
return ReflectionUtils.createUserDto(
id,
Set.of(RoleType.USER),
"[email protected]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.*;

import java.lang.reflect.Constructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Optional;
Expand Down Expand Up @@ -32,6 +31,7 @@
import com.ajou.hertz.domain.user.dto.UserDto;
import com.ajou.hertz.domain.user.service.UserCommandService;
import com.ajou.hertz.domain.user.service.UserQueryService;
import com.ajou.hertz.util.ReflectionUtils;

@DisplayName("[Unit] Service - Kakao")
@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -138,13 +138,7 @@ private void verifyEveryMocksShouldHaveNoMoreInteractions() {
}

private UserDto createUserDto(long id) throws Exception {
Constructor<UserDto> userResponseConstructor = UserDto.class.getDeclaredConstructor(
Long.class, Set.class, String.class, String.class, String.class,
String.class, LocalDate.class, Gender.class, String.class, String.class,
LocalDateTime.class
);
userResponseConstructor.setAccessible(true);
return userResponseConstructor.newInstance(
return ReflectionUtils.createUserDto(
id,
Set.of(RoleType.USER),
"[email protected]",
Expand All @@ -164,21 +158,14 @@ private UserDto createUserDto() throws Exception {
}

private static KakaoLoginRequest createKakaoLoginRequest() throws Exception {
Constructor<KakaoLoginRequest> kakaoLoginRequestConstructor =
KakaoLoginRequest.class.getDeclaredConstructor(String.class, String.class);
kakaoLoginRequestConstructor.setAccessible(true);
return kakaoLoginRequestConstructor.newInstance(
return ReflectionUtils.createKakaoLoginRequest(
"authorization-code",
"https://redirect-uri"
);
}

private static KakaoTokenResponse createKakaoTokenResponse() throws Exception {
Constructor<KakaoTokenResponse> kakaoTokenResponseConstructor = KakaoTokenResponse.class.getDeclaredConstructor(
String.class, String.class, Integer.class, String.class, Integer.class
);
kakaoTokenResponseConstructor.setAccessible(true);
return kakaoTokenResponseConstructor.newInstance("bearer", "access-token", 43199, "refresh-token", 5184000);
return ReflectionUtils.createKakaoTokenResponse("bearer", "access-token", 43199, "refresh-token", 5184000);
}

private static KakaoUserInfoResponse createKakaoUserInfoResponse() {
Expand Down
Loading

0 comments on commit 8739482

Please sign in to comment.