Home | History | Annotate | Download | only in throwingproviders
      1 package com.google.inject.testing.throwingproviders;
      2 
      3 import static com.google.common.truth.Truth.assertThat;
      4 import static com.google.inject.testing.throwingproviders.CheckedProviderSubject.assertThat;
      5 
      6 import com.google.common.truth.ExpectFailure;
      7 import com.google.common.truth.SimpleSubjectBuilder;
      8 import com.google.inject.throwingproviders.CheckedProvider;
      9 import com.google.inject.throwingproviders.CheckedProviders;
     10 import org.junit.Rule;
     11 import org.junit.Test;
     12 import org.junit.runner.RunWith;
     13 import org.junit.runners.JUnit4;
     14 
     15 /**
     16  * Unit tests for {@link CheckedProviderSubject}.
     17  *
     18  * @author eatnumber1 (at) google.com (Russ Harmon)
     19  */
     20 @RunWith(JUnit4.class)
     21 public class CheckedProviderSubjectTest {
     22   public @Rule ExpectFailure expect = new ExpectFailure();
     23 
     24   private interface StringCheckedProvider extends CheckedProvider<String> {}
     25 
     26   @Test
     27   public void providedValue_gotExpected_expectSuccess() {
     28     String expected = "keep Summer safe";
     29     CheckedProvider<String> provider = CheckedProviders.of(StringCheckedProvider.class, expected);
     30 
     31     assertThat(provider).providedValue().isEqualTo(expected);
     32   }
     33 
     34   @Test
     35   public void providedValue_gotUnexpected_expectFailure() {
     36     String expected = "keep Summer safe";
     37     String unexpected = "Summer is unsafe";
     38     CheckedProvider<String> provider = CheckedProviders.of(StringCheckedProvider.class, unexpected);
     39     String message =
     40         String.format(
     41             "value provided by <%s>: Not true that <%s> is equal to <%s>",
     42             getReturningProviderName(unexpected), unexpected, expected);
     43 
     44     expectWhenTesting().that(provider).providedValue().isEqualTo(expected);
     45     assertThat(expect.getFailure()).hasMessageThat().isEqualTo(message);
     46   }
     47 
     48   private static final class SummerException extends RuntimeException {}
     49 
     50   @Test
     51   public void providedValue_throws_expectFailure() {
     52     CheckedProvider<String> provider =
     53         CheckedProviders.throwing(StringCheckedProvider.class, SummerException.class);
     54     String message =
     55         String.format(
     56             "checked provider <%s> threw an exception",
     57             getThrowingProviderName(SummerException.class.getName()));
     58 
     59     expectWhenTesting().that(provider).providedValue();
     60     AssertionError expected = expect.getFailure();
     61     assertThat(expected).hasCauseThat().isInstanceOf(SummerException.class);
     62     assertThat(expected).hasMessageThat().isEqualTo(message);
     63   }
     64 
     65   @Test
     66   public void thrownException_threwExpected_expectSuccess() {
     67     CheckedProvider<?> provider =
     68         CheckedProviders.throwing(StringCheckedProvider.class, SummerException.class);
     69 
     70     assertThat(provider).thrownException().isInstanceOf(SummerException.class);
     71   }
     72 
     73   @Test
     74   public void thrownException_threwUnexpected_expectFailure() {
     75     Class<? extends Throwable> expected = SummerException.class;
     76     Class<? extends Throwable> unexpected = UnsupportedOperationException.class;
     77     CheckedProvider<String> provider =
     78         CheckedProviders.throwing(StringCheckedProvider.class, unexpected);
     79     String message =
     80         String.format(
     81             "exception thrown by <%s>: Not true that <%s> is an instance of <%s>. "
     82                 + "It is an instance of <%s>",
     83             getThrowingProviderName(UnsupportedOperationException.class.getName()),
     84             UnsupportedOperationException.class.getName(),
     85             SummerException.class.getName(),
     86             UnsupportedOperationException.class.getName());
     87 
     88     expectWhenTesting().that(provider).thrownException().isInstanceOf(expected);
     89     assertThat(expect.getFailure()).hasMessageThat().isEqualTo(message);
     90   }
     91 
     92   @Test
     93   public void thrownException_gets_expectFailure() {
     94     String getValue = "keep WINTER IS COMING safe";
     95     CheckedProvider<String> provider = CheckedProviders.of(StringCheckedProvider.class, getValue);
     96     String message =
     97         String.format(
     98             "Not true that <%s> threw <an exception>. It provided <%s>",
     99             getReturningProviderName(getValue), getValue);
    100 
    101     expectWhenTesting().that(provider).thrownException();
    102     assertThat(expect.getFailure()).hasMessageThat().isEqualTo(message);
    103   }
    104 
    105   private SimpleSubjectBuilder<
    106           CheckedProviderSubject<String, CheckedProvider<String>>, CheckedProvider<String>>
    107       expectWhenTesting() {
    108     return expect
    109         .whenTesting()
    110         .about(CheckedProviderSubject.<String, CheckedProvider<String>>checkedProviders());
    111   }
    112 
    113   private String getReturningProviderName(String providing) {
    114     return String.format("generated CheckedProvider returning <%s>", providing);
    115   }
    116 
    117   private String getThrowingProviderName(String throwing) {
    118     return String.format("generated CheckedProvider throwing <%s>", throwing);
    119   }
    120 }
    121