Skip to content

Commit

Permalink
Test : 세션에 값 임시 저장 테스트 추가
Browse files Browse the repository at this point in the history
-추가된 내용 :
1. 세션에 orderId, amount 값을 임시 저장한 것이 제대로 저장되었는지 테스트
이유 : 결제 최종 승인 요청 전에 값이 변조되지 않았는지 검증하기 위함

미흡 :
결제 요청 받았을 때, 세션에 값이 저장되지 않았을 경우에 대한 예외 처리 미완
테스트 코드에 대한 부족함이 느껴져 찜찜함이 있다.
  • Loading branch information
na0th committed Oct 16, 2024
1 parent e4213bc commit 61b0401
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.tasksprints.auction.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tasksprints.auction.api.payment.PaymentController;
import com.tasksprints.auction.common.constant.ApiResponseMessages;
import com.tasksprints.auction.domain.payment.dto.request.PaymentRequest;
import com.tasksprints.auction.domain.payment.service.PaymentService;

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.data.jpa.mapping.JpaMetamodelMappingContext;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.web.servlet.MockMvc;

import java.math.BigDecimal;


import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(PaymentController.class)
@MockBean(JpaMetamodelMappingContext.class)
public class PaymentControllerTest {
@Autowired
private MockMvc mockMvc;

@MockBean
private PaymentService paymentService;

@Autowired
private ObjectMapper objectMapper;

@MockBean MockHttpSession session;
@Test
@DisplayName("결제 전 임시 값 저장")
public void 결제__임시__저장() throws Exception {
PaymentRequest.Prepare prepareRequest = new PaymentRequest.Prepare("test1", BigDecimal.valueOf(1000.00));

mockMvc.perform(post("/api/v1/payment/prepare")
.session(session)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(prepareRequest)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.message").value(ApiResponseMessages.PAYMENT_PREPARED_SUCCESS));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.tasksprints.auction.domain.payment.repository;

public class PaymentRepositoryTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.tasksprints.auction.domain.payment.service;

import com.tasksprints.auction.domain.payment.dto.request.PaymentRequest;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;

import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockHttpSession;

import java.math.BigDecimal;
import static org.assertj.core.api.Assertions.*;


@ExtendWith(MockitoExtension.class)
public class PaymentServiceImplTest {

@InjectMocks
private PaymentServiceImpl paymentService;

// @Mock
// private MockHttpSession MockSession;

@Nested
@DisplayName("결제 전 세션 임시 저장 테스트")
class 임시_저장_테스트{
@Test
void 결제_요청을_받았을__세션에_값이_저장되면_성공한다 () {
//given
MockHttpSession session = new MockHttpSession();
String orderId = "testOrderId";
BigDecimal amount = BigDecimal.valueOf(1000.00);
PaymentRequest.Prepare prepareRequest = new PaymentRequest.Prepare(orderId, amount);
//when
paymentService.prepare(session, prepareRequest);
//then
assertThat(session.getAttribute("orderId")).isEqualTo(orderId);
assertThat(session.getAttribute("amount")).isEqualTo(amount);
}
@Test
void 결제_요청을_받았을__세션에_값이_저장되지_않으면_예외_처리 () {
//given
//when
//then
}
}
}

0 comments on commit 61b0401

Please sign in to comment.