프레임워크
-
useEffect가 2번 실행된다면 ( ft. StrictMode )프레임워크/Nextjs 2024. 4. 18. 18:38
여유있게 시간을 가지고 공부하면서 가장 즐거운 부분은 문제가 생겼을 때, 천천히 공식문서를 음미할 수 있다는 부분이 아닌가 합니다 ㅎㅎ 이번 글은 게시글을 조회하면서 조회수가 2개씩 오르는 것을 디버깅하다가 발견하여 적어봅니다. 이 글은 다음과 같은 순서로 진행됩니다. 1. 문제를 확인 2. 원인 파악 및 해결 3. StrictMode는 무엇? 1. 문제를 확인 분명히 클릭이 한번만 발생하는데 useEffect가 두번씩 동작합니다. 디펜던시가 뭐가 걸려있나 확인해봤는데, 빈 배열입니다. 뭔일인가 싶어서, 바로 GPT에게 물어보니 Strict Mode라는 키워드를 던져줍니다. 2. 원인 파악 및 해결 리액트로 작업할 때는 보통 벗겨내고 작업을 해와서 크게 인지하지 않고 있었는데, Next에서도 Strict..
-
내부에서 실행되는 메소드에 인자가 잘 전달되는지 테스트프레임워크/JUnit 2024. 4. 17. 02:09
이번 글은 스프링 부트 프로젝트에서 컨트롤러를 테스트할 때 사용해보니 괜찮은 것 같아서 정리를 해봅니다. 스프링을 넘어서 내부에서 호출되는 메소드에 인자를 잘 전달하는가 를 테스트하는 방법입니다. ( 소스코드 ) 1. 테스트 세팅 컨트롤러 레이어에서 서비스 레이어에 적절한 인자를 전달하는지 테스트하는 방식으로 진행하겠습니다. 다이어그램의 파란색 화살표에 포커스된 테스트를 작성할 것입니다. 컨트롤러 클래스 코드입니다. 아주 간단한 3개의 엔드포인트를 가지고 있습니다. ( 게시글 조회 / 게시글 생성 / 게시글 목록 조회 ) 서비스 코드는 인터페이만 노출할 뿐 내용은 없습니다. 서비스에서 사용되는 간단한 dto 입니다. 그리고 컨트롤러를 테스트하기 위한 테스트 코드도 작성해줍니다. 2. 테스트 작성 개별 게..
-
argThat을 사용해서 primitive 타입을 확인하려고 할 때 주의사항프레임워크/JUnit 2024. 4. 17. 00:24
1. argThat을 사용할 때 NullPointExeception을 만날 수 있습니다. "org.mockito.ArgumentMatchers.argThat(org.mockito.ArgumentMatcher)" is null 문제의 코드입니다. 서비스 메소드에서 Long이 아닌 long을 받고 있습니다. argThat을 사용해서 id를 비교하려고 하니 예외가 발생합니다. argThat은 기본적으로 Wrapper type을 기대하는데 primitive 타입을 사용하니 NullPointException을 던집니다. 이럴 때는 eq() 사용해서 더욱 간단히 표현하면 좋겠습니다.
-
멀티 쓰레드 환경에서 게시글 조회수 증가에 대한 검증프레임워크/JUnit 2024. 4. 15. 21:40
조회수 증가와 같이 동시에 여러 쓰레드에서 접근하는 Race condition이 발생할 수 있는 상태가 적절하게 대응이 되어있는지 확인하기 위한 테스트를 작성해보겠습니다. 모든 예제는 깃헙 소스코드의 concurrency 프로젝트에서 확인 가능합니다. 이 글은 다음과 같은 구성으로 작성되었습니다. 1. 프로젝트 세팅 2. 테스트 코드 작성 3. 멀티 쓰레드 환경의 테스트 코드는 어떻게 진행되는 것일가? 4. 동시성 이슈로부터 안전한 코드 만들기 5. 테스트 util 생성하기 1. 프로젝트 세팅 가장 기본적인 형태로 프로젝트가 구성되어 있습니다. 서비스 레이어에는 생성과 조회를 담당하는 2가지 메소드가 있습니다. Repository에서는 JPA 를 사용하고 있고, 비관적락을 사용하는 메소드를 별도로 구현하..
-
InvalidUseOfMatchersException프레임워크/JUnit 2024. 3. 27. 09:05
테스트 작성하면서 발생한 에러 중 종종 만나는 예외를 정리해봅니다. mockMvc를 사용하고 있고 아래와 같은 헬퍼함수를 하나 작성해서 공통으로 사용 중에 있습니다. private ResultActions patchAndVerify(String id, String body) throws Exception { return mockMvc.perform(patch(baseUrl + "/" + id) .contentType(MediaType.APPLICATION_JSON) .content(body) ); } 수정하기 api의 컨트롤러에 대한 테스트를 작성하고 있습니다. when() 절에서 any(JobPostingEditReq.class)를 사용하기 때문에 입력에 따른 결과값의 형태는 serviceTest에거 ..
-
N + 1 정복하기 <5. Entity Graph>프레임워크/Spring Boot 2024. 2. 21. 13:27
시리즈1. https://iwsaitw.tistory.com/entry/N-1-정복하기 시리즈2. https://iwsaitw.tistory.com/entry/N-1-정복하기-2-FetchTypeEAGER 시리즈3. https://iwsaitw.tistory.com/entry/N-1-정복하기-3-Fetch-Join 시리즈4. https://iwsaitw.tistory.com/entry/N-1-정복하기-4-BatchSize (소스코드: https://github.com/blog-example/-JPA-N_Plus_1) ORM을 사용하면 만나는 흔한 이슈 중 하나인 N + 1 ! 명확하게 설명할 수 있을 정도로 머리속에 집어넣어보겠습니다. 이번 글에서는 Entity Graph를 활용해서 N + 1을 해결하는..
-
N + 1 정복하기 <4. @BatchSize>프레임워크/Spring Boot 2024. 2. 21. 03:40
시리즈1. https://iwsaitw.tistory.com/entry/N-1-정복하기 시리즈2. https://iwsaitw.tistory.com/entry/N-1-정복하기-2-FetchTypeEAGER 시리즈3. https://iwsaitw.tistory.com/entry/N-1-정복하기-3-Fetch-Join (소스코드: https://github.com/blog-example/-JPA-N_Plus_1) ORM을 사용하면 만나는 흔한 이슈 중 하나인 N + 1 ! 명확하게 설명할 수 있을 정도로 머리속에 집어넣어보겠습니다. 이번 글에서는 @BatchSize를 활용해서 N + 1을 해결하는 과정을 살펴보겠습니다. 이글은 3번 글에서 이어집니다. 3번 글을 보고 오시면 이해에 도움이 될 것 같습니다! ..
-
N + 1 정복하기 <3. Fetch Join>프레임워크/Spring Boot 2024. 2. 21. 02:57
시리즈1. https://iwsaitw.tistory.com/entry/N-1-정복하기 시리즈2. https://iwsaitw.tistory.com/entry/N-1-정복하기-2-FetchTypeEAGER (소스코드: https://github.com/blog-example/-JPA-N_Plus_1) ORM을 사용하면 만나는 흔한 이슈 중 하나인 N + 1 ! 명확하게 설명할 수 있을 정도로 머리속에 집어넣어보겠습니다. 이번 글에서는 fetch join을 활용해서 N + 1을 해결하는 과정을 살펴보겠습니다. 1. JPQL? Fetch Join? Fetch Join 을 이야기 하기에 앞서서 JPQL을 먼저 이야기할 필요가 있습니다. gpt가 뭐라고 정의하는지 한 번 보겠습니다. JPQL은 개발자가 원하는 ..