Skip to content

Commit

Permalink
test: 사용자 단위 테스트 구현 (#57)
Browse files Browse the repository at this point in the history
* test: 사용자 단위 테스트 구현

* test: 사용자 서비스 단위 테스트 구현

* test: 부서 엔티티 테스트 구현

* test: 부서 서비스 단위 테스트 구현
  • Loading branch information
gengminy authored Dec 13, 2023
1 parent 9ddf3fe commit 248a707
Show file tree
Hide file tree
Showing 13 changed files with 447 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.testcar.car.domains.member.model.UpdateMemberRequest;
import com.testcar.car.domains.member.model.vo.MemberFilterCondition;
import com.testcar.car.domains.member.repository.MemberRepository;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -56,7 +57,7 @@ public Member updateById(Long memberId, UpdateMemberRequest request) {

/** 계정을 삭제 처리 합니다. (soft delete) */
public Member deleteById(Member member, Long memberId) {
if (member.getId() == memberId) {
if (Objects.equals(member.getId(), memberId)) {
throw new BadRequestException(ErrorCode.CANNOT_DELETE_MYSELF);
}
final Member deleteMember = this.findById(memberId);
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/com/testcar/car/common/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ private Constant() {}
public static final LocalDateTime YESTERDAY = NOW.minusDays(1L);

/** Member */
public static final String MEMBER_EMAIL = "[email protected]";
public static final String MEMBER_NAME = "홍길동";

public static final String ANOTHER_MEMBER_NAME = "동길홍";
public static final String MEMBER_EMAIL = "[email protected]";
public static final String ANOTHER_MEMBER_EMAIL = "[email protected]";
public static final String MEMBER_PASSWORD = "1234abcd@";
public static final String MEMBER_NAME = "홍길동";
public static final Role MEMBER_ROLE = Role.ADMIN;
public static final String DEPARTMENT_NAME = "모비스시스템팀";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public static void setUp() {

// then
assertEquals(mockPage, result);
verify(carRepository).findAllPageByCondition(condition, pageable);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,6 @@ public static void setUp() {

// then
assertEquals(mockPage, result);
verify(carRepository).findAllWithStocksPageByCondition(condition, pageable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public static void setUp() {

// then
assertEquals(mockPage, result);
verify(carReservationRepository).findAllPageByCondition(condition, pageable);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public static void setUp() {

// then
assertEquals(mockPage, result);
verify(carStockRepository).findAllPageByCondition(condition, pageable);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public static void setUp() {

// Then
assertEquals(mockPage, result);
verify(carTestRepository).findAllPageByCondition(condition, pageable);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.testcar.car.domains.department;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.when;

import com.testcar.car.common.MemberEntityFactory;
import com.testcar.car.common.exception.BadRequestException;
import com.testcar.car.common.exception.NotFoundException;
import com.testcar.car.domains.department.entity.Department;
import com.testcar.car.domains.department.model.CreateDepartmentRequest;
import com.testcar.car.domains.department.repository.DepartmentRepository;
import com.testcar.car.domains.department.request.DepartmentRequestFactory;
import java.util.Optional;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
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;

@ExtendWith(MockitoExtension.class)
public class DepartmentServiceTest {
@Mock private DepartmentRepository departmentRepository;
@InjectMocks private DepartmentService departmentService;

private static Department department;
private static final Long departmentId = 1L;

@BeforeAll
public static void setUp() {
department = MemberEntityFactory.createDepartment();
}

@Test
void 부서ID_DB에서_부서_엔티티를_가져온다() {
// given
when(departmentRepository.findByIdAndDeletedFalse(departmentId))
.thenReturn(Optional.of(department));

// when
final Department result = departmentService.findById(departmentId);

// then
assertNotNull(result);
assertEquals(department, result);
then(departmentRepository).should().findByIdAndDeletedFalse(departmentId);
}

@Test
void 부서_ID_해당하는_정보가_DB_존재하지_않으면_오류가_발생한다() {
// given
when(departmentRepository.findByIdAndDeletedFalse(departmentId))
.thenReturn(Optional.empty());

// when, then
Assertions.assertThrows(
NotFoundException.class,
() -> {
departmentService.findById(departmentId);
});
then(departmentRepository).should().findByIdAndDeletedFalse(departmentId);
}

@Test
void 새로운_부서를_생성한다() {
// given
final CreateDepartmentRequest request =
DepartmentRequestFactory.createDepartmentRequestFactory();
when(departmentRepository.save(any(Department.class))).thenReturn(department);
when(departmentRepository.existsByNameAndDeletedFalse(request.getName())).thenReturn(false);

// when
final Department result = departmentService.create(request);

// then
assertNotNull(result);
assertEquals(department, result);
then(departmentRepository).should().save(any(Department.class));
then(departmentRepository).should().existsByNameAndDeletedFalse(request.getName());
}

@Test
void 부서명이_중복되면_오류가_발생한다() {
// given
final CreateDepartmentRequest request =
DepartmentRequestFactory.createDepartmentRequestFactory();
when(departmentRepository.existsByNameAndDeletedFalse(request.getName())).thenReturn(true);

// when, then
Assertions.assertThrows(
BadRequestException.class,
() -> {
departmentService.create(request);
});
then(departmentRepository).should().existsByNameAndDeletedFalse(request.getName());
then(departmentRepository).shouldHaveNoMoreInteractions();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.testcar.car.domains.department.entity;

import static com.testcar.car.common.Constant.DEPARTMENT_NAME;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

public class DepartmentTest {
@Test
public void 새로운_부서를_생성한다() {
// given
final String name = DEPARTMENT_NAME;

// when
final Department result = Department.builder().name(name).build();

// then
assertThat(result).isNotNull();
assertThat(result.getName()).isEqualTo(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.testcar.car.domains.department.request;

import static com.testcar.car.common.Constant.DEPARTMENT_NAME;

import com.testcar.car.domains.department.model.CreateDepartmentRequest;

public class DepartmentRequestFactory {
private DepartmentRequestFactory() {}

public static CreateDepartmentRequest createDepartmentRequestFactory() {
return CreateDepartmentRequest.builder().name(DEPARTMENT_NAME).build();
}
}
Loading

0 comments on commit 248a707

Please sign in to comment.