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

Commit

Permalink
#36 테스트 코드 작성
Browse files Browse the repository at this point in the history
- spring-boot-starter 안에 junit 및 mockito framework가 포함됨으로 기존 필요없는 의존성 삭제
- README 파일에 테스트 내용 추가
- UserServcieImplTest api 메서드 작성
  • Loading branch information
junshock5 committed Sep 21, 2020
1 parent 9717040 commit daeb192
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 14 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ USECASE
- 관리자
- 관리자는 회원관리를 위해 구매자/판매자의 상태를 관리 할 수있다.

테스트
---
- Mockito Framework를 활용하여 고립된 테스트 코드를 작성
- Jenkins CI를 적용하여 테스트 자동화
- 협업하는 동료의 소스코드에 서로 테스트코드를 작성하여 서로의 소스코드를 알 수 있도록 하고 있습니다.

ERD
---
![UsedMarket_24_20200730_39_51](https://user-images.githubusercontent.com/61732452/88840524-98ed7100-d217-11ea-9b76-8043edc17326.png)
13 changes: 0 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
Expand Down Expand Up @@ -99,13 +93,6 @@
<artifactId>spring-session-data-redis</artifactId>
</dependency>

<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<!-- AOP -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
//@RunWith(SpringRunner.class)
class UsedMarketServerApplicationTests {

@Test
Expand Down
111 changes: 111 additions & 0 deletions src/test/java/com/market/server/service/Impl/UserServiceImplTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.market.server.service.Impl;

import com.market.server.dto.UserDTO;
import com.market.server.mapper.UserProfileMapper;
import com.market.server.utils.SHA256Util;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Date;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.BDDMockito.given;

@RunWith(MockitoJUnitRunner.class)
class UserServiceImplTest {
/*
* '@Mock'이 붙은 목 객체를 해당 어노테이션이 선언된 객체에 주입할 수 있다.
* Dao객체를 주입하기 위해서는 Dao에 '@Mock'을, Service에' @InjectMocks'를 붙여주어야한다.
*/
@InjectMocks
UserServiceImpl userService;

@Mock
UserProfileMapper userProfileMapper;

// 새로운 멤버 객체를 생성하여 반환한다.
public UserDTO generateUser() {
MockitoAnnotations.initMocks(this); // mock all the field having @Mock annotation
UserDTO userDTO = new UserDTO();
userDTO.setId("textUserId");
userDTO.setPassword(SHA256Util.encryptSHA256("testPassword"));
userDTO.setName("testUserName");
userDTO.setPhone("010-1111-2222");
userDTO.setAddress("testAdress");
userDTO.setStatus(UserDTO.Status.DEFAULT);
userDTO.setCreatetime(new Date());
userDTO.setUpdatetime(new Date());
userDTO.setAddmin(false);
return userDTO;
}

@Test
public void getUserInfo() {
UserDTO userDTO = generateUser();
assertTrue("textUserId".equals(userDTO.getId()));
assertTrue("testUserName" == userDTO.getName());
}

@Test
void register() {
UserDTO userDTO = generateUser();
userProfileMapper.register(userDTO);

}

@Test
void login() {
UserDTO userDTO = generateUser();
given(userProfileMapper.findByIdAndPassword("textUserId",
SHA256Util.encryptSHA256("testPassword")))
.willReturn(userDTO);
assertThat(userService.login("textUserId", "testPassword")).isEqualTo(userDTO);
}

@Test
void isDuplicatedId() {
UserDTO userDTO = generateUser();
given(userProfileMapper.idCheck("textUserId"))
.willReturn(1);
given(userProfileMapper.idCheck("textUserId2"))
.willReturn(0);
userService.login(userDTO.getId(), userDTO.getPassword());
}

@Test
void updatePassword() {
UserDTO userDTO = generateUser();
given(userProfileMapper.updatePassword(userDTO))
.willReturn(1);
given(userProfileMapper.findByIdAndPassword(userDTO.getId(), SHA256Util.encryptSHA256("testPassword")))
.willReturn(userDTO);

userService.updatePassword(userDTO.getId(), "testPassword", "1234");
}

@Test
void updateAddress() {
UserDTO userDTO = generateUser();
given(userProfileMapper.updateAddress(userDTO))
.willReturn(1);
given(userProfileMapper.getUserProfile(userDTO.getId()))
.willReturn(userDTO);

userService.updateAddress(userDTO.getId(), "testAdress22");
}

@Test
void deleteId() {
UserDTO userDTO = generateUser();
given(userProfileMapper.findByIdAndPassword("textUserId",
SHA256Util.encryptSHA256("testPassword")))
.willReturn(userDTO);

userService.deleteId(userDTO.getId(),"testPassword");
}
}

0 comments on commit daeb192

Please sign in to comment.