-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MockitoMockMaker provides mocking of final classes and methods (#1753)
The MockitoMockMaker provides the ability to mock final classes and final methods with the InlineMockMaker of Mockito. fixes #735 and fixes #683
- Loading branch information
Showing
20 changed files
with
1,097 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
spock-core/src/main/java/org/spockframework/mock/runtime/mockito/MockitoMockMaker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.spockframework.mock.runtime.mockito; | ||
|
||
import org.spockframework.mock.CannotCreateMockException; | ||
import org.spockframework.mock.IMockObject; | ||
import org.spockframework.mock.runtime.IMockMaker; | ||
import org.spockframework.util.ReflectionUtil; | ||
import org.spockframework.util.ThreadSafe; | ||
|
||
import java.lang.reflect.Modifier; | ||
import java.util.Collections; | ||
import java.util.EnumSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
@ThreadSafe | ||
public class MockitoMockMaker implements IMockMaker { | ||
public static final MockMakerId ID = new MockMakerId("mockito"); | ||
private static final boolean mockitoAvailable = ReflectionUtil.isClassAvailable("org.mockito.Mockito"); | ||
private static final Set<MockMakerCapability> CAPABILITIES = Collections.unmodifiableSet(EnumSet.of( | ||
MockMakerCapability.INTERFACE, | ||
MockMakerCapability.CLASS, | ||
MockMakerCapability.ADDITIONAL_INTERFACES, | ||
MockMakerCapability.EXPLICIT_CONSTRUCTOR_ARGUMENTS, | ||
MockMakerCapability.FINAL_CLASS, | ||
MockMakerCapability.FINAL_METHOD | ||
)); | ||
|
||
private final MockitoMockMakerImpl impl; | ||
|
||
public MockitoMockMaker() { | ||
if (mockitoAvailable) { | ||
this.impl = new MockitoMockMakerImpl(); | ||
} else { | ||
this.impl = null; | ||
} | ||
} | ||
|
||
@Override | ||
public MockMakerId getId() { | ||
return ID; | ||
} | ||
|
||
@Override | ||
public Set<MockMakerCapability> getCapabilities() { | ||
return CAPABILITIES; | ||
} | ||
|
||
@Override | ||
public int getPriority() { | ||
return 300; | ||
} | ||
|
||
@Override | ||
public IMockObject asMockOrNull(Object object) { | ||
if (impl == null) { | ||
return null; | ||
} | ||
return impl.asMockOrNull(object); | ||
} | ||
|
||
@Override | ||
public Object makeMock(IMockCreationSettings settings) throws CannotCreateMockException { | ||
return Objects.requireNonNull(impl).makeMock(settings); | ||
} | ||
|
||
@Override | ||
public IMockabilityResult getMockability(IMockCreationSettings settings) { | ||
Class<?> mockType = settings.getMockType(); | ||
if (Modifier.isFinal(mockType.getModifiers()) && !settings.getAdditionalInterface().isEmpty()) { | ||
return () -> "Cannot mock final classes with additional interfaces."; | ||
} | ||
if (!mockitoAvailable) { | ||
return () -> "The mockito-core library >= 4.11 is missing on the class path."; | ||
} | ||
return Objects.requireNonNull(impl).isMockable(settings.getMockType()); | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
spock-core/src/main/java/org/spockframework/mock/runtime/mockito/MockitoMockMakerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.spockframework.mock.runtime.mockito; | ||
|
||
import org.mockito.MockMakers; | ||
import org.mockito.MockSettings; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoFramework; | ||
import org.mockito.exceptions.base.MockitoException; | ||
import org.mockito.invocation.Invocation; | ||
import org.mockito.invocation.InvocationContainer; | ||
import org.mockito.invocation.MockHandler; | ||
import org.mockito.mock.MockCreationSettings; | ||
import org.mockito.plugins.MockMaker; | ||
import org.spockframework.mock.CannotCreateMockException; | ||
import org.spockframework.mock.IMockObject; | ||
import org.spockframework.mock.ISpockMockObject; | ||
import org.spockframework.mock.MockNature; | ||
import org.spockframework.mock.runtime.IMockMaker; | ||
import org.spockframework.mock.runtime.IProxyBasedMockInterceptor; | ||
import org.spockframework.runtime.GroovyRuntimeUtil; | ||
import org.spockframework.util.ExceptionUtil; | ||
import org.spockframework.util.ObjectUtil; | ||
import org.spockframework.util.ReflectionUtil; | ||
import org.spockframework.util.ThreadSafe; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
@ThreadSafe | ||
class MockitoMockMakerImpl { | ||
private static final Class<?>[] CLASS_ARRAY = new Class[0]; | ||
|
||
private final MockMaker inlineMockMaker; | ||
private final Method spockMockMethod; | ||
|
||
MockitoMockMakerImpl() { | ||
MockitoFramework framework = Mockito.framework(); | ||
this.inlineMockMaker = framework.getPlugins().getInlineMockMaker(); | ||
this.spockMockMethod = ReflectionUtil.getMethodByName(ISpockMockObject.class, "$spock_get"); | ||
} | ||
|
||
IMockObject asMockOrNull(Object object) { | ||
MockHandler<?> mockHandler = inlineMockMaker.getHandler(object); | ||
if (mockHandler instanceof SpockMockHandler) { | ||
SpockMockHandler spockHandler = (SpockMockHandler) mockHandler; | ||
//This should be changed to a better API to retrieve the IMockObject, which is currently implemented in two places JavaMockInterceptor and GroovyMockInterceptor | ||
return (IMockObject) spockHandler.mockInterceptor.intercept(object, spockMockMethod, null, null); | ||
} | ||
return null; | ||
} | ||
|
||
Object makeMock(IMockMaker.IMockCreationSettings settings) throws CannotCreateMockException { | ||
try { | ||
MockSettings mockitoSettings = Mockito.withSettings(); | ||
mockitoSettings.mockMaker(MockMakers.INLINE); | ||
if (!settings.getAdditionalInterface().isEmpty()) { | ||
mockitoSettings.extraInterfaces(settings.getAdditionalInterface().toArray(CLASS_ARRAY)); | ||
} | ||
if (settings.getConstructorArgs() != null) { | ||
mockitoSettings.useConstructor(settings.getConstructorArgs().toArray(GroovyRuntimeUtil.EMPTY_ARGUMENTS)); | ||
} else if (settings.getMockNature() == MockNature.SPY) { | ||
//We need to say Mockito it shall use the constructor otherwise it will not initialize fields of the spy, see org.mockito.Mockito.spy(java.lang.Class<T>), which does the same | ||
mockitoSettings.useConstructor(); | ||
} | ||
|
||
//We do not need the verification logic of Mockito | ||
mockitoSettings.stubOnly(); | ||
|
||
applyMockMakerSettingsFromUser(settings, mockitoSettings); | ||
|
||
MockCreationSettings<Object> mockitoCreationSettings = ObjectUtil.uncheckedCast(mockitoSettings.build(settings.getMockType())); | ||
SpockMockHandler handler = new SpockMockHandler(settings.getMockInterceptor()); | ||
|
||
return inlineMockMaker.createMock(mockitoCreationSettings, handler); | ||
} catch (MockitoException ex) { | ||
throw new CannotCreateMockException(settings.getMockType(), " with " + MockitoMockMaker.ID + ": " + ex.getMessage().trim(), ex); | ||
} | ||
} | ||
|
||
private static void applyMockMakerSettingsFromUser(IMockMaker.IMockCreationSettings settings, MockSettings mockitoSettings) { | ||
IMockMaker.IMockMakerSettings mockMakerSettings = settings.getMockMakerSettings(); | ||
if (mockMakerSettings instanceof MockitoMockMakerSettings) { | ||
MockitoMockMakerSettings mockitoMakerSettings = (MockitoMockMakerSettings) mockMakerSettings; | ||
mockitoMakerSettings.applySettings(mockitoSettings); | ||
} | ||
} | ||
|
||
public IMockMaker.IMockabilityResult isMockable(Class<?> mockType) { | ||
MockMaker.TypeMockability result = inlineMockMaker.isTypeMockable(mockType); | ||
if (result.mockable()) { | ||
return IMockMaker.IMockabilityResult.MOCKABLE; | ||
} | ||
return result::nonMockableReason; | ||
} | ||
|
||
static class SpockMockHandler implements MockHandler<Object> { | ||
private final IProxyBasedMockInterceptor mockInterceptor; | ||
|
||
public SpockMockHandler(IProxyBasedMockInterceptor mockInterceptor) { | ||
this.mockInterceptor = mockInterceptor; | ||
} | ||
|
||
@Override | ||
public Object handle(Invocation invocation) { | ||
Object mock = invocation.getMock(); | ||
return mockInterceptor.intercept(mock, invocation.getMethod(), invocation.getArguments(), spockInvocation -> { | ||
try { | ||
return invocation.callRealMethod(); | ||
} catch (Throwable e) { | ||
return ExceptionUtil.sneakyThrow(e); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public MockCreationSettings<Object> getMockSettings() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public InvocationContainer getInvocationContainer() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...-core/src/main/java/org/spockframework/mock/runtime/mockito/MockitoMockMakerSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.spockframework.mock.runtime.mockito; | ||
|
||
import groovy.lang.Closure; | ||
import org.mockito.MockSettings; | ||
import org.spockframework.mock.runtime.IMockMaker; | ||
import spock.mock.MockMakers; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static org.spockframework.mock.runtime.IMockMaker.MockMakerId; | ||
|
||
public final class MockitoMockMakerSettings implements IMockMaker.IMockMakerSettings { | ||
private final Closure<?> mockitoCode; | ||
|
||
public static MockitoMockMakerSettings createSettings(Closure<?> mockitoCode) { | ||
return new MockitoMockMakerSettings(mockitoCode); | ||
} | ||
|
||
private MockitoMockMakerSettings(Closure<?> mockitoCode) { | ||
this.mockitoCode = requireNonNull(mockitoCode); | ||
} | ||
|
||
@Override | ||
public MockMakerId getMockMakerId() { | ||
return MockMakers.mockito.getMockMakerId(); | ||
} | ||
|
||
void applySettings(MockSettings mockSettings) { | ||
requireNonNull(mockSettings); | ||
callClosure(mockitoCode, mockSettings); | ||
} | ||
|
||
private static void callClosure(Closure<?> closure, Object delegate) { | ||
Closure<?> settingsClosure = (Closure<?>) closure.clone(); | ||
settingsClosure.setResolveStrategy(Closure.DELEGATE_FIRST); | ||
settingsClosure.setDelegate(delegate); | ||
settingsClosure.call(delegate); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getMockMakerId().toString(); | ||
} | ||
} |
Oops, something went wrong.