Giter Club home page Giter Club logo

web2_my-restful-service's Introduction

uri로 가변수 보내고 결과값 받기

  • Controller
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/calculate/")
@RestController
public class CalculateController {

    @GetMapping("/add/{a}/{b}")
    public Object add(@PathVariable("a") int a, @PathVariable("b") int b, Model model) {
        return ResponseEntity.ok(model.addAttribute("result", a+b));
    }
    @GetMapping("/minus/{a}/{b}")
    public Object minus(@PathVariable("a") int a, @PathVariable("b") int b, Model model) {
        return ResponseEntity.ok(model.addAttribute("result", a-b));
    }
    @GetMapping("/multiply/{a}/{b}")
    public Object multiply(@PathVariable("a") int a, @PathVariable("b") int b, Model model) {
        return ResponseEntity.ok(model.addAttribute("result", a*b));
    }
    @GetMapping("/divide/{a}/{b}")
    public Object divide(@PathVariable("a") float a, @PathVariable("b") float b, Model model) {
        return ResponseEntity.ok(model.addAttribute("result", Math.round(a/b)));
    }
}

  • Test 코드
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = CalculateController.class)
public class CalculateControllerTest {

    @Autowired
    private MockMvc mvc;

    @Test
    public void add() throws Exception {
        String a = "7";
        String b = "6";

        int result = Integer.parseInt(a) + Integer.parseInt(b);

        mvc.perform(get("/calculate/add/"+a+"/"+b))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.result", is(13)));
    }
    @Test
    public void minus() throws Exception {
        String a = "7";
        String b = "6";

        int result = Integer.parseInt(a) - Integer.parseInt(b);

        mvc.perform(get("/calculate/minus/"+a+"/"+b))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.result", is(result)));
    }
    @Test
    public void multiply() throws Exception {
        String a = "7";
        String b = "6";

        int result = Integer.parseInt(a) * Integer.parseInt(b);

        mvc.perform(get("/calculate/multiply/"+a+"/"+b))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.result", is(result)));
    }
    @Test
    public void divide() throws Exception {
        String a = "7";
        String b = "6";

        float result = Float.parseFloat(a) - Float.parseFloat(b);

        mvc.perform(get("/calculate/divide/"+a+"/"+b))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.result", is(Math.round(result))));
    }
}

  • Test 결과


  • 포스트맨 API 명세서

https://documenter.getpostman.com/view/23389689/2s7ZE4MkCz

web2_my-restful-service's People

Contributors

imkunyoung avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.