0

I am using Spock in combination with Geb for web browser testing.

I have created the "stepThrough" extension by following the instructions found here:

Spock Stepwise - Keep running testsuite after single failure

That works all fine and well, but I would like to make a new annotation. That sets a a geb feature method to "Mandatory" meaning that if this feature method fails I would like to stop execution of the rest of the test.

I like the stepThrough annotation because if one test case fails I can continue with the rest of my testing, but if something like login fails, then I would want to stop the testing because obviously nothing else would work if Login fails.

This is what I have so Far but it does not seem to be working. Where have I gone wrong?

Here is the Annotation class import org.spockframework.runtime.extension.ExtensionAnnotation

import java.lang.annotation.ElementType
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Target



@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@ExtensionAnnotation(ManditoryExtension.class)
@interface Manditory {}

Here is the extension:

class ManditoryExtension extends AbstractAnnotationDrivenExtension<Manditory>{

    void visitFeatureAnnotation(Manditory annotation, FeatureInfo feature) {
        skipFeaturesAfterFirstFailingFeature(feature)
    }

    private void skipFeaturesAfterFirstFailingFeature(final FeatureInfo feature){
        feature.getParent().getBottomSpec().addListener(new AbstractRunListener() {
            void error(ErrorInfo error) {

                if (!error.getMethod().equals(feature)) return


                for (FeatureInfo feat : feature.getSpec().getFeatures())
                    feat.setSkipped(true)
            }
        })
    }


}
switch201
  • 587
  • 1
  • 4
  • 16
  • What exactly is your question? – erdi Aug 09 '17 at 19:18
  • My question is somewhat open ended, and if it is too open ended then I can remove it. I want to know how I would go about creating a spock extension that allows me to mark a feature method as "Mandatory". What do I mean by Mandatory? that means when feature annotated with "@Manditory" fails. the rest of the feature methods will automatically be ignored. You may be wondering why i don't just use @Stepwise. and its because I only want a few feature methods to behave this way not all of them. I may be close to a solution on my own in which case I will post the answer here. – switch201 Aug 10 '17 at 18:20
  • You have only stated what you want to do and not what exactly you are struggling with, hence my question. My guess would be that you can have a look at how Stepwise extension implementation skips subsequent features after a failure but only do it if the failing feature is annotated with the Mandatory annotation. – erdi Aug 10 '17 at 19:12
  • @erdi My apologies. I have added in what I have tried so far. I would think that what I have here should work but alas it is not. – switch201 Aug 10 '17 at 20:24
  • That's way better. Looks like you're on the right track, you reversed engineered `StepwiseExtension` and adapted it to your needs. Given that it looks like it should work, what is the problem with it? Does annotating the method have no effect? Can you confirm that the extension kicks in by throwing an exception from within `ManditoryExtension. visitFeatureAnnotation()`? Can you then verify that the listener kicks in by throwing an exception from within the first statement of the `error()` method of th listener you register as part of `Manditory. skipFeaturesAfterFirstFailingFeature()`? – erdi Aug 11 '17 at 07:37
  • Btw, it's spelled `mandatory` and not `manditory`. – erdi Aug 11 '17 at 07:38

0 Answers0