-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
테스트 커버리지 60%가 넘지 않는 파일의 테스트를 작성한다. (#261)
* test: 테스트 커버리지 60%를 넘기 위해 작성 - IngredientService, Ingredient, UserRecipeItem - 몇몇 메서드 이름 변경 * chore: 불필요한 어노테이션 제거
- Loading branch information
1 parent
05f2ce6
commit e7e5968
Showing
7 changed files
with
220 additions
and
15 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
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
29 changes: 29 additions & 0 deletions
29
...cktailpick-core/src/test/java/com/cocktailpick/core/ingredient/domain/IngredientTest.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,29 @@ | ||
package com.cocktailpick.core.ingredient.domain; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class IngredientTest { | ||
@DisplayName("재료를 수정한다.") | ||
@Test | ||
void update() { | ||
Ingredient ingredient = Ingredient.builder() | ||
.id(1L) | ||
.name("소주") | ||
.color("#000000") | ||
.abv(17.8) | ||
.build(); | ||
|
||
Ingredient updatingIngredient = Ingredient.builder() | ||
.name(ingredient.getName()) | ||
.color(ingredient.getColor()) | ||
.abv(16.5) | ||
.build(); | ||
|
||
ingredient.update(updatingIngredient); | ||
|
||
assertThat(ingredient.getAbv()).isEqualTo(updatingIngredient.getAbv()); | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
...ck-core/src/test/java/com/cocktailpick/core/ingredient/service/IngredientServiceTest.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,122 @@ | ||
package com.cocktailpick.core.ingredient.service; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import com.cocktailpick.core.ingredient.domain.Ingredient; | ||
import com.cocktailpick.core.ingredient.domain.IngredientRepository; | ||
import com.cocktailpick.core.ingredient.dto.IngredientRequest; | ||
import com.cocktailpick.core.ingredient.dto.IngredientResponse; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class IngredientServiceTest { | ||
private IngredientService ingredientService; | ||
|
||
@Mock | ||
private IngredientRepository ingredientRepository; | ||
|
||
private IngredientRequest createRequest; | ||
|
||
private Ingredient ingredient; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
ingredientService = new IngredientService(ingredientRepository); | ||
|
||
createRequest = IngredientRequest.builder() | ||
.name("소주") | ||
.color("#000000") | ||
.abv(17.6) | ||
.build(); | ||
|
||
ingredient = Ingredient.builder() | ||
.id(1L) | ||
.name("소주") | ||
.color("#000000") | ||
.abv(17.6) | ||
.build(); | ||
} | ||
|
||
@DisplayName("재료를 생성한다.") | ||
@Test | ||
void save() { | ||
when(ingredientRepository.save(any())).thenReturn(ingredient); | ||
|
||
ingredientService.save(createRequest); | ||
|
||
verify(ingredientRepository).save(any()); | ||
} | ||
|
||
@DisplayName("모든 재료를 조회한다.") | ||
@Test | ||
void findAll() { | ||
Ingredient beer = Ingredient.builder() | ||
.id(2L) | ||
.name("맥주") | ||
.color("#222222") | ||
.abv(5.0) | ||
.build(); | ||
|
||
when(ingredientRepository.findAll()).thenReturn(Arrays.asList(ingredient, beer)); | ||
|
||
List<IngredientResponse> ingredients = ingredientService.findAll(); | ||
|
||
assertThat(ingredients).extracting("name") | ||
.containsExactly(ingredient.getName(), beer.getName()); | ||
} | ||
|
||
@DisplayName("id로 재료를 조회한다.") | ||
@Test | ||
void findIngredient() { | ||
when(ingredientRepository.findById(any())).thenReturn(Optional.of(ingredient)); | ||
|
||
IngredientResponse response = ingredientService.findIngredient(1L); | ||
|
||
assertAll( | ||
() -> assertThat(response.getId()).isEqualTo(ingredient.getId()), | ||
() -> assertThat(response.getName()).isEqualTo(ingredient.getName()), | ||
() -> assertThat(response.getColor()).isEqualTo(ingredient.getColor()), | ||
() -> assertThat(response.getAbv()).isEqualTo(ingredient.getAbv()) | ||
); | ||
} | ||
|
||
@Test | ||
void update() { | ||
IngredientRequest updateRequest = IngredientRequest.builder() | ||
.name(createRequest.getName()) | ||
.color(createRequest.getColor()) | ||
.abv(16.5) | ||
.build(); | ||
|
||
when(ingredientRepository.findById(any())).thenReturn(Optional.of(ingredient)); | ||
|
||
ingredientService.update(ingredient.getId(), updateRequest); | ||
|
||
assertAll( | ||
() -> assertThat(ingredient.getId()).isEqualTo(1L), | ||
() -> assertThat(ingredient.getName()).isEqualTo(updateRequest.getName()), | ||
() -> assertThat(ingredient.getColor()).isEqualTo(updateRequest.getColor()), | ||
() -> assertThat(ingredient.getAbv()).isEqualTo(updateRequest.getAbv()) | ||
); | ||
} | ||
|
||
@DisplayName("재료를 삭제한다.") | ||
@Test | ||
void delete() { | ||
ingredientService.delete(1L); | ||
|
||
verify(ingredientRepository).deleteById(1L); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...ilpick-core/src/test/java/com/cocktailpick/core/userrecipe/domain/UserRecipeItemTest.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,36 @@ | ||
package com.cocktailpick.core.userrecipe.domain; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.cocktailpick.core.ingredient.domain.Ingredient; | ||
import com.cocktailpick.core.usercocktail.domain.UserCocktail; | ||
|
||
class UserRecipeItemTest { | ||
@DisplayName("유저 레시피를 생성한다.") | ||
@Test | ||
void create() { | ||
Ingredient ingredient = Ingredient.builder() | ||
.id(1L) | ||
.name("소주") | ||
.color("#000000") | ||
.abv(17.8) | ||
.build(); | ||
|
||
UserCocktail userCocktail = UserCocktail.builder() | ||
.id(1L) | ||
.name("막소사") | ||
.description("막걸리와 소주와 사이다") | ||
.memberId(1L) | ||
.build(); | ||
|
||
UserRecipeItem userRecipeItem = UserRecipeItem.builder() | ||
.ingredient(ingredient) | ||
.userCocktail(userCocktail) | ||
.build(); | ||
|
||
assertThat(userRecipeItem).isInstanceOf(UserRecipeItem.class); | ||
} | ||
} |