Search in sources :

Example 6 with MockHandlerImpl

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());
}
Also used : java.util(java.util) ClassFileVersion(net.bytebuddy.ClassFileVersion) MockCreationSettings(org.mockito.mock.MockCreationSettings) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ByteBuddy(net.bytebuddy.ByteBuddy) SerializableMode(org.mockito.mock.SerializableMode) Callable(java.util.concurrent.Callable) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) MockitoException(org.mockito.exceptions.base.MockitoException) FixedValue(net.bytebuddy.implementation.FixedValue) StubMethod(net.bytebuddy.implementation.StubMethod) Sets(org.mockito.internal.util.collections.Sets) MockHandlerImpl(org.mockito.internal.handler.MockHandlerImpl) JAVA_V8(net.bytebuddy.ClassFileVersion.JAVA_V8) Answers(org.mockito.Answers) ElementMatchers.named(net.bytebuddy.matcher.ElementMatchers.named) JavaConstant(net.bytebuddy.utility.JavaConstant) JAVA_V11(net.bytebuddy.ClassFileVersion.JAVA_V11) Visibility(net.bytebuddy.description.modifier.Visibility) IOException(java.io.IOException) Test(org.junit.Test) Collectors(java.util.stream.Collectors) MockMaker(org.mockito.plugins.MockMaker) Assertions.fail(org.assertj.core.api.Assertions.fail) Returns(org.mockito.internal.stubbing.answers.Returns) Assume.assumeTrue(org.junit.Assume.assumeTrue) Pattern(java.util.regex.Pattern) TypeDescription(net.bytebuddy.description.type.TypeDescription) Assert(org.junit.Assert) CreationSettings(org.mockito.internal.creation.settings.CreationSettings) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) IOException(java.io.IOException) Test(org.junit.Test)

Example 7 with MockHandlerImpl

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");
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) MockHandlerImpl(org.mockito.internal.handler.MockHandlerImpl) Test(org.junit.Test)

Example 8 with MockHandlerImpl

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);
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) MockHandlerImpl(org.mockito.internal.handler.MockHandlerImpl) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 9 with MockHandlerImpl

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");
    }
}
Also used : MockitoException(org.mockito.exceptions.base.MockitoException) MockHandlerImpl(org.mockito.internal.handler.MockHandlerImpl) Test(org.junit.Test)

Example 10 with MockHandlerImpl

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");
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) MockHandlerImpl(org.mockito.internal.handler.MockHandlerImpl) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)10 MockHandlerImpl (org.mockito.internal.handler.MockHandlerImpl)10 MockitoException (org.mockito.exceptions.base.MockitoException)5 ByteBuddy (net.bytebuddy.ByteBuddy)4 MockCreationSettings (org.mockito.mock.MockCreationSettings)3 Callable (java.util.concurrent.Callable)2 MockHandler (org.mockito.invocation.MockHandler)2 IOException (java.io.IOException)1 java.util (java.util)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 ClassFileVersion (net.bytebuddy.ClassFileVersion)1 JAVA_V11 (net.bytebuddy.ClassFileVersion.JAVA_V11)1 JAVA_V8 (net.bytebuddy.ClassFileVersion.JAVA_V8)1 Visibility (net.bytebuddy.description.modifier.Visibility)1 TypeDescription (net.bytebuddy.description.type.TypeDescription)1 FixedValue (net.bytebuddy.implementation.FixedValue)1 StubMethod (net.bytebuddy.implementation.StubMethod)1 ElementMatchers.named (net.bytebuddy.matcher.ElementMatchers.named)1 JavaConstant (net.bytebuddy.utility.JavaConstant)1