Unit testing “grails域映射比”;无”;不进行单元测试

Unit testing “grails域映射比”;无”;不进行单元测试,unit-testing,grails,gorm,Unit Testing,Grails,Gorm,目前正在使用Grails2.4.4 我试图保存我的模型,而不让它们假设双向关系。所以我添加了mappedBy[field:'none']。但它并没有在单元测试上工作。当我运行应用程序时,它可以工作,但当我运行单元测试并保存域时,它们总是采用双向关系 考虑示例(为了简单起见,我省略了约束,将字段设为null): 每当我做某事时: def user = new User().save() def organization = new Organization(leader: user).save()

目前正在使用Grails2.4.4

我试图保存我的模型,而不让它们假设双向关系。所以我添加了
mappedBy[field:'none']
。但它并没有在单元测试上工作。当我运行应用程序时,它可以工作,但当我运行单元测试并保存域时,它们总是采用双向关系

考虑示例(为了简单起见,我省略了约束,将字段设为null):

每当我做某事时:

def user = new User().save()
def organization = new Organization(leader: user).save()
user.organization = organization
user.save()

def organization2 = new Organization(leader: user).save()
// now user's organization becomes organization2

不要将单元测试用于持久性测试

单元测试中使用的GORM实现不使用数据库,只使用
ConcurrentHashMap
。大多数GORM核心功能都是受支持的,但正如您所看到的,它们之间存在差距

要正确测试持久性,请使用集成测试,默认情况下,集成测试使用内存中的H2数据库,但如果愿意,可以将DataSource.groovy中的H2数据库更改为test MySQL/Postgres/etc.数据库

def user = new User().save()
def organization = new Organization(leader: user).save()
user.organization = organization
user.save()

def organization2 = new Organization(leader: user).save()
// now user's organization becomes organization2