@Override public synchronized void onFailure(Throwable t) { assertFalse(called); assertThat(t).isInstanceOf(CancellationException.class); called = true; } };
public void testCheckArgument_tooManyArgs_failure() { try { Preconditions.checkArgument(false, "A %s C %s E", "b", "d", "f"); fail("no exception thrown"); } catch (IllegalArgumentException e) { assertThat(e).hasMessageThat().isEqualTo("A b C d E [f]"); } }
@Override public synchronized void terminated(State from) { assertEquals(from, Iterables.getLast(stateHistory, State.NEW)); stateHistory.add(State.TERMINATED); assertEquals(State.TERMINATED, service.state()); if (from == State.NEW) { try { service.awaitRunning(); fail(); } catch (IllegalStateException expected) { assertThat(expected).hasCauseThat().isNull(); assertThat(expected) .hasMessageThat() .isEqualTo("Expected the service " + service + " to be RUNNING, but was TERMINATED"); } } completionLatch.countDown(); }
private static void assertExecutionException(Future<?> future, Exception expectedCause) throws Exception { try { future.get(); fail("Expected ExecutionException"); } catch (ExecutionException e) { assertThat(e).hasCauseThat().isSameAs(expectedCause); } }
public void testGetCheckedUntimed_exceptionClassUsedInitCause() { try { getChecked(FAILED_FUTURE_CHECKED_EXCEPTION, ExceptionWithoutThrowableConstructor.class); fail(); } catch (ExceptionWithoutThrowableConstructor expected) { assertThat(expected).hasMessageThat().contains("mymessage"); assertThat(expected).hasCauseThat().isEqualTo(CHECKED_EXCEPTION); } }
public void testGetCheckedUntimed_ExecutionExceptionChecked() { try { getChecked(FAILED_FUTURE_CHECKED_EXCEPTION, TwoArgConstructorException.class); fail(); } catch (TwoArgConstructorException expected) { assertThat(expected).hasCauseThat().isEqualTo(CHECKED_EXCEPTION); } }
public void testGetCheckedTimed_TimeoutException() { SettableFuture<String> future = SettableFuture.create(); try { getChecked(future, TwoArgConstructorException.class, 0, SECONDS); fail(); } catch (TwoArgConstructorException expected) { assertThat(expected).hasCauseThat().isInstanceOf(TimeoutException.class); } }
@Test public void testReadWithoutRewinding() { parcel.writeInt(123); try { parcel.readInt(); fail("should have thrown"); } catch (UnreliableBehaviorError e) { assertThat(e).hasMessage("Did you forget to setDataPosition(0) before reading the parcel?"); } }
@Override public void testNulls() throws Exception { try { super.testNulls(); } catch (AssertionFailedError e) { assertWithMessage("Method did not throw null pointer OR element not in graph exception.") .that(e) .hasCauseThat() .hasMessageThat() .contains(ERROR_ELEMENT_NOT_IN_GRAPH); } }
@Test public void testWriteIntReadString() { int val = 9; parcel.writeInt(val); parcel.setDataPosition(0); try { parcel.readString(); fail("should have thrown"); } catch (RuntimeException e) { assertThat(e) .hasCauseThat() .hasMessage( "Looking for String at position 0, found Integer [9] taking 4 bytes, " + "and it is non-portable to reinterpret it"); } }
public void testTrustedGetFailure_Completed() { SettableFuture<String> future = SettableFuture.create(); future.set("261"); assertThat(future.tryInternalFastPathGetFailure()).isNull(); }
public void testCancel_notDoneInterrupt() throws Exception { InterruptibleFuture future = new InterruptibleFuture(); assertTrue(future.cancel(true)); assertTrue(future.isCancelled()); assertTrue(future.isDone()); assertTrue(future.wasInterrupted()); assertTrue(future.interruptTaskWasCalled); try { future.get(); fail("Expected CancellationException"); } catch (CancellationException e) { // See AbstractFutureCancellationCauseTest for how to set causes assertThat(e).hasCauseThat().isNull(); } }
public void testTrustedGetFailure_Failed() { SettableFuture<String> future = SettableFuture.create(); Throwable failure = new Throwable(); future.setException(failure); assertThat(future.tryInternalFastPathGetFailure()).isEqualTo(failure); }
@Override public ListenableFuture<Integer> apply(Throwable t) throws Exception { assertThat(t).isSameAs(raisedException); return secondary; } });
public void testNewDataInput_readFullyAndThenSome() { ByteArrayDataInput in = ByteStreams.newDataInput(bytes); byte[] actual = new byte[bytes.length * 2]; try { in.readFully(actual); fail("expected exception"); } catch (IllegalStateException ex) { assertThat(ex).hasCauseThat().isInstanceOf(EOFException.class); } }
@Test public void finderThrowsNiceError() { Context context = InstrumentationRegistry.getContext(); View view = new View(context); try { Utils.findRequiredView(view, android.R.id.button1, "yo mama"); fail(); } catch (IllegalStateException e) { assertThat(e).hasMessage("Required view 'button1' with ID " + android.R.id.button1 + " for yo mama was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation."); } }
private void checkLoggedCause(Throwable t) { assertThat(popLoggedThrowable()).hasCauseThat().isSameAs(t); }
public void testGetCheckedTimed_ExecutionExceptionChecked() { try { getChecked(FAILED_FUTURE_CHECKED_EXCEPTION, TwoArgConstructorException.class, 0, SECONDS); fail(); } catch (TwoArgConstructorException expected) { assertThat(expected).hasCauseThat().isEqualTo(CHECKED_EXCEPTION); } }
@Test public void testWriteStringReadInt() { String val = "test"; parcel.writeString(val); parcel.setDataPosition(0); try { parcel.readInt(); fail("should have thrown"); } catch (RuntimeException e) { assertThat(e) .hasCauseThat() .hasMessage( "Looking for Integer at position 0, found String [test] taking 16 bytes, " + "and it is non-portable to reinterpret it"); } }
public void testTrustedGetFailure_CanceledNoCause() { SettableFuture<String> future = SettableFuture.create(); future.cancel(false); assertThat(future.tryInternalFastPathGetFailure()).isNull(); }