OAauth 2.0 로그인 기능 테스트를 하며 겪은 고민 및 해결 방법 공유 #66
jaejae-yoo
started this conversation in
공유
Replies: 1 comment
-
좋은 자료 공유 감사합니다 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
로그인 기능 PR (#64)
로그인 코드 리뷰에 도움이 되면 좋겠어서 관련 내용을 작성해 보았습니다.
저와 짱구(@sc0116)가 이번에 구현한 로그인 기능은 지속적으로 외부 API와 통신해야 했는데요.
해당 과정에서 테스트에 대한 많은 고민이 있었습니다.
1. 외부 API 동작 과정을 저희가 직접 제어할 수 없으니, 반환 값으로 어떤 값이 나올지 예상할 수 없었습니다.
2. 동작 과정 자체가 여러 단계로 이루어져 있어서 여러 단계의 각 과정을 slice해서 테스트하기 매우 어려웠습니다.
서비스 코드를 보며 설명해 드리겠습니다.
해당 과정을 정리해보면,
다음으로 위에서 사용되고 있는 OAuthClient 클래스의 getAccessToken() 메서드를 보겠습니다.
여기서 RestTemplate라는 클래스를 볼 수 있는데요.
RestTemplate란?
RestTemplate는 HTTP 통신에 유용하게 쓸 수 있는 템플릿을 제공합니다.
exchange 메서드는 지정된 요청을 실행하고 ResponseEntity로 응답을 반환합니다.
위에서 보실 수 있듯이 이는 한 flow에서 처리되고 있습니다.
따라서 각 과정을 slice해서 테스트할 수 없고, 외부 API를 사용하니 서비스의 테스트를 진행하기 어려웠습니다.
해당 문제를 해결하기 위해 MocRestServiceServer라는 모의 서버를 사용하였습니다.
Spring test 모듈에는 MocRestServiceServer라는 모의 서버가 포함되어 있는데요.
해당 방식을 사용하면 RestTemplate 인스턴스를 통해 특정 요청이 전달될 때 특정 개체를 반환하도록 서버를 구성합니다.
따라서 저희는 fakeController를 생성해서 MocRestServiceServer를 통해 반환값을 제공하도록 작성해보았습니다.
해당 가짜 컨트롤러를 통해 외부 API 통합 테스트를 진행할 수 있었네요.
코드 리뷰에 조금이나마 이해가 되면 좋을 것 같네요!
References
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/web/client/MockRestServiceServer.html
https://www.baeldung.com/spring-mock-rest-template
Beta Was this translation helpful? Give feedback.
All reactions