3

I want to make a few Tests which have a specific dependecy on each other. Because of this, I have one "main"-Test, which should call the other tests. Here are two example classes:

@Stepwise
public class TestClass extends GebReportingSpec{
NotAutomaticExecutedIT test = new NotAutomaticExecutedIT();

 def "anderen Test aufrufen"() {
    given:
        test."test"()
    when:
        def wert = true
    then:
        wert == true

 }

}

and

@Ignore
public class NotAutomaticExecutedIT extends GebReportingSpec {

 def "test"() {
    given:
        def trueness = true;
    when:
        def argument = true;
    then:
        argument != trueness;
 }
}

If I run the test, i get the following exception:

groovy.lang.MissingFieldException: No such field: $spock_sharedField__browser for class: org.codehaus.groovy.runtime.NullObject at geb.spock.GebSpec.getBrowser(GebSpec.groovy:40) at geb.spock.GebSpec.methodMissing(GebSpec.groovy:54) at org.gkl.kms.webapp.tests.BestellungenIT.anderen Test aufrufen(TestClass.groovy:16)

Isn't it possible to do this?

GaNew
  • 33
  • 4

1 Answers1

2

The error is because you are calling a field test which is not static, and is not annotated with @Shared. I'm not 100% sure what you're trying to do will work even if you add the @Shared annotation.

What I would do is move the common test logic to helper functions. These are functions which do not use the spock when/then blocks, but instead simply use assert. Put these helper functions into a super class and have all Specifications which will use it extend that class. Then you can do something like:

public class AutomaticSpec extends BaseSpec{

  def "This is a test"(){
     when:
       def x = some value
     then:
       super.helperFunction(x) //super not needed here, just for clarity
  }
}

and the base spec:

public class BaseSpec extends GebReportingSpec{
    def helperFunction(x){
      assert x == some condition
      true
    }
}

With this setup you should be able to use common logic in multiple tests.

EDIT: The helpers should use assert instead of returning false for failure, in order to keep spock's fancy error reporting.

jk47
  • 755
  • 4
  • 10