프레임워크/JUnit
-
내부에서 실행되는 메소드에 인자가 잘 전달되는지 테스트프레임워크/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에거 ..