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

fix: add interceptor around parameterized features (#953) #954

Merged
merged 1 commit into from
Feb 16, 2024
Merged
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 @@ -65,26 +65,11 @@ public void visitSpecAnnotation(T annotation, SpecInfo spec) {

spec.getAllFeatures().forEach(feature -> {

feature.addInterceptor(invocation -> {
TestContext testContext = buildContext(invocation, null);
try {
beforeTestMethod(testContext);
interceptTest(new TestMethodInvocationContext<Object>() {
@Override
public TestContext getTestContext() {
return testContext;
}

@Override
public Object proceed() throws Throwable {
invocation.proceed();
return null;
}
});
} finally {
afterTestMethod(testContext);
}
});
if (feature.isParameterized()) {
feature.addIterationInterceptor(this::getInvocationInterceptor);
} else {
feature.addInterceptor(this::getInvocationInterceptor);
}

feature.getFeatureMethod().addInterceptor(invocation -> {
try {
Expand Down Expand Up @@ -198,6 +183,28 @@ public Object proceed() throws Throwable {
});
}

@SuppressWarnings("java:S112") // Throwable is what we deal with here
private void getInvocationInterceptor(IMethodInvocation invocation) throws Throwable {
TestContext testContext = buildContext(invocation, null);
try {
beforeTestMethod(testContext);
interceptTest(new TestMethodInvocationContext<Object>() {
@Override
public TestContext getTestContext() {
return testContext;
}

@Override
public Object proceed() throws Throwable {
invocation.proceed();
return null;
}
});
} finally {
afterTestMethod(testContext);
}
}

private MicronautTestValue buildValueObject(MicronautTest micronautTest) {
if (micronautTest != null) {
return new MicronautTestValue(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/*
* Copyright 2017-2020 original 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 io.micronaut.test.spock.intercept

import io.micronaut.context.annotation.Property
Expand All @@ -21,7 +6,6 @@ import io.micronaut.test.spock.MathService
import jakarta.inject.Inject
import spock.lang.Specification
import spock.lang.Stepwise
import spock.lang.Unroll

@Stepwise
@MicronautTest
Expand All @@ -34,7 +18,6 @@ class InterceptTestSpec extends Specification {
@Inject
TestInterceptor testInterceptor

@Unroll
void "should compute #num to #expected"() {
when:
def result = mathService.compute(num)
Expand Down Expand Up @@ -62,6 +45,8 @@ class InterceptTestSpec extends Specification {
'OUT BEFORE $spock_feature_0_0',
'IN AFTER $spock_feature_0_0',
'OUT AFTER $spock_feature_0_0',
'OUT $spock_feature_0_0', // This is the implicit unroll
'IN $spock_feature_0_0',
'IN BEFORE $spock_feature_0_0',
'OUT BEFORE $spock_feature_0_0',
'IN AFTER $spock_feature_0_0',
Expand All @@ -80,13 +65,8 @@ class InterceptTestSpec extends Specification {
'OUT BEFORE $spock_feature_0_2',
// This test is $spock_feature_0_2
]

then:
assert calls.size() == expected.size()
for (int i = 0; i < expected.size(); i++) {
def a = calls.get(i)
def b = expected[i]
assert a == b
}
calls == expected
}

}
Loading