ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 트러블 슈팅 -shop 프로젝트
    카테고리 없음 2025. 5. 23. 23:53

     

     

    Testcode에서 발생한 직렬화/역직렬화 문제

     

    증상

     commentService.getCommentCount(findByProduct.getId())//계속 null로 반환

     

    ✅ 왜 MockitoBean이 위험했냐?

    • @MockkitoBean = 아무 동작도 없는 가짜 객체
    • 메서드 호출하면 null 리턴함
    • 네가 new CommentServiceImpl(..., commentCacheService) 했을 때,
      그 commentCacheService는 아무것도 하지 않는 Mock이었던 것

     

    해결방법

     @Autowired
        private CommentService commentService;
    
        @Autowired
        private CommentCacheService commentCacheService;

     

     

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
    
        // (선택) 직렬화 설정: String -> String
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new StringRedisSerializer());
    
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());    <-추가
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }

     

    최종 수정 코드

     

    @Transactional
    	@Test
    	public void 댓글_수_확인(){
    		Order order = serOrder();
    		order.changeOrderStatus(OrderStatus.PAID);
    		if(!order.getUser().getId().equals(2L)){
    			System.out.printf(order.getUser().getId().toString());
    			throw  new CustomRuntimeException(ExceptionCode.UNAUTHORIZED_REVIEW_ACCESS);
    		}
    		if(order.getOrderStatus().equals(OrderStatus.PENDING)){
    			throw  new CustomRuntimeException(ExceptionCode.PAYMENT_REQUIRED);
    		}
    		boolean notFound = order.getProductOrderList()
    			.stream()
    			.noneMatch(po -> po.getProductId().equals(1L));
    		if(notFound){
    			throw  new CustomRuntimeException(ExceptionCode.PRODUCTORDER_NOT_FOUND);
    		}
    		Product findByProduct = productRepository.findById(1L).orElseThrow(()-> new CustomRuntimeException(ExceptionCode.PRODUCT_CANT_FIND));
    
    
    		Random random = new Random();
    		List<CommentRequestDto> dto = List.of(
    			new CommentRequestDto("빠른 배송 감사합니다.", "빨리 받았어요", random.nextInt(0,5)),
    			new CommentRequestDto("빠른 배송 감사합니다.", "빨리 받았어요", random.nextInt(0,5)),
    			new CommentRequestDto("빠른 배송 감사합니다.", "빨리 받았어요", random.nextInt(0,5)),
    			new CommentRequestDto("빠른 배송 감사합니다.", "빨리 받았어요", random.nextInt(0,5)),
    			new CommentRequestDto("빠른 배송 감사합니다.", "빨리 받았어요", random.nextInt(0,5))
    		);
    
    		List<Comment>comments = new ArrayList<>();
    		for(int i = 0; i < dto.size(); i++){
    
    			comments.add( new Comment(dto.get(0), order.getUser(),findByProduct));
    			redisTemplate.opsForHash().increment("product:review:count",findByProduct.getId().toString(), 1);
    
    		}
    		commentRepository.saveAll(comments);
    		commentRepository.flush();
    
    		CommentGetCountResponseDto commentGetCountResponseDto = new CommentGetCountResponseDto();
    
    		commentGetCountResponseDto = commentService.getCommentCount(findByProduct.getId());
    
    		System.out.println("commentService class: " + commentService);
    		System.out.println("getCommentCount result: " + commentService.getCommentCount(findByProduct.getId()).getCount());
    	}

     

Designed by Tistory.