Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Grails @Integration and Spock example that uses setupSpec
package integtest
import grails.test.mixin.integration.Integration
import grails.transaction.*
import spock.lang.*
import geb.spock.*
import grails.util.*
import java.util.concurrent.Callable
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.Future
import java.util.concurrent.TimeUnit
import spock.lang.Shared
import org.springframework.context.*
import org.grails.datastore.mapping.engine.event.*
/**
* See the API for {@link grails.test.mixin.support.GrailsUnitTestMixin} for usage instructions
*/
@Integration
@Rollback
class BookSpec extends GebSpec {
@Shared Future future
@Shared def executor
@Shared context
def setupSpec() {
context = Holders.findApplicationContext()
def fixture = {
new Book(title:"Blah").save()
}
if(context == null || !context.isActive()) {
executor = Executors.newSingleThreadExecutor()
future = executor.submit {
while(Holders.findApplicationContext() == null) {
// wait
sleep 10
}
context = Holders.findApplicationContext()
context.addApplicationListener { ApplicationEvent event ->
if(event instanceof DatastoreInitializedEvent) {
Book.withTransaction(fixture)
}
}
}
}
else {
// context already exists so just use it
Book.withTransaction(fixture)
}
}
def cleanupSpec() {
executor?.shutdown()
}
def setup() {
future.get(90, TimeUnit.SECONDS)
}
def cleanup() {
}
void "test something"() {
when:
new Book(title:"The Stand").save()
then:
Book.count() == 2
}
void "test something 2"() {
expect:
Book.count() == 1
}
}
@yackx
Copy link

yackx commented Mar 8, 2017

What have you done 😱

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment