Running Grails functional tests in Grails 3 - grails

This seems to be a very basic functionality that is provided by Grails but I am not able to get to work. I am trying to add some API tests for my application using the documentation provided here.
After adding the test and running grails test-app -integration I don't see tests getting executed. Below is the output of the command.
➜ myProj git:(func-test) ✗ grails test-app -integration
> Configure project :
BUILD SUCCESSFUL in 0s
6 actionable tasks: 6 up-to-date
| Tests PASSED
I don't see any tests getting executed.
Below is my test which I am expecting to fail.
package myApp
import grails.test.mixin.integration.Integration
import grails.transaction.*
import spock.lang.*
import geb.spock.*
/**
* See http://www.gebish.org/manual/current/ for more instructions
*/
#Integration
#Rollback
class UsersSpec extends GebSpec {
def setup() {
}
def cleanup() {
}
void "visit homepage"() {
when:"The home page is visited"
go '/'
then:"The title is correct"
title == "Welcome to My Application"
}
}
I have also tried running the test class directly using the following commands:
grails test-app myApp.UsersSpec
grails test-app -integration myApp.UsersSpec
grails test-app -functional
grails test-app -functional myApp.UsersSpec
But none of them seems to be executing the required test.

Assuming that your tests are defined under src/integration-test/groovy/ and you haven't done anything unusual with respect to reconfiguring your test source folders then you can run them several ways. I suggest using Gradle:
./gradlew integrationTest
(leave the ./ off if you are on Windows)

Related

Grails 3: IllegalStateException after running integration test

I keep getting an exception when I run my integration test:
import grails.testing.mixin.integration.Integration
import grails.transaction.Rollback
import org.springframework.beans.factory.annotation.Autowired
import spock.lang.Specification
#Integration
#Rollback
class EmailSpec extends Specification {
#Autowired
EmailService service
// def setup() {
// }
//
// def cleanup() {
// }
def 'test send email'() {
when: 'email gets sent'
def sendMailCalled = false
service.metaClass.sendTestEMail = {
sendMailCalled = true
}
service.sendTestEMail("test#myprovider.de")
then:
sendMailCalled == true
}
}
Result:
"C:\Program Files\Java\jdk1.8.0_112\bin\java" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2017.2.4\lib\idea_rt.jar=49498:C:\Program Files\JetBrains\IntelliJ IDEA 2017.2.4\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\user\AppData\Local\Temp\classpath.jar com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 "de.mypackeage.EmailSpec,test send email"
java.lang.IllegalStateException: No GORM implementations configured. Ensure GORM has been initialized correctly
at org.grails.datastore.gorm.GormEnhancer.findSingleDatastore(GormEnhancer.groovy:380)
at org.grails.datastore.gorm.GormEnhancer.findSingleTransactionManager(GormEnhancer.groovy:399)
at de.mypackeage.EmailSpec.test send email(EmailSpec.groovy)
Process finished with exit code -1
The test already ran without problems but I don't really know why it would'nt run anymore. Any kind of help is appreciated.
The project is set up with Grails 3.3.1
The problem was that I executed the test in IntelliJ as an JUnit test. So the grails context didn't start up and resulted in that error.
I run integration tests from IntelliJ frequently.
If you edit the configurations (Upper Right) -> Defaults -> JUnit
Set the VM options to -Dgrails.env=test -ea
That will enable the "test" profile to be executed in intellij.
Hope it helps.

Grails refuses to run my integration test

