use of org.mockito.internal.handler.MockHandlerImpl in project mockito by mockito.
the class InlineDelegateByteBuddyMockMakerTest method should_leave_causing_stack_with_two_spies.
@Test
public void should_leave_causing_stack_with_two_spies() throws Exception {
// 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());
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());
// 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
assertEquals(1, wrapperClassElements.size());
assertEquals("callWrapped", wrapperClassElements.get(0).getMethodName());
}
use of org.mockito.internal.handler.MockHandlerImpl in project mockito by mockito.
the class InlineByteBuddyMockMakerTest method test_parameters_retention.
@Test
public void test_parameters_retention() throws Exception {
assumeTrue(ClassFileVersion.ofThisVm().isAtLeast(JAVA_V8));
// Change when ByteBuddy has ASM6 - see #788
assumeTrue(ClassFileVersion.ofThisVm().isLessThan(JAVA_V9));
Class<?> typeWithParameters = new ByteBuddy().subclass(Object.class).defineMethod("foo", void.class, Visibility.PUBLIC).withParameter(String.class, "bar").intercept(StubMethod.INSTANCE).make().load(null).getLoaded();
MockCreationSettings<?> settings = settingsFor(typeWithParameters);
@SuppressWarnings("unchecked") Object proxy = mockMaker.createMock(settings, new MockHandlerImpl(settings));
assertThat(proxy.getClass()).isEqualTo(typeWithParameters);
assertThat(new TypeDescription.ForLoadedType(typeWithParameters).getDeclaredMethods().filter(named("foo")).getOnly().getParameters().getOnly().getName()).isEqualTo("bar");
}
use of org.mockito.internal.handler.MockHandlerImpl in project mockito by mockito.
the class InlineDelegateByteBuddyMockMakerTest method test_constant_dynamic_compatibility.
@Test
public void test_constant_dynamic_compatibility() throws Exception {
assumeTrue(ClassFileVersion.ofThisVm().isAtLeast(JAVA_V11));
Class<?> typeWithCondy = new ByteBuddy().subclass(Callable.class).method(named("call")).intercept(FixedValue.value(JavaConstant.Dynamic.ofNullConstant())).make().load(null).getLoaded();
MockCreationSettings<?> settings = settingsFor(typeWithCondy);
@SuppressWarnings("unchecked") Object proxy = mockMaker.createMock(settings, new MockHandlerImpl(settings));
assertThat(proxy.getClass()).isEqualTo(typeWithCondy);
}
use of org.mockito.internal.handler.MockHandlerImpl in project mockito by mockito.
the class InlineDelegateByteBuddyMockMakerTest method should_throw_exception_redefining_array.
@Test
@SuppressWarnings("unchecked")
public void should_throw_exception_redefining_array() {
int[] array = new int[5];
MockCreationSettings<? extends int[]> settings = settingsFor(array.getClass());
try {
mockMaker.createMock(settings, new MockHandlerImpl(settings));
fail("Expected a MockitoException");
} catch (MockitoException e) {
assertThat(e).hasMessageContaining("Arrays cannot be mocked");
}
}
use of org.mockito.internal.handler.MockHandlerImpl in project mockito by mockito.
the class InlineDelegateByteBuddyMockMakerTest method test_parameters_retention.
@Test
public void test_parameters_retention() throws Exception {
assumeTrue(ClassFileVersion.ofThisVm().isAtLeast(JAVA_V8));
Class<?> typeWithParameters = new ByteBuddy().subclass(Object.class).defineMethod("foo", void.class, Visibility.PUBLIC).withParameter(String.class, "bar").intercept(StubMethod.INSTANCE).make().load(null).getLoaded();
MockCreationSettings<?> settings = settingsFor(typeWithParameters);
@SuppressWarnings("unchecked") Object proxy = mockMaker.createMock(settings, new MockHandlerImpl(settings));
assertThat(proxy.getClass()).isEqualTo(typeWithParameters);
assertThat(new TypeDescription.ForLoadedType(typeWithParameters).getDeclaredMethods().filter(named("foo")).getOnly().getParameters().getOnly().getName()).isEqualTo("bar");
}
Aggregations