【问题标题】:What are the approaches to testing RESTful services in Grails?在 Grails 中测试 RESTful 服务的方法有哪些?
【发布时间】:2014-06-04 05:59:04
【问题描述】:

我对 Grails 并不陌生,但我也不是很熟悉。我实际上是边走边学。我注意到的一件事是,虽然 Grails 提供了对 TDD/BDD 的内置支持,但它缺乏关于它的重要文档。特别是官方指南的 REST 部分并没有说明如何测试 RESTful 服务。

我目前正在尝试使用使用Functional Testing Plugin 创建的功能测试来测试我的代码。由于我在尝试通过 GORM 方法访问数据源时得到IllegalStateException,并且将@Mock 添加到域类并没有改变任何东西,我不得不寻找解决方法。从我的网络搜索中,我发现Remote Control Plugin 可以用于它。但是,当使用插件运行代码时,我再次收到InvalidClassException

所以我的测试逻辑是这样的,

  1. 点击数据源;检查是否没有记录。
  2. 向服务发出POST 请求以创建记录。
  3. 再次点击数据源以检查记录是否已创建。

以下是步骤 (1) 中使用 RemoteControlPlugin 组件的示例代码。

def recordCount = remote {
    //Position is a domain class
    Position.count
}
assertEquals 0, recordCount

我得到的,

| 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)

【问题讨论】:

  • 我推荐geb plugin 进行功能测试。几个月来,我一直在使用它和远程控制插件。 documentation 很棒,mail list 支持非常出色。
  • 您使用的是什么版本的 Grails?就像我说的,我遇到了remote-control-plugin 的问题。
  • 2.3.x 和 2.4.0。这对我有用: def remote = new RemoteControl(); def user = remote { User.get(1) }
  • 我使用的是 2.3.9。我不知道为什么我收到 InvalidClassException 说“本地类不兼容”。
  • 我不记得看到那个错误,但我从 2.3.7 升级到 2.4.0。你用的是最新的遥控插件吗?不久前更新了。

标签: rest grails plugins tdd bdd


【解决方案1】:

我也遇到了这个错误,这是我的一个域类的外观:

class Friend extends User implements Serializable {

    private static final long serialVersionUID = 5127248463279511859L

我相信我使用 IDE 来生成 long。解释见here

另外,我在非分叉模式下运行我的功能测试。这可能会影响远程控制插件。我之前没想到。

grails.project.fork = [
    test: false,

【讨论】:

  • 您是如何为您的 Groovy 类生成 serialVersionUID 的?我正在使用 Eclipse,但如果我正在处理 Groovy 类,它不会警告我。
  • 我这样做时使用的是 Intellij,而不必使用 Eclipse。我只想发明一个并试一试。
  • 我尝试使用 1234567 作为 UID,但仍然遇到同样的错误。
  • 我认为这还不够长。试试我上面例子中的那个。
  • 好吧,考虑到 1L 通常就足够了,我认为这不会改变任何事情......
猜你喜欢
  • 2023-03-30
  • 2016-01-06
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
  • 2011-12-03
  • 1970-01-01
  • 1970-01-01
  • 2013-06-30
相关资源
最近更新 更多