2012-05-19 21 views
4

我正在通過Smith和Ledbrook的Grails in Action。本書中的例子是針對Grails 1.1和Hibernate 1.1編寫的(根據下載的源代碼的application.properties)。Grails 1.1應用程序的集成測試不適用於Grails 2.0.3。它可能是GORM配置問題嗎?

其中一個例子是「喧譁」。我的機器上裝有Grails 2.0.3。我使用「grails create-app hubbub」創建了我自己的應用程序副本,創建了我的域類並使用Grails命令進行了測試,然後在本書的源代碼中鍵入。換句話說,我並不試圖運行在Grails 2.0.3環境中使用Grails 1.1生成的源代碼樹。 Grails 2.0.3生成了不是該示例唯一的所有配置和類。我只是在第一個地方輸入了非Grails生成的例子中的少量源代碼。

這是我的問題 - 其中一個集成測試使用save()方法來保存對象。當我運行測試時,它只包含一個save(),它會成功。如果測試包含一個以上的save()方法,不過,所有調用保存()後的第一個失敗:

| Failure: testSaveAndUpdate(com.grailsinaction.UserIntegrationTests) 
| groovy.lang.MissingMethodException: No signature of method: 
    com.grailsinaction.User.save() is applicable for argument 
    types:() values: [] 
Possible solutions: save(), save(boolean), save(java.util.Map), wait(), 
    any(), wait(long) 
at 
com.grailsinaction.UserIntegrationTests.testSaveAndUpdate 
(UserIntegrationTests.groovy:46) 

我已經重新安排了調用保存(),和第一一個總是有效,而隨後的調用總是失敗。我已經註釋掉了測試來運行每個調用來自行保存,並且每個人都可以自行成功。

這裏是在我稱之爲save()方法的類:

package com.grailsinaction 

class User { 

String userId 
String password 
String homepage 
Date dateCreated 

static constraints = { 
    userId(size: 3..20, unique: true) 
    password(size: 6..8) 
    homepage(url: true, nullable: true) 
} 

下面是關於用戶的集成測試調用保存():

package com.grailsinaction 

import grails.test.* 

class UserIntegrationTests extends GrailsUnitTestCase { 

void testFirstSaveEver() { 
    def user = new User(userId: 'joe', password: 'secret', 
     homepage: 'http://www.grailsinaction.com') 
    assertNotNull user.save() 
    assertNotNull user.id 

    def foundUser = User.get(user.id) 
    assertEquals 'joe', foundUser.userId 
} 

void testSaveAndUpdate() { 
    def user = new User(userId: 'joe', password: 'secret', 
     homepage: 'http://www.grailsinaction.com') 
    assertNotNull user.save() 

    def foundUser = User.get(user.id) 
    foundUser.password = 'sesame' 
    foundUser.save() 

    def editedUser = User.get(user.id) 
    assertEquals 'sesame', editedUser.password 
} 
} 

這是從數據配置我生成的DataSource.groovy(所有環境,但爲簡潔起見編輯了測試):

dataSource { 
    pooled = true 
    driverClassName = "org.h2.Driver" 
    username = "sa" 
    password = "" 
} 
hibernate { 
    cache.use_second_level_cache = true 
    cache.use_query_cache = false 
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' 
} 
// environment specific settings 
environments { 
    development { 
     ... 
    } 
    test { 
     dataSource { 
      dbCreate = "update" 
      url = "jdbc:h2:mem:testDb;MVCC=TRUE" 
     } 
    } 
    production { 
     dataSource { 
      ... 
      } 
     } 
    } 
} 

下面是從書中原文:

dataSource { 
    pooled = true 
    driverClassName = "org.hsqldb.jdbcDriver" 
    username = "sa" 
    password = "" 
} 
hibernate { 
    cache.use_second_level_cache=true 
    cache.use_query_cache=true 
    cache.provider_class='com.opensymphony.oscache.hibernate.OSCacheProvider' 
} 
// environment specific settings 
environments { 
    development { 
     dataSource { 
      ... 
     } 
    } 
    test { 
     dataSource { 
      dbCreate = "update" 
      url = "jdbc:hsqldb:mem:testDb" 
     } 
    } 
    production { 
     dataSource { 
      ... 
     } 
    } 
} 

最大的區別在於跳出的是,原來司機是org.hsqldb.jdbcDriver,而一個Grails的2.0.3採用的是org.h2.Driver。另一個主要區別是原始數據源url是jdbc:hsqldb:mem:testDb,而新數據源是jdbc:h2:mem:testDb; MVCC = TRUE。最後,指定的緩存機制差異很大。

難道這是一個配置問題,必須做一些與h2和hsqldb之間的差異?如果是這樣,那麼我有什麼最好的地方去了解我需要做些什麼才能做到這一點?

回答

6

在grails 2中,你不應該在集成測試中擴展GrailsUnitTestCase,將其更改爲GroovyTestCase,你應該擺脫這個問題。

由於GrailsUnitTestCase認爲這是一個單元測試,必須從域對象中模擬(然後拆除並移除)GORM方法。它沒有意識到它有真正的方法,而不是嘲笑。

+0

謝謝。現在正在工作。現在你已經指出了這一點,在從單元測試類派生的類中運行集成測試似乎很古怪,但是我會浪費大量時間來重新配置Hibernate,並且從未注意到這一點。 –

相關問題