Skip to content

Commit

Permalink
test: fileUpload 테스트코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
sdj3261 committed Dec 25, 2023
1 parent b7a41a9 commit c5fc006
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 26 deletions.
11 changes: 7 additions & 4 deletions src/test/java/com/example/demo/Fixtures.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ public static Product product() {
public static Product product(int number) {
ProductId productId = new ProductId("012300000000" + number);
return new Product(
productId, "Product #" + number, new Money(123_000L));
productId, "Product #" + number,
"IMAGE_URL",
new Money(123_000L)
);
}

public static Cart cart() {
return cart(List.of());
}
public static Cart cart() {
return cart(List.of());
}

public static Cart cart(List<Product> products) {
CartId cartId = new CartId("0124000000001");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ void setUp() {
@Test
void createProduct() {
String name = "제-품";
String imageUrl = "IMAGE_URL";
Money price = new Money(100_000L);

Product product = createProductService.createProduct(name, imageUrl, price);
Product product = createProductService
.createProduct(name, imageUrl, price);

assertThat(product.name()).isEqualTo(name);
assertThat(product.imageUrl()).isEqualTo(imageUrl);
assertThat(product.price()).isEqualTo(price);

verify(productRepository).save(product);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@
import com.example.demo.application.product.GetProductListService;
import com.example.demo.dtos.ProductListDto;
import com.example.demo.models.Money;
import com.example.demo.utils.ImageStorage;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;

import java.io.FileInputStream;
import java.util.List;

import static com.example.demo.controllers.helpers.ResultMatchers.contentContains;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(ProductController.class)
Expand All @@ -34,39 +36,42 @@ class ProductControllerTest {
@MockBean
private CreateProductService createProductService;

@MockBean
private ImageStorage imageStorage;

@Test
@DisplayName("GET /products")
void list() throws Exception {
ProductListDto.ProductDto productDto =
new ProductListDto.ProductDto("test-id", "제품", 100_000L);
new ProductListDto
.ProductDto("test-id", "제품", "IMAGE_URL", 100_000L);

given(getProductListService.getProductListDto()).willReturn(
new ProductListDto(List.of(productDto)));
new ProductListDto(List.of(productDto)));

mockMvc.perform(get("/products"))
.andExpect(status().isOk())
.andExpect(contentContains("제품"));
.andExpect(status().isOk())
.andExpect(contentContains("제품"));
}

@Test
@DisplayName("POST /products")
void create() throws Exception {
String json = String.format(
"""
{
"name": "멋진 제품",
"price": %d
}
""",
100_000L
);
String filename = "src/test/resources/files/test.jpg";

MockMultipartFile file = new MockMultipartFile(
"image", "test.jpg", "image/jpeg",
new FileInputStream(filename));

given(imageStorage.save(file)).willReturn("data/image.jpg");

mockMvc.perform(post("/products")
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isCreated());
mockMvc.perform(multipart("/products")
.file(file)
.param("name", "신제품")
.param("price", String.valueOf(100_000L)))
.andExpect(status().isCreated());

verify(createProductService)
.createProduct("멋진 제품", imageUrl, new Money(100_000L));
.createProduct("신제품", "data/image.jpg", new Money(100_000L));
}
}
7 changes: 6 additions & 1 deletion src/test/java/com/example/demo/models/ProductTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
class ProductTest {
@Test
void creation() {
Product product = Product.create("제품명", imageUrl, new Money(123_456L));
Product product = Product.create(
"제품명",
"IMAGE_URL",
new Money(123_456L)
);

assertThat(product.id()).isNotNull();
assertThat(product.name()).isEqualTo("제품명");
assertThat(product.imageUrl()).isEqualTo("IMAGE_URL");
assertThat(product.price()).isEqualTo(new Money(123_456L));
}
}

0 comments on commit c5fc006

Please sign in to comment.