Portfolio/Spring Boot

스프링 부트 9일차 - Spring-Boot-Devtools, 스프링 웹 MVC

Foo 2019. 4. 9. 22:55
728x90

1. 사용하려면 다음 의존성 추가

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>


2. 다음과 같이 properties를 설정한다.

static {
Map<String, Object> properties = new HashMap<>();
properties.put("spring.thymeleaf.cache", "false");
properties.put("spring.freemarker.cache", "false");
properties.put("spring.groovy.template.cache", "false");
properties.put("spring.mustache.cache", "false");
properties.put("server.servlet.session.persistent", "true");
properties.put("spring.h2.console.enabled", "true");
properties.put("spring.resources.cache.period", "0");
properties.put("spring.resources.chain.cache", "false");
properties.put("spring.template.provider.cache", "false");
properties.put("spring.mvc.log-resolved-exception", "true");
properties.put("server.error.include-stacktrace", "ALWAYS");
properties.put("server.servlet.jsp.init-parameters.development", "true");
properties.put("spring.reactor.stacktrace-mode.enabled", "true");
PROPERTIES = Collections.unmodifiableMap(properties);
}


캐시를 꺼줌.


3. 코드가 변경되면 애플리케이션을 restart 시켜준다.

  => 수동으로 리스타트 하는 것보다 훨씬 빠름

       

4. 2에서 설정되는 properties는 properties 설정 우선순위의 1순위이다.

   ~/.spring-boot-devtools.properties

   => 사용자의 홈 디렉토리에 위치


5. 스프링 부트에서 스프링 웹 MVC를 사용할때 별다른 설정 없이 바로 GetMapping 같은 매핑을 사용할 수 있는 것은

    스프링 부트의 autoconfigure에서 관련 설정을 자동으로 해주고 있기 때문이다.


6. 스프링 MVC 확장 -> 필요한 부분만 구현해주거나 새로 만들어주면 됨.

  => @Configuration + WebMvcConfigurer Implements


7. 스프링 MVC 재정의 -> 기존 스프링 MVC에서 해주는 Configuration이 사라져서 필요한 것들을 직접 구현해주어야함.

  => @Configuration + @EnableWebMvc




* andExpect(org.springframework.test.web.servlet.ResultMatcher) in ResultActions cannot be applied to  에러가 자꾸 떠서 고생했음.

이건 잘 되는 코드

package me.jun.demospringmvc.user;


import org.hamcrest.Matchers;
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.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@RunWith(SpringRunner.class)
@WebMvcTest(UserController.class)
public class UserControllerTest {

@Autowired
MockMvc mockMvc;

@Test
public void hello() throws Exception {
mockMvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string("hello"));
}

@Test
public void createUser_JSON() throws Exception {
String userJson = "{\"username\":\"jun\", \"password\":\"123\"}";
mockMvc.perform(post("/users/create")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.content(userJson))
.andExpect(status().isOk())
.andExpect(jsonPath("$.username", is(equalTo("jun"))))
.andExpect(jsonPath("$.password", is(equalTo("123"))));

}
}


이건 안되는 코드

package me.jun.demospringmvc.user;

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.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;


import static org.assertj.core.internal.bytebuddy.matcher.ElementMatchers.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.jsonPath;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


@RunWith(SpringRunner.class)
@WebMvcTest(UserController.class)
public class UserControllerTest {

@Autowired
MockMvc mockMvc;

@Test
public void hello() throws Exception {
mockMvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string("hello"));
}

@Test
public void createUser_JSON() throws Exception {
String userJson = "";
mockMvc.perform(post("/users/create")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.content(userJson))
.andExpect(status().isOk())
.andExpect(jsonPath("$.username",
is(equalTo("jun"))))
.andExpect(jsonPath("$.password",
is(equalTo("123"))));

}
}


문제가 되는 부분은 이 부분이다.

import static org.assertj.core.internal.bytebuddy.matcher.ElementMatchers.is;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.jsonPath;


위 두개 의존성을 import하면 안되고

import static org.hamcrest.core.Is.is;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

을 import 해줘야한다. 특히 jsonPath는 매우 헷갈림..