Search in sources :

Example 1 with MockCreationSettings

use of org.mockito.mock.MockCreationSettings in project mockito by mockito.

the class InlineByteBuddyMockMakerTest method should_throw_exception_redefining_unmodifiable_class.

@Test
@SuppressWarnings("unchecked")
public void should_throw_exception_redefining_unmodifiable_class() {
    MockCreationSettings settings = settingsFor(int.class);
    try {
        mockMaker.createMock(settings, new MockHandlerImpl(settings));
        fail("Expected a MockitoException");
    } catch (MockitoException e) {
        e.printStackTrace();
        assertThat(e).hasMessageContaining("Could not modify all classes");
    }
}
Also used : MockCreationSettings(org.mockito.mock.MockCreationSettings) MockitoException(org.mockito.exceptions.base.MockitoException) MockHandlerImpl(org.mockito.internal.handler.MockHandlerImpl) Test(org.junit.Test)

Example 2 with MockCreationSettings

use of org.mockito.mock.MockCreationSettings in project mockito by mockito.

the class MockUtil method resetMock.

public static <T> void resetMock(T mock) {
    InternalMockHandler oldHandler = (InternalMockHandler) getMockHandler(mock);
    MockCreationSettings settings = oldHandler.getMockSettings();
    MockHandler newHandler = createMockHandler(settings);
    mockMaker.resetMock(mock, newHandler, settings);
}
Also used : MockCreationSettings(org.mockito.mock.MockCreationSettings) MockHandlerFactory.createMockHandler(org.mockito.internal.handler.MockHandlerFactory.createMockHandler) InternalMockHandler(org.mockito.internal.InternalMockHandler) MockHandler(org.mockito.invocation.MockHandler) InternalMockHandler(org.mockito.internal.InternalMockHandler)

Example 3 with MockCreationSettings

use of org.mockito.mock.MockCreationSettings in project mockito by mockito.

the class MockHandlerFactoryTest method valid_handle_result_is_permitted.

@Test
public //see issue 331
void valid_handle_result_is_permitted() throws Throwable {
    //given:
    MockCreationSettings<?> settings = (MockCreationSettings<?>) new MockSettingsImpl().defaultAnswer(new Returns(123));
    InternalMockHandler<?> handler = createMockHandler(settings);
    mock.intReturningMethod();
    Invocation invocation = super.getLastInvocation();
    //when:
    Object result = handler.handle(invocation);
    //then
    assertEquals(123, result);
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) Invocation(org.mockito.invocation.Invocation) MockCreationSettings(org.mockito.mock.MockCreationSettings) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) Test(org.junit.Test)

Example 4 with MockCreationSettings

use of org.mockito.mock.MockCreationSettings in project mockito by mockito.

the class MockHandlerFactoryTest method handle_result_must_not_be_null_for_primitives.

@Test
public //see issue 331
void handle_result_must_not_be_null_for_primitives() throws Throwable {
    //given:
    MockCreationSettings<?> settings = (MockCreationSettings<?>) new MockSettingsImpl().defaultAnswer(new Returns(null));
    InternalMockHandler<?> handler = createMockHandler(settings);
    mock.intReturningMethod();
    Invocation invocation = super.getLastInvocation();
    //when:
    Object result = handler.handle(invocation);
    //then null value is not a valid result for a primitive
    assertNotNull(result);
    assertEquals(0, result);
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) Invocation(org.mockito.invocation.Invocation) MockCreationSettings(org.mockito.mock.MockCreationSettings) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) Test(org.junit.Test)

Example 5 with MockCreationSettings

use of org.mockito.mock.MockCreationSettings in project mockito by mockito.

the class MockUtil method maybeRedefineMockName.

public static void maybeRedefineMockName(Object mock, String newName) {
    MockName mockName = getMockName(mock);
    //TODO SF hacky...
    MockCreationSettings mockSettings = getMockHandler(mock).getMockSettings();
    if (mockName.isDefault() && mockSettings instanceof CreationSettings) {
        ((CreationSettings) mockSettings).setMockName(new MockNameImpl(newName));
    }
}
Also used : MockName(org.mockito.mock.MockName) MockCreationSettings(org.mockito.mock.MockCreationSettings) CreationSettings(org.mockito.internal.creation.settings.CreationSettings) MockCreationSettings(org.mockito.mock.MockCreationSettings)

Aggregations

MockCreationSettings (org.mockito.mock.MockCreationSettings)5 Test (org.junit.Test)3 MockSettingsImpl (org.mockito.internal.creation.MockSettingsImpl)2 Returns (org.mockito.internal.stubbing.answers.Returns)2 Invocation (org.mockito.invocation.Invocation)2 MockitoException (org.mockito.exceptions.base.MockitoException)1 InternalMockHandler (org.mockito.internal.InternalMockHandler)1 CreationSettings (org.mockito.internal.creation.settings.CreationSettings)1 MockHandlerFactory.createMockHandler (org.mockito.internal.handler.MockHandlerFactory.createMockHandler)1 MockHandlerImpl (org.mockito.internal.handler.MockHandlerImpl)1 MockHandler (org.mockito.invocation.MockHandler)1 MockName (org.mockito.mock.MockName)1