Skip to content

Instantly share code, notes, and snippets.

@gajo4256
Last active December 6, 2015 23:09
Show Gist options
  • Select an option

  • Save gajo4256/4f2c7079fedd840e2222 to your computer and use it in GitHub Desktop.

Select an option

Save gajo4256/4f2c7079fedd840e2222 to your computer and use it in GitHub Desktop.
It will ignore all subclasses if some parent class is annotated with @IgnoreExtended. E.g. if we run TestSpecImpl it will be ignored because parent TestSpec is annotated.
/*
* Copyright 2009 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.runtime.extension.builtin;
import org.jetbrains.annotations.NotNull;
import org.spockframework.runtime.extension.AbstractAnnotationDrivenExtension;
import org.spockframework.runtime.model.FeatureInfo;
import org.spockframework.runtime.model.SpecInfo;
import spock.lang.IgnoreExtended;
import java.util.function.Consumer;
/**
* @author tgajsak
*/
public class IgnoreExtendedExtension extends AbstractAnnotationDrivenExtension<IgnoreExtended> {
public void visitSpecAnnotation(IgnoreExtended ignore, SpecInfo spec) {
spec.getSpecsTopToBottom().forEach(ignoreSubSpecs(spec.getName()));
}
@NotNull
private Consumer<SpecInfo> ignoreSubSpecs(final String annotatedSpecName) {
return new Consumer<SpecInfo>() {
boolean foundIgnoredSpec = false;
@Override
public void accept(SpecInfo specInfo) {
if (annotatedSpecName.equals(specInfo.getName()) || foundIgnoredSpec) {
foundIgnoredSpec = true;
specInfo.setSkipped(true);
}
}
};
}
public void visitFeatureAnnotation(IgnoreExtended ignore, FeatureInfo feature) {
feature.setSkipped(true);
}
}
import spock.lang.Specification
class MainSpec extends Specification {
def "this is MAIN test method"() {
expect:
1 == 1
}
}
import spock.lang.IgnoreExtended
@IgnoreExtended
class TestSpec extends MainSpec {
def "this is test method"() {
expect:
1 == 1
}
}
class TestSpecImpl extends TestSpec {
def "this is implementation method"() {
expect:
2 == 2
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment