テストコードの作成


テストコードの作成


既存のコードと同じパッケージ構造がテストディレクトリに表示されます.
また,基本的にはMockを利用してダミーオブジェクトを作成することが多く実現されている.
  • Calculator.java
  • package me.sungbin.springcalculator.component;
    
    import lombok.RequiredArgsConstructor;
    import org.springframework.stereotype.Component;
    
    @Component
    @RequiredArgsConstructor
    public class Calculator {
    
        private final ICalculator iCalculator;
    
        public int sum(int x, int y) {
            this.iCalculator.init();
            return this.iCalculator.sum(x, y);
        }
    
        public int minus(int x, int y) {
            this.iCalculator.init();
            return this.iCalculator.minus(x, y);
        }
    }
    
  • DollarCalculator.java
  • package me.sungbin.springcalculator.component;
    
    import lombok.RequiredArgsConstructor;
    import org.springframework.stereotype.Component;
    
    @Component
    @RequiredArgsConstructor
    public class DollarCalculator implements ICalculator {
    
        private int price = 1;
        private final MarketApi marketApi;
    
        @Override
        public void init() {
            this.price = marketApi.connect();
        }
    
        /* 의도 부분 */
        @Override
        public int sum(int x, int y) {
            x *= price;
            y *= price;
            return x + y;
        }
    
        @Override
        public int minus(int x, int y) {
            x *= price;
            y *= price;
            return x - y;
        }
    }
    
  • ICalculator.java
  • package me.sungbin.springcalculator.component;
    
    public interface ICalculator {
    
        int sum(int x, int y);
    
        int minus(int x, int y);
    
        void init();
    }
    
  • MarketApi.java
  • package me.sungbin.springcalculator.component;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class MarketApi {
    
        public int connect() {
            return 1100;
        }
    }
    
  • CalculatorApiController.java
  • package me.sungbin.springcalculator.controller;
    
    import lombok.RequiredArgsConstructor;
    import me.sungbin.springcalculator.component.Calculator;
    import me.sungbin.springcalculator.dto.Req;
    import me.sungbin.springcalculator.dto.Res;
    import org.springframework.web.bind.annotation.*;
    
    @RestController
    @RequestMapping("/api")
    @RequiredArgsConstructor
    public class CalculatorApiController {
    
        private final Calculator calculator;
    
        @GetMapping("/sum")
        public int sum(@RequestParam int x, @RequestParam int y) {
            return calculator.sum(x, y);
        }
    
        @PostMapping("/minus")
        public Res minus(@RequestBody Req req) {
            int result = calculator.minus(req.getX(), req.getY());
    
            Res res = new Res();
            res.setResult(result);
            res.setResponse(new Res.Body());
            return res;
        }
    }
    
  • Req.java
  • package me.sungbin.springcalculator.dto;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Req {
    
        private int x;
    
        private int y;
    }
    
  • Res.java
  • package me.sungbin.springcalculator.dto;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Res {
    
        private int result;
    
        private Body response;
    
        @Data
        @NoArgsConstructor
        @AllArgsConstructor
        public static class Body {
            private String resultCode = "OK";
        }
    }
    

    テストコード

  • DollarCalculatorTest.java
  • package me.sungbin.springcalculator.component;
    
    import org.junit.jupiter.api.Assertions;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.test.mock.mockito.MockBean;
    
    import static org.junit.jupiter.api.Assertions.*;
    import static org.mockito.Mockito.when;
    
    @SpringBootTest
    public class DollarCalculatorTest {
    
        @MockBean
        private MarketApi marketApi;
    
        @Autowired
        private Calculator calculator;
    
        @Test
        public void dollarCalculatorTest() {
            when(marketApi.connect()).thenReturn(3000);
    
            int sum = calculator.sum(10, 10);
            int minus = calculator.minus(10, 10);
    
            Assertions.assertEquals(60000, sum);
            Assertions.assertEquals(0, minus);
        }
    
    
    }
  • CalculatorApiControllerTest.java
  • package me.sungbin.springcalculator.controller;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    import me.sungbin.springcalculator.component.Calculator;
    import me.sungbin.springcalculator.component.DollarCalculator;
    import me.sungbin.springcalculator.component.MarketApi;
    import me.sungbin.springcalculator.dto.Req;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc;
    import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
    import org.springframework.boot.test.mock.mockito.MockBean;
    import org.springframework.context.annotation.Import;
    import org.springframework.http.MediaType;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
    import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
    import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
    
    import static org.mockito.Mockito.when;
    
    @WebMvcTest(CalculatorApiController.class)
    @AutoConfigureWebMvc
    @Import({Calculator.class, DollarCalculator.class})
    public class CalculatorApiControllerTest {
    
        @MockBean
        private MarketApi marketApi;
    
        @Autowired
        private MockMvc mockMvc;
    
        @BeforeEach
        public void init() {
            when(marketApi.connect()).thenReturn(3000);
        }
    
        @Test
        public void sumTest() throws Exception {
            // http://localhost:8080/api/sum
            mockMvc.perform(
                    MockMvcRequestBuilders.get("http://localhost:8080/api/sum")
                            .queryParam("x", "10")
                            .queryParam("y", "10")
            ).andExpect(
                    MockMvcResultMatchers.status().isOk()
            ).andExpect(
                    MockMvcResultMatchers.content().string("60000")
            ).andDo(MockMvcResultHandlers.print());
        }
    
        @Test
        public void minusTest() throws Exception {
            Req req = new Req();
            req.setX(10);
            req.setY(10);
    
            String json = new ObjectMapper().writeValueAsString(req);
    
            mockMvc.perform(
                    MockMvcRequestBuilders.post("http://localhost:8080/api/minus")
                            .contentType(MediaType.APPLICATION_JSON)
                            .content(json)
            ).andExpect(
                    MockMvcResultMatchers.status().isOk()
            ).andExpect(
                    MockMvcResultMatchers.jsonPath("$.result").value("0")
            ).andExpect(
                    MockMvcResultMatchers.jsonPath("$.response.resultCode").value("OK")
            ).andDo(MockMvcResultHandlers.print());
        }
    }