Skip to content

Commit

Permalink
Fixes mockito#2489 : Fixed codestyle
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Kozel committed Jan 22, 2022
1 parent 728c2e6 commit 7ec800d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
Expand Up @@ -315,7 +315,8 @@ private static Object tryInvoke(Method origin, Object instance, Object[] argumen
return accessor.invoke(origin, instance, arguments);
} catch (InvocationTargetException exception) {
Throwable cause = exception.getCause();
new ConditionalStackTraceFilter().filter(removeRecursiveCalls(cause, origin.getDeclaringClass()));
new ConditionalStackTraceFilter()
.filter(removeRecursiveCalls(cause, origin.getDeclaringClass()));
throw cause;
}
}
Expand All @@ -332,12 +333,13 @@ static Throwable removeRecursiveCalls(final Throwable cause, final Class<?> decl
indexesToBeRemoved.add(elementIndex);
}
}
final List<StackTraceElement> adjustedList = new ArrayList<>(Arrays.asList(cause.getStackTrace()));
final List<StackTraceElement> adjustedList =
new ArrayList<>(Arrays.asList(cause.getStackTrace()));
indexesToBeRemoved.stream()
.sorted(Comparator.reverseOrder())
.mapToInt(Integer::intValue)
.forEach(adjustedList::remove);
cause.setStackTrace(adjustedList.toArray(new StackTraceElement[]{}));
.sorted(Comparator.reverseOrder())
.mapToInt(Integer::intValue)
.forEach(adjustedList::remove);
cause.setStackTrace(adjustedList.toArray(new StackTraceElement[] {}));
return cause;
}

Expand Down
Expand Up @@ -249,13 +249,17 @@ public void should_leave_causing_stack() throws Exception {
settings, new MockHandlerImpl<>(settings), new ExceptionThrowingClass());

StackTraceElement[] returnedStack =
assertThrows(IOException.class, () -> proxy.get().throwException()).getStackTrace();
assertThrows(IOException.class, () -> proxy.get().throwException()).getStackTrace();

assertNotNull("Stack trace from mockito expected", returnedStack);

List<StackTraceElement> exceptionClassElements = Arrays.stream(returnedStack)
.filter(element -> element.getClassName().equals(ExceptionThrowingClass.class.getName()))
.collect(Collectors.toList());
List<StackTraceElement> exceptionClassElements =
Arrays.stream(returnedStack)
.filter(
element ->
element.getClassName()
.equals(ExceptionThrowingClass.class.getName()))
.collect(Collectors.toList());
assertEquals(3, exceptionClassElements.size());
assertEquals("internalThrowException", exceptionClassElements.get(0).getMethodName());
assertEquals("internalThrowException", exceptionClassElements.get(1).getMethodName());
Expand All @@ -264,26 +268,34 @@ public void should_leave_causing_stack() throws Exception {

@Test
public void should_leave_causing_stack_with_two_spies() throws Exception {
//given
// given
MockSettingsImpl<ExceptionThrowingClass> settingsEx = new MockSettingsImpl<>();
settingsEx.setTypeToMock(ExceptionThrowingClass.class);
settingsEx.defaultAnswer(Answers.CALLS_REAL_METHODS);
Optional<ExceptionThrowingClass> proxyEx =
mockMaker.createSpy(settingsEx, new MockHandlerImpl<>(settingsEx), new ExceptionThrowingClass());
mockMaker.createSpy(
settingsEx,
new MockHandlerImpl<>(settingsEx),
new ExceptionThrowingClass());

MockSettingsImpl<WrapperClass> settingsWr = new MockSettingsImpl<>();
settingsWr.setTypeToMock(WrapperClass.class);
settingsWr.defaultAnswer(Answers.CALLS_REAL_METHODS);
Optional<WrapperClass> proxyWr =
mockMaker.createSpy(settingsWr, new MockHandlerImpl<>(settingsWr), new WrapperClass());
mockMaker.createSpy(
settingsWr, new MockHandlerImpl<>(settingsWr), new WrapperClass());

//when
IOException ex = assertThrows(IOException.class, () -> proxyWr.get().callWrapped(proxyEx.get()));
List<StackTraceElement> wrapperClassElements = Arrays.stream(ex.getStackTrace())
.filter(element -> element.getClassName().equals(WrapperClass.class.getName()))
.collect(Collectors.toList());
// when
IOException ex =
assertThrows(IOException.class, () -> proxyWr.get().callWrapped(proxyEx.get()));
List<StackTraceElement> wrapperClassElements =
Arrays.stream(ex.getStackTrace())
.filter(
element ->
element.getClassName().equals(WrapperClass.class.getName()))
.collect(Collectors.toList());

//then
// then
assertEquals(1, wrapperClassElements.size());
assertEquals("callWrapped", wrapperClassElements.get(0).getMethodName());
}
Expand Down

0 comments on commit 7ec800d

Please sign in to comment.