• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

WebMvcConfigurer 1부 Formatter - localhost:8080/hello?name=keesun

20.05.31 20:14 작성 조회수 155

0

package me.whiteship.web_mvc_onfigurer;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SampleController {

@GetMapping("/hello")
public String hello(@RequestParam("name") Person person) {
return "hello " + person.getName();
}
}

package me.whiteship.web_mvc_onfigurer;

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.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;

@RunWith(SpringRunner.class)
@WebMvcTest
public class SampleControllerTest {

@Autowired
MockMvc mockMvc;

@Test
public void hello() throws Exception {
this.mockMvc.perform(get("/hello")
.param("name", "keesun"))
.andDo(print())
.andExpect(content().string("hello keesun"));
}

}

package me.whiteship.web_mvc_onfigurer;

import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
}

package me.whiteship.web_mvc_onfigurer;

public class Person {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

package me.whiteship.web_mvc_onfigurer;

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.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;

@RunWith(SpringRunner.class)
@WebMvcTest
public class SampleControllerTest {

@Autowired
MockMvc mockMvc;

@Test
public void hello() throws Exception {
this.mockMvc.perform(get("/hello")
.param("name", "keesun"))
.andDo(print())
.andExpect(content().string("hello keesun"));
}

}

junit 4로 테스트 결과 아래와 같이 에러가 발생합니다.

Resolved Exception:

             Type = org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException

MockHttpServletRequest:

      HTTP Method = GET

      Request URI = /hello

       Parameters = {name=[keesun]}

          Headers = []

             Body = <no character encoding set>

    Session Attrs = {}

Handler:

             Type = me.whiteship.web_mvc_onfigurer.SampleController

           Method = me.whiteship.web_mvc_onfigurer.SampleController#hello(Person)

Async:

    Async started = false

     Async result = null

Resolved Exception:

             Type = org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException

ModelAndView:

        View name = null

             View = null

            Model = null

FlashMap:

       Attributes = null

MockHttpServletResponse:

           Status = 500

    Error message = null

          Headers = []

     Content type = null

             Body = 

    Forwarded URL = null

   Redirected URL = null

          Cookies = []

MockHttpServletRequest:

      HTTP Method = GET

      Request URI = /hello

       Parameters = {name=[keesun]}

          Headers = []

             Body = <no character encoding set>

    Session Attrs = {}

Handler:

             Type = me.whiteship.web_mvc_onfigurer.SampleController

           Method = me.whiteship.web_mvc_onfigurer.SampleController#hello(Person)

Async:

    Async started = false

     Async result = null

Resolved Exception:

             Type = org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException

ModelAndView:

        View name = null

             View = null

            Model = null

FlashMap:

       Attributes = null

MockHttpServletResponse:

           Status = 500

    Error message = null

          Headers = []

     Content type = null

             Body = 

    Forwarded URL = null

   Redirected URL = null

          Cookies = []

java.lang.AssertionError: Response content 

Expected :hello keesun

Actual   :

<Click to see difference>

소스 확인 좀 부탁드려도 될까요?

답변 1

답변을 작성해보세요.

0

name이라는 문자열( String)을 Person으로 변환할 수 있는 코드가 안보이네요. 그래서 MethodArgumentConversionNotSupportedException 이런 에러가 발생한거 같네요.