@SpringBootTest
@RunWith(SpringRunner.class)
class MemberServiceTest {
@InjectMocks
MemberService memberService;
@Mock
MemberRepository memberRepository;
@BeforeEach
void setUp() {}
@Nested
@DisplayName("회원가입")
class SignupTest {
@DisplayName("일반적인 경우")
@Test
void signupNormalTest() {
// given
SignupRequestDto signupRequest = SignupRequestDto.builder()
.email("test@google.com")
.password("12345678")
.name("홍길동")
.phone_number("010-1234-5678")
.address("테스트 주소")
.build();
when(memberRepository.save(any())).thenReturn(signupRequest.toEntity());
// when
ResponseDto signupResponse = memberService.signup(signupRequest);
MemberResponseDto signupResultData = (MemberResponseDto) signupResponse.getData();
// then
Assertions.assertThat(signupResponse).isNotNull();
Assertions.assertThat(signupResponse.isSuccess()).isEqualTo(true);
Assertions.assertThat(signupResponse.getError()).isNull();
Assertions.assertThat(signupResultData.getName()).isEqualTo("홍길동");
Assertions.assertThat(signupResultData.getEmail()).isEqualTo("test@google.com");
}
...(이하 생략)
아래는 디버깅을 해본 화면이다. 확인 결과 passwordEncoder에서 막혀 NullPointerException이 발생하는 것이었다.
아래의 코드를 추가함으로써 Mock PasswordEncoder
를 주입하여 해결했다.
@Mock
PasswordEncoder passwordEncoder;