본문 바로가기

공부/스프링 부트와 AWS로 혼자 구현하는 웹서비스

[이동욱스프링책]java.lang.ClassCastException: class org.springframework.test.web.servlet.result.JsonPathResultMatchers cannot be cast to class org.springframework.test.web.servlet.ResultMatcher (org.springframework.test.web.servlet.result.JsonPat..

package com.jojoldu.book.springboot.web;

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.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


@RunWith(SpringRunner.class)
@WebMvcTest(controllers = HelloController.class)
public class HelloControllerTest {
    @Autowired
    private MockMvc mvc;

    @Test
    public void hello가_리턴된다() throws Exception{
        String hello = "hello";

        mvc.perform(get("/hello")).andExpect(status().isOk()).andExpect(content().string(hello));
    }

    @Test
    public void helloDto가_리턴된다() throws Exception {
        String name = "hello";
        int amount = 10000;

        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)));
    }
}

jsonPath 뒤에 사용한 is 라는 matcher 메소드가 ByteBuddy에서 온 탓
hemcrest가 제공하는 Matchers를 써야함