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());
}
}
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());
}
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());
}
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);
}
Aggregations