Antop
Brain to Blog
Antop
전체 방문자
931,612
오늘
4
어제
12
  • 분류 전체보기 (167)
    • Ubuntu Developer (0)
    • Mini Project (12)
    • Request Sample (1)
    • Study (5)
      • Vue.js (2)
      • Hadoop (3)
    • Java+ (23)
      • Trouble Shooting (5)
      • Example (8)
    • Framework (22)
      • Struts (3)
      • Spring (3)
      • Spring OSGi (2)
      • Spring Security (3)
      • Hibernate (9)
      • Mybatis (1)
    • Android (1)
    • Oracle Solution (40)
      • Tutorial for Oracle Coheren.. (1)
      • PO Processing (17)
      • BPM 11g Foundation Training (11)
      • Oracle Service Bus 11g Hand.. (5)
      • BPEL (3)
      • Trouble Shooting (2)
    • JavaScript (5)
      • jQuery (2)
      • Ext JS (1)
    • Tools (12)
      • Eclipse (11)
      • Maven (1)
    • Database (6)
      • Oracle (3)
      • MySQL (2)
    • Server (30)
      • Ubuntu (16)
      • WebLogic (7)
      • VMware ESXi (6)
    • Etc (9)
      • Scrap (4)
      • Link (1)
      • Game (2)

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

  • Install Ambari and Deploy HDP⋯
    2016.06.05
    Install Ambari and Deploy HDP⋯
  • web.xml 서블릿 버전별 DTD
    2013.03.03
  • 메이븐 웹 프로젝트 생성 후 해⋯
    2013.09.09
    메이븐 웹 프로젝트 생성 후 해⋯
  • 2014년 도로명 주소 사용에 따⋯
    2013.12.22
    2014년 도로명 주소 사용에 따⋯
  • Mybatis Interceptor + RowBoun⋯
    2017.12.12

태그

  • Java
  • osb
  • LAB
  • ORM
  • hibernate
  • JSON
  • JavaScript
  • BPM
  • SOA
  • esxi
  • VMware
  • Spring
  • Eclipse
  • Weblogic
  • JDBC
  • oracle
  • xml
  • API
  • JSP
  • Struts
  • mybatis
  • ubuntu
  • jQuery
  • 하이버네이트
  • EJB
  • BPEL
  • maven
  • extjs
  • MySQL
  • Ajax

최근 댓글

  • 소스를 오픈 해주셔서 감사합니⋯
    헤르메스의날개
  • 드라이버에 의존하는것보다 확⋯
    망고스틴
  • 비슷한걸 개발하면서 어떻게 해⋯
    홍마초
  • 잘 보고 갑니다...
    딸랑딸랑
  • 잘 보고 갑니다...
    이웃사촌

최근 글

  • Mockito.thenThrow() 주의사항!
    2021.09.25
  • Spring + @Lazy
    2019.08.05
    Spring + @Lazy
  • [Kotlin + Spring] Maven confi⋯
    2019.05.26
  • Installation Vue.js (Eclipse)
    2018.05.24
    Installation Vue.js (Eclipse)
  • Installation Vue.js (WebStorm⋯
    2018.05.21
    Installation Vue.js (WebStorm⋯

티스토리

hELLO · Designed By 정상우.
Antop

Brain to Blog

Framework/Spring

Mockito.thenThrow() 주의사항!

2021. 9. 25. 03:23
반응형

예외를 던지도록하는 테스트를 할 때 Exception 안에 메시지를 활용할 경우 Exception.class 를 사용하지말 고 new Exception()을 사용하자.

 

일단 예외 클래스를 보자

/**
 * 이미 종료된 대회일 때 예외
 *
 * @author jammini
 */
public class AlreadyContestEndException extends BadRequestException {
    public AlreadyContestEndException() {
        super("이미 종료된 대회입니다.");
    }
}

 

서비스를 모킹해서 예외를 던지도록 할 것이다.

@WebMvcTest(ContestInfoApi.class)
class ContestInfoApiTest extends WebMvcBase {
    @MockBean
    private ContestService contestService;
    
    @Test
    void alreadyContestEnd() throws Exception {
        // when
        // AlreadyContestEndException.class
        when(contestService.modify(anyLong(), any())).thenThrow(AlreadyContestEndException.class);
        
        // ...
    }
    
 }

 

@ControllerAdvice 를 이용해 예외 클래스 안의 message 속성을 사용할 경우!

/**
 * 웹 예외 핸들러<br>
 * 시스템상에 발생하는 예외를 잡아서 공통으로 처리한다.
 *
 * @author antop
 */
@Slf4j
@RestControllerAdvice
public class ErrorAdvisor {

    /**
     * 400 Bad Request 예외 처리
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(BadRequestException.class)
    ErrorMessage badRequest(Exception e) {
        log.debug("message = {}", e.getMessage());
        return ErrorMessage.badRequest(e.getMessage());
    }
    
  }

 

message는 null이 출력되게 된다. thenThrow() 인자로 new Exception() 을 사용하자.

when(contestService.modify(anyLong(), any())).thenThrow(new AlreadyContestEndException());

 

 

 

 

 

반응형
  • 카카오스토리
  • 트위터
  • 페이스북

'Framework > Spring' 카테고리의 다른 글

Mockito.thenThrow() 주의사항!  (0) 2021.09.25
Spring + @Lazy  (0) 2019.08.05
Twelve Best Practices For Spring XML Configurations  (0) 2010.06.23
    'Framework/Spring' 카테고리의 다른 글
    • Spring + @Lazy
    • Twelve Best Practices For Spring XML Configurations
    Antop
    Antop
    뇌에서 블로그로... antop@naver.com
    댓글쓰기
    이전 글
    Spring + @Lazy
    • 이전
    • 1
    • 2
    • 3
    • 4
    • 5
    • ···
    • 167
    • 다음