1 package io.atlassian.fugue.hamcrest;
2
3 import org.junit.Test;
4
5 import static io.atlassian.fugue.Try.failure;
6 import static io.atlassian.fugue.Try.successful;
7 import static io.atlassian.fugue.hamcrest.TryMatchers.isFailure;
8 import static io.atlassian.fugue.hamcrest.TryMatchers.isSuccessful;
9 import static org.hamcrest.Matchers.*;
10 import static org.junit.Assert.assertThat;
11
12 public class TryMatchersTest {
13
14 @Test public void shouldMatchAnyFailure() {
15 assertThat(failure(new RuntimeException("any")), TryMatchers.isFailure());
16 }
17
18 @Test public void shouldNotMatchASuccessWhenExpectingAnyFailure() {
19 assertThat(successful("a success"), not(isFailure()));
20 }
21
22 @Test public void shouldMatchASpecificFailure() {
23 assertThat(failure(new RuntimeException("any")), TryMatchers.isFailure(any(Exception.class)));
24 }
25
26 @Test public void shouldNotMatchASuccessWhenExpectingASpecificFailure() {
27 assertThat(successful("a success"), not(isFailure(any(Exception.class))));
28 }
29
30 @Test public void shouldNotMatchADifferentFailure() {
31 assertThat(failure(new IllegalArgumentException("wrong exception")),
32 not(TryMatchers.isFailure(is(new IllegalStateException("expected exception")))));
33 }
34
35 @Test public void shouldMatchASuccessfulTry() {
36 assertThat(successful("anyResult"), isSuccessful(isA(String.class)));
37 }
38
39 @Test public void shouldMatchASuccessfulTryWithASupertypeMatcher() {
40 assertThat(successful("anyResult"), isSuccessful(isA(Object.class)));
41 }
42
43 @Test public void shouldNotMatchAFailureWhenExpectingASuccess() {
44 assertThat(failure(new RuntimeException("any")), not(isSuccessful(any(Object.class))));
45 }
46
47 }