2016-11-25 37 views
4

私はすべての例外が正しいことを確認しようとしています。値はCompletableFuturesにラップされているため、スローされる例外はExecutionExceptionです。原因は例外で、通常チェックします。クイック例:ExpectedException原因の原因?

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

のでfoo()bar()によってスローされた例外を変換し、そのすべてがCompletableFuturesAsyncHandlers内で行われている(私は全体のコードをコピーしません、それはあくまでも参考のためです)

私のユニットでfoo()を呼び出すときに、私はbar()を作ってるんだテストが例外をスローし、それが正しく翻訳されますことを確認したい:

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-possible-to-wrapped-exception/20759785#20759785 –

+0

@RC。ほぼ、しかし、私は1つのレベルを深くし、次の原因を取得する必要があります:) – Amir

+1

あなたは適応することができる必要があります:https://stackoverflow.com/a/6528640/180100 –

答えて

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); 
     } 
    } 

} 
関連する問題