-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test: 사용자 단위 테스트 구현 * test: 사용자 서비스 단위 테스트 구현 * test: 부서 엔티티 테스트 구현 * test: 부서 서비스 단위 테스트 구현
- Loading branch information
Showing
13 changed files
with
447 additions
and
3 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
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 |
---|---|---|
|
@@ -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 = "모비스시스템팀"; | ||
|
||
|
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
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
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
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
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
102 changes: 102 additions & 0 deletions
102
src/test/java/com/testcar/car/domains/department/DepartmentServiceTest.java
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 |
---|---|---|
@@ -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(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/java/com/testcar/car/domains/department/entity/DepartmentTest.java
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 |
---|---|---|
@@ -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); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/java/com/testcar/car/domains/department/request/DepartmentRequestFactory.java
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 |
---|---|---|
@@ -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(); | ||
} | ||
} |
Oops, something went wrong.