@Test public void thrownException_threwUnexpected_expectFailure() { Class<? extends Throwable> expected = SummerException.class; Class<? extends Throwable> unexpected = UnsupportedOperationException.class; CheckedProvider<String> provider = CheckedProviders.throwing(StringCheckedProvider.class, unexpected); String message = String.format( "exception thrown by <%s>: Not true that <%s> is an instance of <%s>. " + "It is an instance of <%s>", getThrowingProviderName(UnsupportedOperationException.class.getName()), UnsupportedOperationException.class.getName(), SummerException.class.getName(), UnsupportedOperationException.class.getName()); expectWhenTesting().that(provider).thrownException().isInstanceOf(expected); assertThat(expect.getFailure()).hasMessageThat().isEqualTo(message); }
@Test public void providedValue_gotExpected_expectSuccess() { String expected = "keep Summer safe"; CheckedProvider<String> provider = CheckedProviders.of(StringCheckedProvider.class, expected); assertThat(provider).providedValue().isEqualTo(expected); }
@Test public void providedValue_gotUnexpected_expectFailure() { String expected = "keep Summer safe"; String unexpected = "Summer is unsafe"; CheckedProvider<String> provider = CheckedProviders.of(StringCheckedProvider.class, unexpected); String message = String.format( "value provided by <%s>: Not true that <%s> is equal to <%s>", getReturningProviderName(unexpected), unexpected, expected); expectWhenTesting().that(provider).providedValue().isEqualTo(expected); assertThat(expect.getFailure()).hasMessageThat().isEqualTo(message); }
@Test public void providedValue_throws_expectFailure() { CheckedProvider<String> provider = CheckedProviders.throwing(StringCheckedProvider.class, SummerException.class); String message = String.format( "checked provider <%s> threw an exception", getThrowingProviderName(SummerException.class.getName())); expectWhenTesting().that(provider).providedValue(); AssertionError expected = expect.getFailure(); assertThat(expected).hasCauseThat().isInstanceOf(SummerException.class); assertThat(expected).hasMessageThat().isEqualTo(message); }
@Test public void thrownException_threwExpected_expectSuccess() { CheckedProvider<?> provider = CheckedProviders.throwing(StringCheckedProvider.class, SummerException.class); assertThat(provider).thrownException().isInstanceOf(SummerException.class); }
@Test public void thrownException_gets_expectFailure() { String getValue = "keep WINTER IS COMING safe"; CheckedProvider<String> provider = CheckedProviders.of(StringCheckedProvider.class, getValue); String message = String.format( "Not true that <%s> threw <an exception>. It provided <%s>", getReturningProviderName(getValue), getValue); expectWhenTesting().that(provider).thrownException(); assertThat(expect.getFailure()).hasMessageThat().isEqualTo(message); }
private SimpleSubjectBuilder< CheckedProviderSubject<String, CheckedProvider<String>>, CheckedProvider<String>> expectWhenTesting() { return expect .whenTesting() .about(CheckedProviderSubject.<String, CheckedProvider<String>>checkedProviders()); }
public static <T, P extends CheckedProvider<T>> Subject.Factory<CheckedProviderSubject<T, P>, P> checkedProviders() { return new CheckedProviderSubjectFactory<>(); }
public static <T, P extends CheckedProvider<T>> CheckedProviderSubject<T, P> assertThat( @Nullable P provider) { return assertAbout(CheckedProviderSubject.<T, P>checkedProviders()).that(provider); }