Before proceeding on with GRAILS CACHING with EHCACHE, I just want to take some time out for Grails Unit Testing.
Grails provide an extensive Test Suite along with SPOCK. It provides well suited templates to carry out testing for Controllers, Domains etc and Grails provides extensive documentation for that.
When we create various components like Controllers, Domains, Interceptors etc. etc., the corresponding Test Suites are being created alongside by Grails. So what else!!!. Only we need to define the Unit Test Cases.... That's it..
But I was constantly facing some Stackframe Error while I was trying to execute the Unit Test case with Gradle.
The version of JDK that I was using is of version 1.7.0_45.
But on installing and using JDK 1.7.0_80 I was able to resolve the issue.
Following are the dependencies for Testing in Gradle.
The Controller for which we will be writing Test Case is:
The detailed description about Grails Testing and other features can be read from the comprehensive Official Documentation.
Grails provide an extensive Test Suite along with SPOCK. It provides well suited templates to carry out testing for Controllers, Domains etc and Grails provides extensive documentation for that.
When we create various components like Controllers, Domains, Interceptors etc. etc., the corresponding Test Suites are being created alongside by Grails. So what else!!!. Only we need to define the Unit Test Cases.... That's it..
But I was constantly facing some Stackframe Error while I was trying to execute the Unit Test case with Gradle.
The version of JDK that I was using is of version 1.7.0_45.
But on installing and using JDK 1.7.0_80 I was able to resolve the issue.
Following are the dependencies for Testing in Gradle.
/**
* Test Configuration
*/
testCompile "org.hamcrest:hamcrest-core:1.3" // only necessary if Hamcrest matchers are used
testRuntime "cglib:cglib-nodep:3.1" // allows mocking of classes (in addition to interfaces)
testRuntime "org.objenesis:objenesis:2.1" // allows mocking of classes without default constructor (together with CGLIB)
testCompile 'junit:junit:4.12'
testCompile ('org.spockframework:spock-core:1.0-groovy-2.4'){
exclude group: 'org.codehaus.groovy'
}
Here are some of the Test Cases that I have written for reference.The Controller for which we will be writing Test Case is:
class DoctorController extends RestfulControllerThe Unit Test Case class is:{ static responseFormats = ['xml','json'] def index() { render( view:"registerDoc") } DoctorController() { super(Doctor.class, true) } def registerDoc(Doctor doctor) { println "The Doctor Name is:${doctor.name}" doctor.save() // doctor = doctor.find("1") println "The Saved Doctor is $doctor" respond doctor } def registerDoctorCommand(DoctorCommand doctorCommand) { println "The Doctor Command is:$doctorCommand" render "The Doctor name is ${doctorCommand.name}" } }
@TestFor(DoctorController)
@Mock([Doctor,Hospital])
class DoctorControllerSpec extends Specification {
def setup() {
}
def cleanup() {
}
void "test something"() {
expect:"fix me"
true == true
}
void "test index"() {
when:
controller.index()
then:
view == "/doctor/registerDoc"
}
void "test registerDoc"() {
def result
when:
println "**** Mocking ****"
Hospital hospital = new Hospital(name:'okkk')
/**
* In order to pass NULL hospital we can make the Domain constraint nullable
* as true, which is by default false.
*
* Please refer to Doctor Domain class
*
*/
Doctor doctor = new Doctor(name: '1asd', regNo: '234', spec: 'ok', hospital: hospital)
try {
result = controller.registerDoc(doctor)
}
catch (e)
{
e.printStackTrace()
}
then:
Doctor.count() == 1
}
void "test registerDoctorCommand"()
{
when:
params.name = 'ok'
params.regNo = 'ok123'
params.spec = 'ok12345'
controller.registerDoctorCommand()
then:
response.text == 'The Doctor name is ok1'
}
}
Here the DoctorControllerSpec class by extending Specification supports testing using the Groovy Test framework i.e. SPOCK
@TestFor annotation describes the Component for which we want to write the test for and @Mock depicts the components to be mocked.The detailed description about Grails Testing and other features can be read from the comprehensive Official Documentation.






