0

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,

  1. Hit the datasource; check that there are no records.
  2. Do a POST request to the service to create a record.
  3. 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)
Psycho Punch
  • 6,418
  • 9
  • 53
  • 86
  • I recommend the [geb plugin](http://www.grails.org/plugin/geb) for functional tests. I've been using it along with the remote-control plugin for months. The [documentation](http://www.gebish.org/manual/current/) is great, and the [mail list](http://markmail.org/list/org.codehaus.geb.user) support is outstanding. – Ken Jun 04 '14 at 07:07
  • What version of Grails are you using? Like I said, I'm having issues with `remote-control-plugin`. – Psycho Punch Jun 04 '14 at 07:24
  • 2.3.x and 2.4.0. This works for me: def remote = new RemoteControl(); def user = remote { User.get(1) } – Ken Jun 04 '14 at 07:41
  • I'm using 2.3.9. I don't know why I'm getting `InvalidClassException` saying "local class incompatible". – Psycho Punch Jun 04 '14 at 07:45
  • I don't remember seeing that error, but I went from 2.3.7 to 2.4.0. Are you using the latest remote-control plugin? It was updated not long ago. – Ken Jun 04 '14 at 07:49
  • Yes. I'm using v1.5. No matter what I put inside the closure, I get the error. – Psycho Punch Jun 04 '14 at 07:57
  • You should probably edit your question and add the stracktrace. That might help you get a workable answer. – Ken Jun 04 '14 at 08:16

1 Answers1

0

I had this error as well, and here's how one of my domain classes looks:

class Friend extends User implements Serializable {

    private static final long serialVersionUID = 5127248463279511859L

I believe I used the IDE to generate the long. See here for the explanation.

Also, I run my functional tests in non-forked mode. That could impact the remote-control plugin. I didn't think of that earlier.

grails.project.fork = [
    test: false,
Community
  • 1
  • 1
Ken
  • 685
  • 2
  • 5
  • 11