I created a grails integration test using:
$ grails create-integration-test RoundControllerIntegration
and the following code was generated:
package gcbgb
import grails.test.mixin.integration.Integration
import grails.transaction.*
import spock.lang.*
#Integration
#Rollback
class RoundControllerIntegrationSpec extends Specification {
def setup() {
}
def cleanup() {
}
void "test something"() {
expect:"fix me"
true == false
}
}
This represents a clearly failing test, but when I run integration tests using:
$ grails test-app -integration
I get no failures...
$ grails test-app -integration
> Configure project :
Gradle now uses separate output directories for each JVM language, but this build assumes a single directory for all classes from a source set. This behaviour has been deprecated and is scheduled to be removed in Gradle 5.0
at build_26x4k1g7i20kh9xdxq6diqdhm.run(/home/peter/ownCloud/workspace-halcon/gcbgb/build.gradle:19)
The setTestClassesDir(File) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use the setTestClassesDirs(FileCollection) method instead.
at build_26x4k1g7i20kh9xdxq6diqdhm.run(/home/peter/ownCloud/workspace-halcon/gcbgb/build.gradle:19)
BUILD SUCCESSFUL in 2s
6 actionable tasks: 1 executed, 5 up-to-date
| Tests PASSED

How to run Spock Integration test on Grails

Technology stack for my project is Grails 3.2.3,Groovy 2.4.7 ,Gradle 3.2.1,GORM and IDE is Intellij and backend is MongoDB.
I have implemented Spock Integraction test class TestControllerSpec and want to run single Spock integration test
what are the configuration changes required in order to run single testcase and how?
#Integration
#Rollback
class TestControllerSpec extends Specification {
#Unroll
void "temp listObjects"(){
def result
def params = [id: '123']
when:
result = controller.index(10)
then:
result == null
result.size()==0
}
}
Use command line:
grails test-app -integration
or for the particular test:
grails test-app com.best.company.BestTestClass -integration

grails test-app individual integration test

How do I execute an individual grails integration test?
grails test-app com.mytest.UserIntegrationSpec does not work
I have an integration test called com.mytest.UserIntegrationSpec:
package com.mytest
import grails.test.mixin.integration.Integration
import grails.transaction.*
import spock.lang.*
#Integration
#Rollback
class UserIntegrationSpec extends Specification {
:
Try with
grails test-app integration: com.mytest.UserIntegrationSpec.
When executing individual test I use that command and it works.

Can't install spock to grails 1.3.7 project

I created a simple hello world grails app using Grails 1.3.7:
grails create-app hello
grails create-controller hello
Then I try to install the spock plugin using:
grails install-plugin spock
And I get the following:
Error loading event script from file [/Users/wholladay/.grails/1.3.7/projects/hello/plugins/spock-0.6/scripts/_Events.groovy] startup failed:
Could not instantiate global transform class org.spockframework.compiler.SpockTransform specified at jar:file:/Users/wholladay/.ivy2/cache/org.spockframework/spock-core/jars/spock-core-0.6-groovy-1.8.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation because of exception org.spockframework.util.IncompatibleGroovyVersionException: The Spock compiler plugin cannot execute because Spock 0.6.0-groovy-1.8 is not compatible with Groovy 1.7.8. For more information, see http://versioninfo.spockframework.org
Spock location: file:/Users/wholladay/.ivy2/cache/org.spockframework/spock-core/jars/spock-core-0.6-groovy-1.8.jar
Groovy location: file:/usr/local/Cellar/grails/current/lib/groovy-all-1.7.8.jar
So I went to: http://versioninfo.spockframework.org and noticed that there is a version 0.6-groovy-1.7 of spock. So I tried:
grails install-plugin spock 0.6-groovy-1.7
But then I got the following error:
Error resolving plugin [name:spock, group:org.grails.plugins, version:0.6-groovy-1.7].
Plugin not found for name [spock] and version [0.6-groovy-1.7]
Any ideas?
Have you tried using the dependency resolution install instructions on the plugin page?
In your BuildConfig.groovy:
grails.project.dependency.resolution = {
repositories {
grailsCentral()
mavenCentral()
}
dependencies {
test "org.spockframework:spock-grails-support:0.6-groovy-1.7"
}
plugins {
test(":spock:0.6") {
exclude "spock-grails-support"
}
}
}
I think you've made simple mistake. What you want is:
grails install-plugin spock 0.6-groovy-1.7

Resources