天天看點

Service Unit Test 報告 “not a domain class” 的問題

Service Unit Test 不能調用 GORM save() 等方法的問題

Either class [homemaking.domain.account.Role] is not a domain class or GORM has not been initialized correctly or has already been shutdown. Ensure GORM is loaded and configured correctly before calling any methods on a GORM entity.

原因:

發現是修改了 domain 所在的包名導緻的問題,如果不改包名,就不會有問題。

具體來說就是 domain 測試類所在的包名必須對應 domain 所在的包名,即 測試類是 homemaking.UserServiceSpec 那麼 domain class 必須在 homemaking 包下!

并且和 profile 沒有多大關系,即 profile = rest-api-plugin 的也能正确執行 service unit test!

是以,當報告 “is not a domain class or GORM has not been initialized correctly”錯誤時,需要檢查:

  1. 測試類是否 extends HibernateSpec 而非 Specification
  2. 測試類的包是否和domain 類的包名一緻