2016-11-25 80 views
4

我試圖驗證我所有的例外都是正確的。因爲值包含在CompletableFutures中,所拋出的異常是ExecutionException,原因是我通常會檢查的異常。簡單的例子:ExpectedException造成原因?

void foo() throws A { 
    try { 
    bar(); 
    } catch B b { 
    throw new A(b); 
    } 
} 

所以foo()轉換異常由bar()扔了,所有這一切都內CompletableFuturesAsyncHandlers做(我不會複製整個代碼,它只是供參考)

在我單位測試中,我正在做bar()拋出一個異常,並要檢查它的翻譯正確調用foo()時:

Throwable b = B("bleh"); 
when(mock.bar()).thenThrow(b); 
ExpectedException thrown = ExpectedException.none(); 
thrown.expect(ExecutionException.class); 
thrown.expectCause(Matchers.allOf(
       instanceOf(A.class), 
       having(on(A.class).getMessage(), 
         CoreMatchers.is("some message here")), 
     )); 

到目前爲止好,b UT我也想驗證例外的因由A是例外Bhaving(on(A.class).getCause(), CoreMatchers.is(b))原因CodeGenerationException --> StackOverflowError

TL; DR:我如何獲得預期的異常引起的原因是什麼?

+0

也許這個問題是你想要的(我不知道):https://stackoverflow.com/questions/ 871216/junit - 可能期望包裝異常/ 20759785#20759785 – 2016-11-25 18:29:36

+0

@RC。幾乎,但我必須更深入一級,並得到下一個原因:) – Amir

+1

你應該能夠適應:https://stackoverflow.com/a/6528640/180100 – 2016-11-26 09:24:04

回答

0

也許你應該嘗試用簡單的hasProperty匹配器,以隔離問題:

thrown.expectCause(allOf(
        instanceOf(A.class), 
        hasProperty("message", is("some message here")), 
     )); 
0

這是我用它來測試只因果類鏈的例子。 參考文獻:


import static org.hamcrest.Matchers.contains; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 

import org.hamcrest.Description; 
import org.hamcrest.Matcher; 
import org.hamcrest.TypeSafeMatcher; 
import org.junit.Rule; 
import org.junit.Test; 
import org.junit.rules.ExpectedException; 

public class CausalClassChainTest { 

    @Rule 
    public ExpectedException expectedException = ExpectedException.none(); 

    @Test 
    public void test() throws Exception { 
     expectedException.expect(IOException.class); 
     expectedException.expectCause(new CausalClassChainMather(Exception.class, RuntimeException.class)); 

     throw new IOException(new Exception(new RuntimeException())); 
    } 

    private static class CausalClassChainMather extends TypeSafeMatcher<Throwable> { 

     private final Class<? extends Throwable>[] expectedClasses; 
     private List<Class<? extends Throwable>> causualClasses; 
     private Matcher<Iterable<? extends Class<? extends Throwable>>> matcher; 

     public CausalClassChainMather(Class<? extends Throwable>... classes) { 
      this.expectedClasses = classes; 
     } 

     @Override 
     public void describeTo(Description description) { 
      // copy of MatcherAssert.assertThat() 
      description.appendText("") 
        .appendText("\nExpected: ") 
        .appendDescriptionOf(matcher) 
        .appendText("\n  but: "); 
      matcher.describeMismatch(causualClasses, description); 
     } 

     @Override 
     protected boolean matchesSafely(Throwable item) { 

      List<Class<? extends Throwable>> causes = new ArrayList<Class<? extends Throwable>>(); 
      while (item != null) { 
       causes.add(item.getClass()); 
       item = item.getCause(); 
      } 
      causualClasses = Collections.unmodifiableList(causes); 

      // ordered test 
      matcher = contains(expectedClasses); 
      return matcher.matches(causualClasses); 
     } 
    } 

} 
相關問題