What's the difference between:
@Shared
MyObject myObject = new MyObject()
and
MyObject myObject
def setupSpec() {
myObject = new MyObjec()
}
Why should I use the @Shared annotation in the second example? Both are only created once, aren't they?
In your second example, you probably got this error:
Error:(22, 9) Groovyc: Only @Shared and static fields may be accessed from here
So you can choose one of those options:
use @Shared
annotation and init field in one line
@Shared
MyObject myObject = new MyObject()
use static
and init field in one line
static MyObject myObject = new MyObject()
use @Shared
annotation and init field inside setupSpec
method
@Shared
MyObject myObject
def setupSpec() {
myObject = new MyObject()
}
use static
and init field inside setupSpec
method
static MyObject myObject
def setupSpec() {
myObject = new MyObject()
}