2016-08-14 5 views
2

AssertJ를 사용할 때 excetion을 던져서 원인의 메시지가 일부 문자열과 같은지 확인하는 방법이 있습니까?AssertJ가 원인 메시지에 어설트합니다.

나는 현재 같은 일을 해요 :

assertThatThrownBy(() -> SUT.method()) 
      .isExactlyInstanceOf(IllegalStateException.class) 
      .hasRootCauseExactlyInstanceOf(Exception.class); 

및 근본 원인에 메시지를 확인하는 주장을 추가하고 싶습니다. 당신이 지금 할 수있는 최선은 hasStackTraceContaining을 사용

답변

4

정확하지 않게, 예를

Throwable runtime = new RuntimeException("no way", 
             new Exception("you shall not pass")); 

assertThat(runtime).hasCauseInstanceOf(Exception.class) 
        .hasStackTraceContaining("no way") 
        .hasStackTraceContaining("you shall not pass");