본문 바로가기

코딩(Coding)/Web

[Spring]_#2

 

HelloResponsDto

 

package kr.ac.andong.web.dto;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor

public class HelloResponseDto {

private final String name;
private final int amount;

}

 


Controller

 

package kr.ac.andong.web;

import kr.ac.andong.web.dto.HelloResponseDto;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {
@GetMapping("/")
public String helloo(){
return "오하요";
}

@GetMapping("/hello")
public String hello(){
return "안녕하세요";
}

@GetMapping("/hello/dto")
public HelloResponseDto helloDto(@RequestParam("name") String name,
@RequestParam("amount")int amount){
return new HelloResponseDto(name,amount);
}
}

 


Application

 

package kr.ac.andong;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}

 


ControllerTest

 

package kr.ac.andong.web;

import kr.ac.andong.web.Controller;
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.*;

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = Controller.class)


public class ControllerTest {
@Autowired
private MockMvc mvc;

@Test
public void hello_return() throws Exception {
String hello = "안녕하세요";

mvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string(hello));
}
@Test
public void hellodto_return() throws Exception{
String name="hello";
int amount=1000;

mvc.perform(get("/hello/dto").param("name",name)
.param("amount",String.valueOf(amount)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name",is(name)))
.andExpect(jsonPath("$.amount",is(amount)));

}
}

 


HelloControllerDtoTest

 

package kr.ac.andong.web.dto;

import org.junit.Test;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

public class HelloResponseDtoTest {
@Test
public void lombok_test(){
String name="test";
int amount=1000;

HelloResponseDto dto = new HelloResponseDto(name,amount);

assertThat(dto.getName()).isEqualTo(name);
assertThat(dto.getAmount()).isEqualTo(amount);
}
}

 


결과

 

'코딩(Coding) > Web' 카테고리의 다른 글

[Spring]_#1  (0) 2021.07.12
[jQuery]_#1  (0) 2021.07.02
HTML_5일차 [JavaScript]  (0) 2021.07.02
HTML_4일차 [JavaScript]  (0) 2021.07.01
HTML_3일차  (0) 2021.06.30