public void appendWarnings(Failure failure, String warnings) { if (isEmpty(warnings)) { return; } //TODO: this has to protect the use in case jUnit changes and this internal state logic fails Throwable throwable = (Throwable) Whitebox.getInternalState(failure, "fThrownException"); String newMessage = "contains both: actual test failure *and* Mockito warnings.\n" + warnings + "\n *** The actual failure is because of: ***\n"; ExceptionIncludingMockitoWarnings e = new ExceptionIncludingMockitoWarnings(newMessage, throwable); e.setStackTrace(throwable.getStackTrace()); Whitebox.setInternalState(failure, "fThrownException", e); }
@Test public void shouldContainWarnings() throws Exception { //when Result result = new JUnitCore().run(new ContainsWarnings()); //then assertEquals(1, result.getFailures().size()); Throwable exception = result.getFailures().get(0).getException(); assertTrue(exception instanceof ExceptionIncludingMockitoWarnings); }
@Test public void shouldReplaceException() throws Exception { //given RuntimeException actualExc = new RuntimeException("foo"); Failure failure = new Failure(Description.EMPTY, actualExc); //when hacker.appendWarnings(failure, "unused stubbing"); //then assertEquals(ExceptionIncludingMockitoWarnings.class, failure.getException().getClass()); assertEquals(actualExc, failure.getException().getCause()); Assertions.assertThat(actualExc.getStackTrace()).isEqualTo(failure.getException().getStackTrace()); }
@Test @Ignore public void shouldContainWarnings() throws Exception { //when Result result = new JUnitCore().run(new ContainsWarnings()); //then assertEquals(1, result.getFailures().size()); Throwable exception = result.getFailures().get(0).getException(); assertTrue(exception instanceof ExceptionIncludingMockitoWarnings); }