Skip to content

Commit

Permalink
테스트 커버리지 60%가 넘지 않는 파일의 테스트를 작성한다. (#261)
Browse files Browse the repository at this point in the history
* test: 테스트 커버리지 60%를 넘기 위해 작성

- IngredientService, Ingredient, UserRecipeItem
- 몇몇 메서드 이름 변경

* chore: 불필요한 어노테이션 제거
  • Loading branch information
toneyparky authored Feb 13, 2021
1 parent 05f2ce6 commit e7e5968
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package com.cocktailpick.api.ingredient.controller;

import java.net.URI;
import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cocktailpick.core.ingredient.dto.IngredientRequest;
import com.cocktailpick.core.ingredient.dto.IngredientResponse;
import com.cocktailpick.core.ingredient.service.IngredientService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.util.List;

@CrossOrigin("*")
@RequiredArgsConstructor
Expand All @@ -19,8 +28,8 @@ public class IngredientController {

@PostMapping
public ResponseEntity<Void> createIngredient(@RequestBody IngredientRequest ingredientRequest) {
Long saveId = ingredientService.save(ingredientRequest);
return ResponseEntity.created(URI.create("/api/ingredients/" + saveId)).build();
Long saveId = ingredientService.save(ingredientRequest);
return ResponseEntity.created(URI.create("/api/ingredients/" + saveId)).build();
}

@GetMapping
Expand All @@ -35,14 +44,15 @@ public ResponseEntity<IngredientResponse> findIngredient(@PathVariable Long id)
}

@PutMapping("/{id}")
public ResponseEntity<Void> updateIngredient(@PathVariable Long id, @RequestBody IngredientRequest ingredientRequest) {
ingredientService.updateIngredient(id, ingredientRequest);
public ResponseEntity<Void> updateIngredient(@PathVariable Long id,
@RequestBody IngredientRequest ingredientRequest) {
ingredientService.update(id, ingredientRequest);
return ResponseEntity.ok().build();
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteIngredient(@PathVariable Long id) {
ingredientService.deleteIngredient(id);
ingredientService.delete(id);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.cocktailpick.api.ingredient.controller;

import com.cocktailpick.api.cocktail.docs.CocktailDocumentation;
import com.cocktailpick.api.common.documentation.DocumentationWithSecurity;
import com.cocktailpick.api.ingredient.docs.IngredientDocumentation;
import com.cocktailpick.core.ingredient.dto.IngredientRequest;
Expand Down Expand Up @@ -127,7 +126,7 @@ void updateIngredient() throws Exception {
@DisplayName("재료를 삭제한다.")
@Test
void deleteIngredient() throws Exception {
doNothing().when(ingredientService).deleteIngredient(any());
doNothing().when(ingredientService).delete(any());

mockMvc.perform(RestDocumentationRequestBuilders.delete("/api/ingredients/{id}", 1L)
.header("authorization", "Bearer ADMIN_TOKEN"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private Ingredient findById(Long id) {

@CachePut(value = "ingredient", key = "#id")
@Transactional
public void updateIngredient(Long id, IngredientRequest ingredientRequest) {
public void update(Long id, IngredientRequest ingredientRequest) {
Ingredient ingredient = findById(id);
Ingredient requestIngredient = ingredientRequest.toIngredient();

Expand All @@ -57,7 +57,7 @@ public void updateIngredient(Long id, IngredientRequest ingredientRequest) {

@CacheEvict(value = "ingredient", key = "#id")
@Transactional
public void deleteIngredient(Long id) {
public void delete(Long id) {
ingredientRepository.deleteById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.cocktailpick.core.common.domain.BaseTimeEntity;
import com.cocktailpick.core.userrecipe.domain.UserRecipe;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -25,7 +26,7 @@ public class UserCocktail extends BaseTimeEntity {
name = "user_cocktail_sequence_gen",
sequenceName = "user_cocktail_sequence"
)
private String id;
private Long id;

@NotBlank
private String name;
Expand All @@ -38,4 +39,12 @@ public class UserCocktail extends BaseTimeEntity {

@Embedded
private UserRecipe userRecipe = UserRecipe.empty();

@Builder
public UserCocktail(Long id, String name, String description, Long memberId) {
this.id = id;
this.name = name;
this. description = description;
this. memberId = memberId;
}
}
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());
}
}
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);
}
}
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);
}
}

0 comments on commit e7e5968

Please sign in to comment.