The following is what we are doing and it worked in grails 2.0.0 and spock 0.6
A Simple Domain:
package com.grails
class User {
String name, password
def methodWithNoArguments(){
return "Object: methodWithNoArguments"
}
static constraints = {
}
}
Unit spec:
package com.grails
import spock.lang.*
import grails.test.mixin.TestFor
import grails.test.mixin.Mock
import grails.test.GrailsMock
#TestFor(UserController) // testing for
#Mock([User]) // collaborators class
UserControllerSpec extends Specification {
// DOES NOT work in 2.2.3 spock 0.7,
// WORKS in grails-2.0.0 spock 0.6
def 'create a Domain collaborator using Mock'() {
given: 'user'
// can't mock this anymore, causes classcast exception.
User user = Mock(User)
user.methodWithNoArguments() >> { "Mock: (3) methodWithNoArguments" }
when: 'we call the method'
def val1 = user.methodWithNoArguments()
then: 'value should be from the mocked method'
val1 == "Mock: (3) methodWithNoArguments"
} }
the above causes the following exception in grails 2.2.3 and spock 0.7. NOTE ( it works in grails-2.0.0 and spock 0.6 )
| java.lang.ClassCastException: com.grails.User cannot be cast to net.sf.cglib.proxy.Factory
at org.spockframework.mock.runtime.ProxyBasedMockFactory$CglibMockFactory.createMock(ProxyBasedMockFactory.java:93)
at org.spockframework.mock.runtime.ProxyBasedMockFactory.create(ProxyBasedMockFactory.java:49)
at org.spockframework.mock.runtime.JavaMockFactory.create(JavaMockFactory.java:51)
at org.spockframework.mock.runtime.CompositeMockFactory.create(CompositeMockFactory.java:44)
at org.spockframework.lang.SpecInternals.createMock(SpecInternals.java:47)
at org.spockframework.lang.SpecInternals.createMockImpl(SpecInternals.java:282)
at org.spockframework.lang.SpecInternals.MockImpl(SpecInternals.java:99)
at com.grails.UserControllerSpec.create a Domain collaborator using Mock(UserControllerSpec.groovy:94)
wondering if there could be some incompatible libraries, here is the BuildConfig.groovy
dependencies {
test "org.spockframework:spock-grails-support:0.7-groovy-2.0"
}
plugins {
runtime ":hibernate:$grailsVersion"
runtime ":jquery:1.8.3"
runtime ":resources:1.2"
build ":tomcat:$grailsVersion"
runtime ":database-migration:1.3.2"
compile ':cache:1.0.1'
test(":spock:0.7") {
exclude "spock-grails-support"
}
}
sample test app is located at git#github.com:nbostech/grails-spock-2.2.3.git
git checkout grails-2.0.0; <2.0.0>/bin/grails test-app --unit UserControllerSpec; // all the tests pass
git checkout master; <2.2.3>/bin/grails test-app --unit UserControllerSpec; // one of the test fails..
Grails and Spock mocks aren't intended to be mixed in this way. Either make it a Grails #Mock, or a Spock Mock().
Related
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.
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)
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
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
May I know why I'm unable to set the System Property with the systemProperty method when using the grails-gradle-plugin?
My build.gradle as follows:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.grails:grails-gradle-plugin:2.0.0"
}
}
version "0.1"
group "example"
apply plugin: "grails"
repositories {
grails.central() //creates a maven repo for the Grails Central repository (Core libraries and plugins)
}
grails {
grailsVersion = '2.3.5'
groovyVersion = '2.1.9'
springLoadedVersion '1.1.3'
}
dependencies {
bootstrap "org.grails.plugins:tomcat:7.0.50" // No container is deployed by default, so add this
compile 'org.grails.plugins:resources:1.2' // Just an example of adding a Grails plugin
}
test {
println "I'm in the test"
//Could not find method systemProperty() for arguments [geb.env, sauce] on root project
systemProperty 'geb.env', 'sauce'//Fails
}
In the test task, I get the following error when i run $gradle grails-test:
Could not find method systemProperty() for arguments [geb.env, sauce] on root project..."
Is this a problem with the grails-gradle plugin since other plugins like "java" allows me to use setProperty? Thanks.
The error message is correct: there's no systemProperty() method.
As Joshua Moore comments, this should work:
test {
println "I'm in the test"
System.setProperty 'geb.env', 'sauce'
}