🍃 Spring

    [Spring] 스프링에서 가상 스레드를 이용한 부하테스트

    a thousand hearings aren’t worth one seeing, and a thousand seeings aren’t worth one doing Spring Property 설정 spring.threads.virtual.enabled=true server.tomcat.threads.max=1 server.tomcat.threads.min-spare=1 Spring에서 JDK 21을 이용할 때 가상 스레드를 활용하기 위해서는 spring.threads.virtual.enabled=true를 활성화해줍니다. 부하테스트를 위해 Thread는 1개로 유지합니다. 코드 작성 @GetMapping("/test") public String test() throws InterruptedExceptio..

    @Autowired HttpServletRequest 사용하기(+RequestContextHolder, ThreadLocal)

    클라이언트 요청을 처리할 때 Request 객체를 다루는 경우가 있다. 일반적으로는 Controller에서 HttpServletRequest를 사용하여 객체를 꺼내오는 방법이 있다. 컨트롤러에서 가져오기 @Controller public class MyController { @RequestMapping("/myEndpoint") public String handleRequest(HttpServletRequest request) { // Request 객체 활용 // ... return "response"; } } RequestContextHolder에서 가져오기 만약에 Controller 영역이 아닌 곳에서는 일반적으로 아래와 같이 사용할 수 있었다. HttpServletRequest request = ..

    JUnit 문법

    Junit 4 Junit 기본 제공 org.junit.Assert.*; Junit에는 위의 패키지에서 기본 제공되던 assertThat이 있었다. 하지만 검증이 좀 딸려서 AssertJ 쓰는게 좋았음. org.assertj.core.api.Assertions AssertJ는 위의 패키지를 말함 Junit 5 Junit 기본 제공 org.junit.jupiter.api.Assertions Junit에서 기본제공하는 jupiter 패키지의 assertEquals와 assertThrow가 있다. assertEquals("zzzz", mvcDto.getName()); 간단한게 검사할 때 쓰인다. 결론은 AssertJ 추천 Hamcrest org.hamcrest.Matchers assertThat(mvcDto.g..

    @WebMvcTest에 대한 올바른 사용법 및 시행착오

    이번에 @WebMvcTest를 사용하면서 겪은 시행착오를 공유해 볼 예정이다. @WebMvcTest란? @WebMvcTest는 Spring Controller 영역에 대한 HTTP 요청만을 단위 테스트할 때 필요한 Bean들만 올려주는 기능을 가진 어노테이션이다. 실제 테스트는 MockMvc를 통해서 수행한다. MockMvc는 HTTP 서버 요청을 직접하는 것이 아니라 컨트롤러에 대한 메소드만을 딱 잘라서 테스트하는 기능이라고 보면 된다. 속도 측면에서 @SpringBootTest를 사용하면서 MockMvc를 사용하면 모든 Spring Bean을 전부 로드해서 느리다는 단점이 있지만 @WebMvcTest는 웹 계층에서 필요한 빈들만 로드하는 기능을 가지고 있어서 불필요한 Bean들은 로드되지 않아 테스트..