Search in sources :

Example 16 with MockSettingsImpl

use of org.mockito.internal.creation.MockSettingsImpl in project mockito by mockito.

the class NoMoreInteractionsTest method noMoreInteractionsExceptionMessageShouldDescribeMock.

@Test
public void noMoreInteractionsExceptionMessageShouldDescribeMock() {
    //given
    NoMoreInteractions n = new NoMoreInteractions();
    IMethods mock = mock(IMethods.class, "a mock");
    InvocationMatcher i = new InvocationBuilder().mock(mock).toInvocationMatcher();
    InvocationContainerImpl invocations = new InvocationContainerImpl(new MockSettingsImpl());
    invocations.setInvocationForPotentialStubbing(i);
    try {
        //when
        n.verify(new VerificationDataImpl(invocations, null));
        //then
        fail();
    } catch (NoInteractionsWanted e) {
        Assertions.assertThat(e.toString()).contains(mock.toString());
    }
}
Also used : InvocationContainerImpl(org.mockito.internal.stubbing.InvocationContainerImpl) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) NoInteractionsWanted(org.mockito.exceptions.verification.NoInteractionsWanted) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) IMethods(org.mockitousage.IMethods) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 17 with MockSettingsImpl

use of org.mockito.internal.creation.MockSettingsImpl in project powermock by powermock.

the class DefaultMockCreatorTest method testMock_shouldReturnMockNameWhenSettingsHaveName.

@Test
public void testMock_shouldReturnMockNameWhenSettingsHaveName() throws NoSuchMethodException, SecurityException {
    final MockSettingsImpl<List<?>> settings = new MockSettingsImpl<List<?>>();
    settings.name("mylist");
    final List<?> result = DefaultMockCreator.mock(List.class, false, false, null, settings, List.class.getMethod("add", Object.class));
    final MockName mockName = util.getMockName(result);
    assertNotNull(mockName);
    assertEquals("mylist", mockName.toString());
}
Also used : MockName(org.mockito.mock.MockName) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) List(java.util.List) Test(org.junit.Test)

Example 18 with MockSettingsImpl

use of org.mockito.internal.creation.MockSettingsImpl in project powermock by powermock.

the class DefaultMockCreatorTest method testMock_shouldReturnClassNameWhenSettingsHaveNoName.

@Test
public void testMock_shouldReturnClassNameWhenSettingsHaveNoName() throws NoSuchMethodException, SecurityException {
    final MockSettingsImpl<List<?>> settings = new MockSettingsImpl<List<?>>();
    final List<?> result = DefaultMockCreator.mock(List.class, false, false, null, settings, List.class.getMethod("add", Object.class));
    final MockName mockName = util.getMockName(result);
    assertNotNull(mockName);
    assertEquals("list", mockName.toString());
}
Also used : MockName(org.mockito.mock.MockName) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) List(java.util.List) Test(org.junit.Test)

Example 19 with MockSettingsImpl

use of org.mockito.internal.creation.MockSettingsImpl in project powermock by powermock.

the class DefaultMockCreator method createMethodInvocationControl.

@SuppressWarnings("unchecked")
private static <T> MockData<T> createMethodInvocationControl(final String mockName, Class<T> type, Method[] methods, boolean isSpy, Object delegator, MockSettings mockSettings) {
    final MockSettingsImpl settings;
    if (mockSettings == null) {
        // We change the context classloader to the current CL in order for the Mockito
        // framework to load it's plugins (such as MockMaker) correctly.
        final ClassLoader originalCL = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(DefaultMockCreator.class.getClassLoader());
        try {
            settings = (MockSettingsImpl) Mockito.withSettings();
            Plugins.getMockMaker();
        } finally {
            Thread.currentThread().setContextClassLoader(originalCL);
        }
    } else {
        settings = (MockSettingsImpl) mockSettings;
    }
    if (isSpy) {
        settings.defaultAnswer(Mockito.CALLS_REAL_METHODS);
    }
    settings.setMockName(new MockNameImpl(mockName));
    settings.setTypeToMock(type);
    InternalMockHandler mockHandler = MockHandlerFactory.createMockHandler(settings);
    MethodInterceptorFilter filter = new PowerMockMethodInterceptorFilter(mockHandler, settings);
    final T mock = new ClassImposterizer(new DefaultInstantiatorProvider().getInstantiator(settings)).imposterise(filter, type);
    ClassLoader classLoader = mock.getClass().getClassLoader();
    if (classLoader instanceof MockClassLoader) {
        MockClassLoader mcl = (MockClassLoader) classLoader;
        mcl.cache(mock.getClass());
    }
    final MockitoMethodInvocationControl invocationControl = new MockitoMethodInvocationControl(filter, isSpy && delegator == null ? new Object() : delegator, mock, methods);
    return new MockData<T>(invocationControl, mock);
}
Also used : MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) MockClassLoader(org.powermock.core.classloader.MockClassLoader) ClassImposterizer(org.powermock.api.mockito.repackaged.ClassImposterizer) MockitoMethodInvocationControl(org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl) MockNameImpl(org.mockito.internal.util.MockNameImpl) DefaultInstantiatorProvider(org.mockito.internal.creation.instance.DefaultInstantiatorProvider) MockClassLoader(org.powermock.core.classloader.MockClassLoader) InternalMockHandler(org.mockito.internal.InternalMockHandler) MethodInterceptorFilter(org.powermock.api.mockito.repackaged.MethodInterceptorFilter)

Aggregations

MockSettingsImpl (org.mockito.internal.creation.MockSettingsImpl)19 Test (org.junit.Test)14 MockSettings (org.mockito.MockSettings)5 CollectionData (com.torodb.core.d2r.CollectionData)4 DocPartData (com.torodb.core.d2r.DocPartData)4 BackendTransactionJob (com.torodb.core.dsl.backend.BackendTransactionJob)4 InsertBackendJob (com.torodb.core.dsl.backend.InsertBackendJob)4 List (java.util.List)4 Optional (java.util.Optional)4 InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)4 Returns (org.mockito.internal.stubbing.answers.Returns)4 MockName (org.mockito.mock.MockName)4 Invocation (org.mockito.invocation.Invocation)3 AddFieldDdlJob (com.torodb.core.dsl.backend.AddFieldDdlJob)2 AddScalarDddlJob (com.torodb.core.dsl.backend.AddScalarDddlJob)2 ImmutableMetaField (com.torodb.core.transaction.metainf.ImmutableMetaField)2 ImmutableMetaScalar (com.torodb.core.transaction.metainf.ImmutableMetaScalar)2 InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)2 MockCreationSettings (org.mockito.mock.MockCreationSettings)2 AddDocPartDdlJob (com.torodb.core.dsl.backend.AddDocPartDdlJob)1