Last active
December 5, 2017 02:00
-
-
Save graemerocher/d0a56cc91b6f06b304b9 to your computer and use it in GitHub Desktop.
Grails @Integration and Spock example that uses setupSpec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What have you done 😱