Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added configurable interaction matching. #1308

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ public interface IMockConfiguration {
*/
IDefaultResponse getDefaultResponse();

/**
* Tells whether this mock object supports last defined return value.
* By default Spock uses a match first algorithm to determine the defined return value of a method.
*
* @return whether this mock object supports last matched response
*/
boolean useLastMatchResponseStrategy();

/**
* Tells whether a mock object stands in for all objects of the mocked type, or just for itself.
* This is an optional feature that may not be supported by a particular {@link MockImplementation}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ public interface IMockObject extends SpecificationAttachable {
*/
IDefaultResponse getDefaultResponse();

/**
* Tells whether this mock object supports last defined return value.
* By default Spock uses a match first algorithm to determine the defined return value of a method.
*
* @return whether this mock object supports last matched response
*/
boolean useLastMatchResponseStrategy();

/**
* Returns the specification that this mock object is attached to.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public GroovyMockInterceptor(IMockConfiguration mockConfiguration, Specification
@Override
public Object intercept(Object target, Method method, Object[] arguments, IResponseGenerator realMethodInvoker) {
IMockObject mockObject = new MockObject(mockConfiguration.getName(), mockConfiguration.getExactType(), target,
mockConfiguration.isVerified(), mockConfiguration.isGlobal(), mockConfiguration.getDefaultResponse(), specification, this);
mockConfiguration.isVerified(), mockConfiguration.isGlobal(), mockConfiguration.getDefaultResponse(),
mockConfiguration.useLastMatchResponseStrategy(), specification, this);

if (method.getDeclaringClass() == ISpockMockObject.class) {
return mockObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ private boolean isGetMetaClassCallOnGroovyObject(Object target, String method, O
private IMockInvocation createMockInvocation(MetaMethod metaMethod, Object target,
String methodName, Object[] arguments, boolean isStatic) {
IMockObject mockObject = new MockObject(configuration.getName(), configuration.getExactType(), target,
configuration.isVerified(), configuration.isGlobal(), configuration.getDefaultResponse(), specification, this);
configuration.isVerified(), configuration.isGlobal(), configuration.getDefaultResponse(),
configuration.useLastMatchResponseStrategy(), specification, this);
IMockMethod mockMethod;
if (metaMethod != null) {
List<Type> parameterTypes = Arrays.<Type>asList(metaMethod.getNativeParameterTypes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ public void addUnmatchedInvocation(IMockInvocation invocation) {

@Override
public IMockInteraction match(IMockInvocation invocation) {
final IMockObject mockObject = invocation.getMockObject();
if (mockObject.useLastMatchResponseStrategy())
return lastMatchStrategy(invocation);

return firstMatchStrategy(invocation);
}

private IMockInteraction firstMatchStrategy(IMockInvocation invocation) {
IMockInteraction firstMatch = null;
for (IMockInteraction interaction : interactions)
if (interaction.matches(invocation)) {
Expand All @@ -93,6 +101,17 @@ public IMockInteraction match(IMockInvocation invocation) {
return firstMatch;
}

private IMockInteraction lastMatchStrategy(IMockInvocation invocation) {
IMockInteraction lastMatch = null;
for (IMockInteraction interaction : interactions)
if (interaction.matches(invocation)) {
if (!interaction.isExhausted() || lastMatch == null)
lastMatch = interaction;
}

return lastMatch;
}

@Override
public void verifyInteractions() {
List<IMockInteraction> unsatisfiedInteractions = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public JavaMockInterceptor(IMockConfiguration mockConfiguration, Specification s
@Override
public Object intercept(Object target, Method method, Object[] arguments, IResponseGenerator realMethodInvoker) {
IMockObject mockObject = new MockObject(mockConfiguration.getName(), mockConfiguration.getExactType(),
target, mockConfiguration.isVerified(), false, mockConfiguration.getDefaultResponse(), specification, this);
target, mockConfiguration.isVerified(), false, mockConfiguration.getDefaultResponse(),
mockConfiguration.useLastMatchResponseStrategy(), specification, this);

if (method.getDeclaringClass() == ISpockMockObject.class) {
return mockObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class MockConfiguration implements IMockConfiguration {
private final List<Object> constructorArgs;
private final List<Class<?>> additionalInterfaces;
private final IDefaultResponse defaultResponse;
private final boolean useLastMatchResponseStrategy;
private final boolean global;
private final boolean verified;
private final boolean useObjenesis;
Expand All @@ -51,6 +52,7 @@ public MockConfiguration(@Nullable String name, Type type, @Nullable Object inst
this.constructorArgs = getOptionAsList(options, "constructorArgs");
this.additionalInterfaces = getOption(options, "additionalInterfaces", List.class, Collections.emptyList());
this.defaultResponse = getOption(options, "defaultResponse", IDefaultResponse.class, this.nature.getDefaultResponse());
this.useLastMatchResponseStrategy = getOption(options, "useLastMatchResponseStrategy", Boolean.class, false);
this.global = getOption(options, "global", Boolean.class, false);
this.verified = getOption(options, "verified", Boolean.class, this.nature.isVerified());
this.useObjenesis = getOption(options, "useObjenesis", Boolean.class, this.nature.isUseObjenesis());
Expand Down Expand Up @@ -103,6 +105,11 @@ public IDefaultResponse getDefaultResponse() {
return defaultResponse;
}

@Override
public boolean useLastMatchResponseStrategy() {
return useLastMatchResponseStrategy;
}

@Override
public boolean isGlobal() {
return global;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,21 @@ public class MockObject implements IMockObject {
private final boolean verified;
private final boolean global;
private final IDefaultResponse defaultResponse;
private final boolean useLastMatchResponseStrategy;
private final SpecificationAttachable mockInterceptor;

private Specification specification;

public MockObject(@Nullable String name, Type type, Object instance, boolean verified, boolean global,
IDefaultResponse defaultResponse, Specification specification, SpecificationAttachable mockInterceptor) {
IDefaultResponse defaultResponse, boolean useLastMatchResponseStrategy, Specification specification,
SpecificationAttachable mockInterceptor) {
this.name = name;
this.type = type;
this.instance = instance;
this.verified = verified;
this.global = global;
this.defaultResponse = defaultResponse;
this.useLastMatchResponseStrategy = useLastMatchResponseStrategy;
this.specification = specification;
this.mockInterceptor = mockInterceptor;
}
Expand Down Expand Up @@ -77,6 +80,11 @@ public IDefaultResponse getDefaultResponse() {
return defaultResponse;
}

@Override
public boolean useLastMatchResponseStrategy() {
return useLastMatchResponseStrategy;
}

@Override
public Specification getSpecification() {
return specification;
Expand Down
22 changes: 22 additions & 0 deletions spock-core/src/main/java/spock/mock/MockingApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public <T> T Mock(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class)
})
Expand Down Expand Up @@ -222,6 +223,7 @@ public <T> T Mock(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class)
})
Expand Down Expand Up @@ -390,6 +392,7 @@ public <T> T Stub(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class)
})
Expand Down Expand Up @@ -443,6 +446,7 @@ public <T> T Stub(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class)
})
Expand Down Expand Up @@ -502,6 +506,7 @@ public <T> T Stub(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class)
})
Expand Down Expand Up @@ -568,6 +573,7 @@ public <T> T Stub(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class)
})
Expand Down Expand Up @@ -619,6 +625,7 @@ public <T> T Spy(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class)
})
Expand Down Expand Up @@ -726,6 +733,7 @@ public <T> T Spy(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class)
})
Expand Down Expand Up @@ -783,6 +791,7 @@ public <T> T Spy(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class)
})
Expand Down Expand Up @@ -848,6 +857,7 @@ public <T> T Spy(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class)
})
Expand Down Expand Up @@ -899,6 +909,7 @@ public <T> T GroovyMock(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class),
@NamedParam(value = "global", type = Boolean.class)
Expand Down Expand Up @@ -951,6 +962,7 @@ public <T> T GroovyMock(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class),
@NamedParam(value = "global", type = Boolean.class)
Expand Down Expand Up @@ -1011,6 +1023,7 @@ public <T> T GroovyMock(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class),
@NamedParam(value = "global", type = Boolean.class)
Expand Down Expand Up @@ -1078,6 +1091,7 @@ public <T> T GroovyMock(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class),
@NamedParam(value = "global", type = Boolean.class)
Expand Down Expand Up @@ -1130,6 +1144,7 @@ public <T> T GroovyStub(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class),
@NamedParam(value = "global", type = Boolean.class)
Expand Down Expand Up @@ -1182,6 +1197,7 @@ public <T> T GroovyStub(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class),
@NamedParam(value = "global", type = Boolean.class)
Expand Down Expand Up @@ -1242,6 +1258,7 @@ public <T> T GroovyStub(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class),
@NamedParam(value = "global", type = Boolean.class)
Expand Down Expand Up @@ -1309,6 +1326,7 @@ public <T> T GroovyStub(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class),
@NamedParam(value = "global", type = Boolean.class)
Expand Down Expand Up @@ -1361,6 +1379,7 @@ public <T> T GroovySpy(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class),
@NamedParam(value = "global", type = Boolean.class)
Expand Down Expand Up @@ -1413,6 +1432,7 @@ public <T> T GroovySpy(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class),
@NamedParam(value = "global", type = Boolean.class)
Expand Down Expand Up @@ -1471,6 +1491,7 @@ public <T> T GroovySpy(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class),
@NamedParam(value = "global", type = Boolean.class)
Expand Down Expand Up @@ -1537,6 +1558,7 @@ public <T> T GroovySpy(
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "additionalInterfaces", type = List.class),
@NamedParam(value = "defaultResponse", type = IDefaultResponse.class),
@NamedParam(value = "useLastMatchResponseStrategy", type = Boolean.class),
@NamedParam(value = "verified", type = Boolean.class),
@NamedParam(value = "useObjenesis", type = Boolean.class),
@NamedParam(value = "global", type = Boolean.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2021 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
*
* http://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.smoke.mock

import spock.lang.Specification

/**
*
* @author Kamil Jędrzejuk
*/
class InteractionScopeMatching extends Specification {
List defaultMockBehaviour = Mock()
List withOverrideLastResponse = Mock([useLastMatchResponseStrategy:true])

def setup() {
defaultMockBehaviour.size() >> 1
withOverrideLastResponse.size() >> 1
}

def "interactions should use response matching algorithm depends on passed useLastMatchResponseStrategy flag when determining the stubbed reply"() {
given:
defaultMockBehaviour.size() >> 2
withOverrideLastResponse.size() >> 2

and:
withOverrideLastResponse.size() >> 3

expect:
defaultMockBehaviour.size() == 1
withOverrideLastResponse.size() == 3
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public IDefaultResponse getDefaultResponse() {
return null;
}

@Override
public boolean useLastMatchResponseStrategy() {
return false;
}

@Override
public Specification getSpecification() {
return null;
Expand Down