Skip to content

Commit

Permalink
🚑 Fix: 코드 변동에 따른 테스트코드 수정 및 불필요한 import 정리 (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
win-luck committed Nov 12, 2023
1 parent e554b1c commit 9cc4ddb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@
import org.springframework.web.context.WebApplicationContext;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@WebMvcTest(controllers = FoodController.class)
public class FoodControllerTest {
class FoodControllerTest {

@Autowired
private MockMvc mockMvc;
Expand Down Expand Up @@ -131,12 +130,20 @@ void testGetFoodListByDate() throws Exception {
@WithMockUser("test")
void testUpdateFood() throws Exception {
//Given
int yy = 2023;
int dd = 22;
int mm = 12;
LocalDate date = LocalDate.of(yy, mm, dd);

ApiResponse<Void> expectedResponse = ApiResponse.success(null, ResponseCode.FOOD_UPDATE_SUCCESS.getMessage());
UpdateFoodDto updateFoodDto = UpdateFoodDto.of(testFoodId, testUserId, "testFood", testBaseNutrition);
String json = mapper.writeValueAsString(updateFoodDto);

mockMvc.perform(MockMvcRequestBuilders
.post("/api/food/update")
.param("yy", String.valueOf(date.getYear()))
.param("mm", String.valueOf(date.getMonthValue()))
.param("dd", String.valueOf(date.getDayOfMonth()))
.contentType(MediaType.APPLICATION_JSON)
.content(json)
.accept(MediaType.APPLICATION_JSON))
Expand All @@ -152,10 +159,18 @@ void testUpdateFood() throws Exception {
@WithMockUser("test")
void testDeleteFood() throws Exception {
//Given
int yy = 2023;
int dd = 22;
int mm = 12;
LocalDate date = LocalDate.of(yy, mm, dd);

ApiResponse<Void> expectedResponse = ApiResponse.success(null, ResponseCode.FOOD_DELETE_SUCCESS.getMessage());

mockMvc.perform(MockMvcRequestBuilders
.delete("/api/food/{foodId}/delete", testFoodId)
.param("yy", String.valueOf(date.getYear()))
.param("mm", String.valueOf(date.getMonthValue()))
.param("dd", String.valueOf(date.getDayOfMonth()))
.header("userId", testUserId)
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class UserControllerTest {
void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
testUser.setId(testUserId);
when(userService.getSimpleUserInfo(testUserId)).thenReturn(ResponseSimpleUserDto.of(testUser.getName(), testUser.getImage(), 100.0));
when(userService.getSimpleUserInfo(testUserId)).thenReturn(ResponseSimpleUserDto.of(testUser.getName(), testUser.getImage()));
when(userService.getUserInfo(testUserId)).thenReturn(ResponseUserDto.from(testUser));
}

Expand All @@ -69,7 +69,7 @@ void setUp() {
void testGetSimpleUserInfo() throws Exception {
// Given
ApiResponse<ResponseSimpleUserDto> expectedResponse = ApiResponse.success(
ResponseSimpleUserDto.of(testUser.getName(), testUser.getImage(), 100.0), ResponseCode.USER_CREATE_SUCCESS.getMessage());
ResponseSimpleUserDto.of(testUser.getName(), testUser.getImage()), ResponseCode.USER_CREATE_SUCCESS.getMessage());

// When & Then
mockMvc.perform( MockMvcRequestBuilders
Expand All @@ -79,8 +79,7 @@ void testGetSimpleUserInfo() throws Exception {
.andExpect(MockMvcResultMatchers.jsonPath("$.header.code").value(expectedResponse.getHeader().getCode()))
.andExpect(MockMvcResultMatchers.jsonPath("$.header.message").value(expectedResponse.getHeader().getMessage()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data.name").value(expectedResponse.getData().getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data.image").value(expectedResponse.getData().getImage()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data.nutritionScore").value(expectedResponse.getData().getNutritionScore()));
.andExpect(MockMvcResultMatchers.jsonPath("$.data.image").value(expectedResponse.getData().getImage()));
}

@DisplayName("회원정보 조회")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.diareat.diareat.user.domain.BaseNutrition;
import com.diareat.diareat.user.domain.User;
import com.diareat.diareat.user.dto.response.ResponseRankUserDto;
import com.diareat.diareat.user.dto.response.ResponseSimpleUserDto;
import com.diareat.diareat.user.repository.FollowRepository;
import com.diareat.diareat.user.repository.UserRepository;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -92,7 +91,7 @@ void testUpdateFood() {

//when
BaseNutrition testChangedBaseNutrition = BaseNutrition.createNutrition(2,3,4,5);
foodService.updateFood(UpdateFoodDto.of(food.getId(), 1L,"testChangedFood", testChangedBaseNutrition));
foodService.updateFood(UpdateFoodDto.of(food.getId(), 1L,"testChangedFood", testChangedBaseNutrition), LocalDate.of(2010, 1, 1));


assertEquals("testChangedFood", food.getName());
Expand All @@ -114,7 +113,7 @@ void testDeleteFood() {
given(foodRepository.existsByIdAndUserId(food.getId(), 1L)).willReturn(true);

//when
foodService.deleteFood(food.getId(), 1L);
foodService.deleteFood(food.getId(), 1L, LocalDate.of(2010, 1, 1));

verify(foodRepository, times(1)).deleteById(food.getId());
}
Expand Down

0 comments on commit 9cc4ddb

Please sign in to comment.