Skip to content

Commit

Permalink
Merge pull request #54 from endless-horses/feature#45-accessory-contr…
Browse files Browse the repository at this point in the history
…oller-test

Accessory Controller 테스트 코드 작성 close #45
  • Loading branch information
byeon22 authored Feb 20, 2024
2 parents 5018574 + 6885b3e commit 3efb588
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@Entity
@Getter
@Setter
@NoArgsConstructor
public class Accessory {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true, nullable = false)
private String name;
@Column(nullable = false)
private Long price;
@Column(nullable = false)
private String imageUrl;
@Column(nullable = false, columnDefinition = "TEXT")
private String explanation;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true, nullable = false)
private String name;
@Column(nullable = false)
private Long price;
@Column(nullable = false)
private String imageUrl;
@Column(nullable = false, columnDefinition = "TEXT")
private String explanation;

@OneToMany(mappedBy = "accessory")
private List<ResultAccessory> resultAccessories = new ArrayList<>();
@OneToMany(mappedBy = "accessory")
private List<ResultAccessory> resultAccessories = new ArrayList<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.endlesshorses.oot.custom.accessory.controller;

import com.endlesshorses.oot.custom.accessory.dto.AccessoryListResponseDto;
import com.endlesshorses.oot.custom.accessory.enity.Accessory;
import com.endlesshorses.oot.custom.accessory.service.AccessoryService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@SpringBootTest
@AutoConfigureMockMvc
class AccessoryControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private AccessoryService accessoryService;

private List<AccessoryListResponseDto> accessoryList;

@BeforeEach
public void setUp() {

}

@Test
@DisplayName("GET /accessories 테스트")
public void list() throws Exception {
// given
Accessory accessory1 = new Accessory();
accessory1.setId(1L);
accessory1.setName("휠 프로텍터");
accessory1.setPrice(10000L);
accessory1.setImageUrl("imageUrl1");
accessory1.setExplanation("설명1~");

Accessory accessory2 = new Accessory();
accessory2.setId(1L);
accessory2.setName("휠 림 어댑터");
accessory2.setPrice(10000L);
accessory2.setImageUrl("imageUrl2");
accessory2.setExplanation("설명2~");

AccessoryListResponseDto accessoryDto1 = new AccessoryListResponseDto(accessory1);
AccessoryListResponseDto accessoryDto2 = new AccessoryListResponseDto(accessory2);

accessoryList = Arrays.asList(accessoryDto1, accessoryDto2);

// when
Mockito.when(accessoryService.list()).thenReturn(accessoryList);

// then
mockMvc.perform(get("/api/accessories")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$[0].id").value(accessoryList.get(0).getId()))
.andExpect(jsonPath("$[0].name").value(accessoryList.get(0).getName()))
.andExpect(jsonPath("$[0].price").value(accessoryList.get(0).getPrice()))
.andExpect(jsonPath("$[0].imageUrl").value(accessoryList.get(0).getImageUrl()))
.andExpect(jsonPath("$[0].explanation").value(accessoryList.get(0).getExplanation()))
.andExpect(jsonPath("$[1].id").value(accessoryList.get(1).getId()))
.andExpect(jsonPath("$[1].name").value(accessoryList.get(1).getName()))
.andExpect(jsonPath("$[1].price").value(accessoryList.get(1).getPrice()))
.andExpect(jsonPath("$[1].imageUrl").value(accessoryList.get(1).getImageUrl()))
.andExpect(jsonPath("$[1].explanation").value(accessoryList.get(1).getExplanation()))
.andExpect(status().isOk())
.andDo(print());
}
}

0 comments on commit 3efb588

Please sign in to comment.