본문 바로가기

항해99

23.10.26 항해 99 16기 실전 프로젝트 20일차

오늘 공부한 것

* HTTPS 배포

* 댓글 Service 테스트 코드 작성

 

오늘은 HTTPS 로 배포를 하였다. 원래 아직 할 생각은 없었는데

프론트엔드에서 Vecel 로 배포를 하면서 HTTPS 로 변경되어서 우리 백엔드도 부랴부랴 배포했다

 

아래의 기업에서 도메인을 구입했다 https://gallae-trip.com

그리고 세팅은 블로그를 참고했다 

역시 돈준 보람이 있는지 HTTPS 로 변환이 엄청 손쉽게 가능했다.

https://www.cloudflare.com/ko-kr/

 

Cloudflare 홈페이지

Cloudflare의 CDN, DNS, DDoS 보호 및 네트워크 보안으로 안전한 인터넷 환경을 구축하세요.

www.cloudflare.com

https://m.blog.naver.com/injadark/222784231593

 

도메인 구매 및 클라우드플레어 도메인 추가 SSL 설정까지!

STUDY 도메인 구매, 서버 세팅까지 일사천리! 이 플랫폼 좋네 좋아~ 예전에 스터디 및 개인 용도를 위...

blog.naver.com

 

이후에는 다시 테스트 코드 작성에 열중했다

오늘은 Comments 의 Service 부분을 작성했다

 

1. CommentsServiceTest

더보기
public class CommentsServiceTest {

    // @Mock 이 붙은 목 객체를 주입시킬 수 있다
    @InjectMocks
    private CommentsService commentsService;

    // 로직이 삭제된 빈껍데기, 실제로 메서드는 가지고 있지만 내부구현이 없음
    @Mock
    private CommentsRepository commentsRepository;

    @Mock
    private PostsRepository postsRepository;

    @BeforeEach
    public void setUp() {
        MockitoAnnotations.openMocks(this);
    }

    public Users MockUsers1(){
        return new Users("rkawk@email.com", "감자", "test123!", UserRoleEnum.USER, "image/profileImg.png");
    }
    public Users MockUsers2(){
        return new Users("qksksk@email.com", "바나나", "test123!", UserRoleEnum.USER, "image/profileImg.png");
    }
    public Users MockAdmin(){
        return new Users("rhksflwk@email.com", "관리자", "test123!", UserRoleEnum.ADMIN, "image/profileImg.png");
    }

    @Test
    @DisplayName("[정상 작동] 댓글 생성")
    public void commentsCreateTest() {

        // Given
        Long postId = 1L;
        CommentsRequestDto requestDto = new CommentsRequestDto("테스트 댓글");
        Users users = MockUsers1(); // 사용자 정보 초기화

        Posts post = new Posts(); // 게시글 정보 초기화
        when(postsRepository.findById(postId)).thenReturn(java.util.Optional.of(post));
        when(commentsRepository.save(any(Comments.class))).thenReturn(new Comments());

        // When
        MessageResponseDto response = commentsService.commentsCreate(postId, requestDto, users);

        // Then
        System.out.println("댓글 생성");
        assertEquals("댓글을 작성하였습니다", response.getMsg());
        assertEquals(HttpServletResponse.SC_OK, response.getStatusCode());
    }

    @Test
    @DisplayName("[비정상 작동]댓글 생성")
    public void commentsCreatePostNotFoundTest() {

        // Given
        Long postId = 1L;
        CommentsRequestDto requestDto = new CommentsRequestDto("테스트 댓글");
        Users users = MockUsers1(); // 사용자 정보 초기화

        when(postsRepository.findById(postId)).thenReturn(java.util.Optional.empty());

        // When
        CustomException exception = assertThrows(CustomException.class, () -> {
            commentsService.commentsCreate(postId, requestDto, users);
        });

        // Than
        System.out.println("ErrorCode.POST_NOT_EXIST: " + "존재하지 않는 게시글입니다");
        System.out.println("exception.getErrorCode(): " + exception.getErrorCode());
        assertEquals(ErrorCode.POST_NOT_EXIST, exception.getErrorCode());
    }

    @Test
    @DisplayName("[정상 작동] 댓글 수정")
    public void commentsUpdateTest() {

        // Given
        Long commentId = 1L;
        CommentsRequestDto requestDto = new CommentsRequestDto("댓글 수정전");
        Users users = MockUsers1(); // 사용자 정보 초기화

        Comments comments = new Comments(); // 게시글 정보 초기화
        when(commentsRepository.findById(commentId)).thenReturn(java.util.Optional.of(comments));

        // When
        MessageResponseDto response = commentsService.commentsUpdate(commentId, requestDto, users);

        // Than
        System.out.println("댓글 수정");
        assertEquals("댓글을 수정하였습니다", response.getMsg());
        assertEquals(HttpServletResponse.SC_OK, response.getStatusCode());
    }
}

   

정상적으로 댓글을 생성했을 때와 게시글이 없어서 댓글을 생성하지 못할 때 두개를 만들었는데

무난하게 성공했다. 하지만 댓글 수정부분에서 아래와 같은 에러 코드가 생성되었다

 

2. 댓글 수정부분 에러코드

    상태를 보아하니 CommentService 에서 아래의 코드가 있는데 이부분을 확인하지 못해서

    CustomException이 나오는거 같다

    해서 어떤 방법이 있는가 고민중인데 아직 해결은 하지 못했다.. 내일 계속 이어서 해봐야겠다

(users.getEmail().equals(comments.getEmail()))
더보기


java.lang.NullPointerException: Cannot invoke "String.equals(Object)" because the return value of "cohttp://m.sparta.team2project.users.Users.getEmail()" is null
at com.sparta.team2project.comments.service.CommentsService.commentsUpdate(CommentsService.java:98)
at com.sparta.team2project.comments.CommentsServiceTest.commentsUpdateTest(CommentsServiceTest.java:121)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.platforhttp://m.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:727)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:156)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:147)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:86)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(InterceptingExecutableInvoker.java:103)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.lambda$invoke$0(InterceptingExecutableInvoker.java:93)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:92)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:86)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:217)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:213)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:138)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:68)
at org.junit.platforhttp://m.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platforhttp://m.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platforhttp://m.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platforhttp://m.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platforhttp://m.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platforhttp://m.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platforhttp://m.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platforhttp://m.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platforhttp://m.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platforhttp://m.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platforhttp://m.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platforhttp://m.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at org.junit.platforhttp://m.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
at org.junit.platforhttp://m.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
at org.junit.platforhttp://m.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
at org.junit.platforhttp://m.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
at org.junit.platforhttp://m.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
at org.junit.platforhttp://m.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
at org.junit.platforhttp://m.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
at org.junit.platforhttp://m.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
at org.junit.platforhttp://m.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassProcessor.java:110)
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.access$000(JUnitPlatformTestClassProcessor.java:90)
at org.gradle.api.internal.tasks.testing.junitplatforhttp://m.JUnitPlatformTestClassProcessor.stop(JUnitPlatformTestClassProcessor.java:85)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:62)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
at jdk.proxy1/jdk.proxy1.$Proxy2.stop(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker$3.run(TestWorker.java:193)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.executeAndMaintainThreadName(TestWorker.java:129)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:100)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:60)
at org.gradle.process.internal.worker.child.ActionExecutionWorker.execute(ActionExecutionWorker.java:56)
at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:113)
at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:65)
at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)