I'm not new to Grails, but I'm not very familiar to it either. I'm practically learning as I go. One of the things I noticed is that while Grails provides built-in support for TDD/BDD, it lacks significant documentation about it. In particular, the REST section of the official guide does not say anything about how to test RESTful services.
I'm currenty trying to test my code using a functional test created using the Functional Testing Plugin. Since I was getting IllegalStateException
when trying to access the datasource via GORM methods, and adding @Mock
to domain classes did not change anything, I had to look for way to work around it. From my Web searches, I found that Remote Control Plugin can be used for it. However, yet again, I'm getting InvalidClassException
when running the code using the plugin.
So my test logic goes like,
- Hit the datasource; check that there are no records.
- Do a
POST
request to the service to create a record. - Hit the datasource again to check if the record was created.
Here's the sample code using RemoteControlPlugin
components for step (1).
def recordCount = remote {
//Position is a domain class
Position.count
}
assertEquals 0, recordCount
For which I get,
| Failure: testCreatePosition(PositionFunctionalTests)
| groovyx.remote.client.RemoteException: An exception was raised in the remote application
at groovyx.remote.client.RemoteControl.processResult(RemoteControl.groovy:129)
at groovyx.remote.client.RemoteControl.exec(RemoteControl.groovy:73)
at groovyx.remote.client.RemoteControl.exec(RemoteControl.groovy:67)
at groovyx.remote.client.RemoteControl.call(RemoteControl.groovy:81)
at PositionFunctionalTests.testCreatePosition(PositionFunctionalTests.groovy:21)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:255)
at junit.framework.TestSuite.run(TestSuite.java:250)
Caused by: java.io.InvalidClassException: PositionFunctionalTests$_testCreatePosition_closure1; local class incompatible: stream classdesc serialVersionUID = 9199922008348880909, local class serialVersionUID = -8935356421602488910
at groovyx.remote.server.CommandInvoker.instantiate(CommandInvoker.groovy:61)
at groovyx.remote.server.CommandInvoker.invokeAgainst(CommandInvoker.groovy:37)
at groovyx.remote.server.CommandChainInvoker.invokeAgainst(CommandChainInvoker.groovy:37)
at groovyx.remote.server.Receiver.invokeCommandChain(Receiver.groovy:129)
at groovyx.remote.server.Receiver.execute(Receiver.groovy:125)
at groovyx.remote.transport.http.RemoteControlServlet.doExecute(RemoteControlServlet.groovy:74)
at grails.plugin.remotecontrol.RemoteControlServlet.doExecute(RemoteControlServlet.groovy:30)
at groovyx.remote.transport.http.RemoteControlServlet.doPost(RemoteControlServlet.groovy:39)