Skip to content
This repository was archived by the owner on Aug 13, 2022. It is now read-only.

#59 회원 단위테스트 추가 #124

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
import com.younghun.hibusgo.dto.AccountDto;
import com.younghun.hibusgo.dto.PasswordDto;
import com.younghun.hibusgo.dto.PaymentDto;
import com.younghun.hibusgo.dto.PaymentMeansDto;
import com.younghun.hibusgo.dto.ProfileDto;
import com.younghun.hibusgo.dto.ReservationDto;
import com.younghun.hibusgo.service.AccountService;
import com.younghun.hibusgo.service.LoginService;
import com.younghun.hibusgo.service.MileageService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

@Mapper
public interface AccountMapper {

Account findById(long id);

void addAccount(Account account);
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/mapper/account.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,8 @@
AND status != 'DELETE'
</update>

<delete id="deleteAccountAll">
DELETE FROM account;
</delete>

</mapper>
8 changes: 0 additions & 8 deletions src/main/resources/mapper/charge.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,5 @@
AND status != 'DELETE'
</update>

<update id="deleteCharge">
UPDATE charge
SET status = 'DELETE'
AND updated_at = NOW()
WHERE id = #{id}
AND status != 'DELETE'
</update>

</mapper>

This file was deleted.

138 changes: 138 additions & 0 deletions src/test/java/com/younghun/hibusgo/service/AccountServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package com.younghun.hibusgo.service;

import com.younghun.hibusgo.aop.LoginCheck.UserLevel;
import com.younghun.hibusgo.domain.Account;
import com.younghun.hibusgo.domain.DataStatus;
import com.younghun.hibusgo.mapper.AccountMapper;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.crypto.password.PasswordEncoder;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;

@ExtendWith(MockitoExtension.class)
class AccountServiceTest {

@InjectMocks
private AccountService accountService;

@Mock
private AccountMapper accountMapper;

@Mock
private PasswordEncoder passwordEncoder;

Account account;

final String userId = "[email protected]";
final String password = "younghun123!";

@BeforeEach
public void setUp() {
account = Account.builder()
.userId("[email protected]")
.password("younghun123!")
.name("younghun")
.phoneNumber("010-6769-5355")
.status(DataStatus.DEFAULT)
.userLevel(UserLevel.USER)
.build();
}

@Test
public void 아이디로_회원_조회() throws Exception {
//given
given(accountMapper.findById(1L)).willReturn(account);

//when
final Account addedAccount = accountService.findById(1L);

//then
assertEquals(account.getId(), addedAccount.getId());
}

@Test
public void 아이디로_회원_존재_확인() throws Exception {
//given
given(accountMapper.existsById(1L)).willReturn(true);

//when
final boolean existsById = accountService.existsById(1L);

//then
assertTrue(existsById);
}

@Test
public void 회원_추가() throws Exception{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기선 메소드명이 한글로 되어있는데 이것보다는 junit5에서 지원하는 테스트의 이름을 지을 수 있는 기능을 사용해보세요~

//when
accountService.addAccount(account);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서는 passwordEncodeCopyAccount 호출을 통해 새 객체를 insert하는것 같은데 해당 동작에 대한 커버가 되어있지 않네요. 다른 부분도 비즈니스로직에서 입력값에 대한 출력, 예외 등의 시나리오가 여러개가 있을 수 있습니다.
이런 시나리오들을 최대한 모두 정의해서 커버해주세요.

그럼 메소드명도 시나리오를 설명하는 형식으로 바뀌게 되겠죠~ (ex. 회원을 추가할때는 비밀번호를 암호화해서 insert 시킨다 등)


//then
then(accountMapper)
.should()
.addAccount(any(Account.class));

}

@Test
public void 아이디_비밀번호로_회원_조회() throws Exception{
//given
String encodePassword = passwordEncoder.encode(password);
given(accountMapper.findByUserIdAndPassword(userId, encodePassword))
.willReturn(account);

//when
final Optional<Account> addedAccount = accountService.findByUserIdAndPassword(userId, password);

//then
assertEquals(userId, addedAccount.get().getUserId());
}

@Test
public void 회원_삭제() throws Exception{
//when
accountService.deleteAccount(1L);

//then
then(accountMapper)
.should()
.deleteAccount(anyLong());
}

@Test
public void 비밀번호_수정() throws Exception{
//given
String encodePassword = passwordEncoder.encode(password);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

passwordEncoder는 mock 객체인데 given-willReturn 처리가 안되어있네요. encoder의 동작도 정의를 해줘야합니다~


//when
accountService.updatePassword(1L, password);

//then
then(accountMapper)
.should()
.updatePassword(1L, encodePassword);
}

@Test
public void 회원정보_수정() throws Exception{
//when
accountService.updateAccountInfo(account);

//then
then(accountMapper)
.should()
.updateAccountInfo(account);
}

}