-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
테스트 관련 Reflection 사용 코드 개선
- Loading branch information
Showing
13 changed files
with
870 additions
and
509 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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, | ||
|
@@ -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]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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") | ||
|
@@ -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), | ||
|
@@ -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), | ||
|
@@ -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), | ||
|
@@ -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), | ||
|
@@ -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), | ||
|
@@ -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), | ||
|
@@ -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]", | ||
|
@@ -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", | ||
|
@@ -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", | ||
|
@@ -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", | ||
|
@@ -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", | ||
|
@@ -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", | ||
|
@@ -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", | ||
|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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") | ||
|
@@ -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() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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) | ||
|
@@ -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() { | ||
|
@@ -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]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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) | ||
|
@@ -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]", | ||
|
@@ -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() { | ||
|
Oops, something went wrong.