Java 具有多租户的单元测试Grails域类
我无法对多租户的grails域类进行单元测试。我越来越 AccountSpec.groovy:26处的rg.spockframework.runtime.ConditionFailedWithExceptionOnError 原因:org.grails.datastore.mapping.Multitenance.exceptions.TenantNotFoundException位于AccountSpec.groovy:26Java 具有多租户的单元测试Grails域类,java,grails,groovy,multi-tenant,grails-domain-class,Java,Grails,Groovy,Multi Tenant,Grails Domain Class,我无法对多租户的grails域类进行单元测试。我越来越 AccountSpec.groovy:26处的rg.spockframework.runtime.ConditionFailedWithExceptionOnError 原因:org.grails.datastore.mapping.Multitenance.exceptions.TenantNotFoundException位于AccountSpec.groovy:26 package crm import grails.go
package crm
import grails.gorm.MultiTenant
import usermanagement.User
class Account implements MultiTenant<Account> {
String avatar
String tenantId
String name
String description
Date establishedDate
String email
String mobile
String website
String fax
Date dateCreated
Date lastUpdated
User user
static constraints = {
avatar nullable:true, blank:true
name unique: 'tenantId'
description nullable: true, blank: true
establishedDate nullable: true, blank: true
fax nullable: true, blank: true
email unique:'tenantId',email: true
website unique:'tenantId',nullable: true, blank: true
}
}
打包crm
导入grails.gorm.multi租户
导入usermanagement.User
类帐户实现多租户{
字符串化身
字符串租户
字符串名
字符串描述
成立日期
字符串电子邮件
移动电话
字符串网站
字符串传真
创建日期
上次更新日期
用户用户
静态约束={
头像可为空:真,空白:真
唯一名称:“租户”
说明可为空:true,空白:true
已建立日期可为空:true,空白:true
传真可为空:真,空白:真
电子邮件唯一:'tenantId',电子邮件:true
网站唯一:'tenantId',可为空:true,空白:true
}
}
帐户的单元测试
package crm
import grails.testing.gorm.DomainUnitTest
import spock.lang.Specification
class AccountSpec extends Specification implements DomainUnitTest<Account> {
def setup() {
}
def cleanup() {
}
void 'test name cannot be null'() {
when:
domain.mobile = null
then:
!domain.validate(['mobile'])
domain.errors['mobile'].code == 'nullable'
}
}
打包crm
导入grails.testing.gorm.DomainUnitTest
导入spock.lang.Specification
类AccountSpec扩展规范实现DomainUnitTest{
def设置(){
}
def cleanup(){
}
void“测试名称不能为null”(){
什么时候:
domain.mobile=null
然后:
!domain.validate(['mobile'])
域。错误['mobile']。代码=='nullable'
}
}
自定义租户解析程序
package usermanagement
import grails.plugin.springsecurity.SpringSecurityService
import org.grails.datastore.mapping.multitenancy.AllTenantsResolver
import org.grails.datastore.mapping.multitenancy.exceptions.TenantNotFoundException
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Lazy
import org.springframework.security.core.userdetails.UserDetails
class CustomTenentResolver implements AllTenantsResolver{
@Lazy
@Autowired
SpringSecurityService springSecurityService
@Override
Iterable<Serializable> resolveTenantIds() {
return DetachedCriteria(Organisation).distinct("namespace").list()
}
@Override
Serializable resolveTenantIdentifier() throws TenantNotFoundException {
final String tenantId = organisation()
if(tenantId){
return tenantId
}
throw new TenantNotFoundException("unable to retrive tenent")
}
String organisation(){
if (springSecurityService.principal == null){
return null
}
if (springSecurityService.principal instanceof String){
return springSecurityService.principal
}
if (springSecurityService.principal instanceof UserDetails){
return User.findByUsername(((UserDetails)springSecurityService.principal).username).organisation.namespace
}
null
}
}
包用户管理
导入grails.plugin.springsecurity.SpringSecurityService
导入org.grails.datastore.mapping.multitenancy.AllTenantsResolver
导入org.grails.datastore.mapping.multitenancy.exceptions.TenantNotFoundException
导入org.springframework.beans.factory.annotation.Autowired
导入org.springframework.context.annotation.Lazy
导入org.springframework.security.core.userdetails.userdetails
类CustomTenentResolver实现AllTenantsResolver{
@懒惰的
@自动连线
SpringSecurityService SpringSecurityService
@凌驾
Iterable resolveTenantIds(){
返回DetachedCriteria(Organization).distinct(“命名空间”).list()
}
@凌驾
Serializable resolveTenantIdentifier()引发TenantNotFoundException{
最终字符串tenantId=organization()
国际单项体育联合会(国际单项体育联合会){
归还租户
}
抛出新租户NotFoundException(“无法检索租户”)
}
字符串组织(){
if(springSecurityService.principal==null){
返回空
}
if(springSecurityService.principal instanceof String){
return springSecurityService.principal
}
if(springSecurityService.principal instanceof UserDetails){
返回User.findByUsername(((UserDetails)springSecurityService.principal.username).organization.namespace
}
无效的
}
}
正确的做法取决于您实际想要实现的目标。由于这是一个单元测试,我假设您正在尝试测试帐户
验证,而不考虑多租户。有不同的方法可以实现这一点,一个简单的方法是在测试中禁用多租户:
import grails.testing.gorm.DomainUnitTest
import spock.lang.Specification
class AccountSpec extends Specification implements DomainUnitTest<Account> {
@Override
Closure doWithConfig() {
{ config ->
config.grails.gorm.multiTenancy.mode = null
}
}
void 'test name cannot be null'() {
when:
domain.mobile = null
then:
!domain.validate(['mobile'])
domain.errors['mobile'].code == 'nullable'
}
}
导入grails.testing.gorm.DomainUnitTest
导入spock.lang.Specification
类AccountSpec扩展规范实现DomainUnitTest{
@凌驾
闭包doWithConfig(){
{config->
config.grails.gorm.multitenance.mode=null
}
}
void“测试名称不能为null”(){
什么时候:
domain.mobile=null
然后:
!domain.validate(['mobile'])
域。错误['mobile']。代码=='nullable'
}
}
正确的做法取决于您实际想要实现的目标。由于这是一个单元测试,我假设您正在尝试测试帐户
验证,而不考虑多租户。有不同的方法可以实现这一点,一个简单的方法是在测试中禁用多租户:
import grails.testing.gorm.DomainUnitTest
import spock.lang.Specification
class AccountSpec extends Specification implements DomainUnitTest<Account> {
@Override
Closure doWithConfig() {
{ config ->
config.grails.gorm.multiTenancy.mode = null
}
}
void 'test name cannot be null'() {
when:
domain.mobile = null
then:
!domain.validate(['mobile'])
domain.errors['mobile'].code == 'nullable'
}
}
导入grails.testing.gorm.DomainUnitTest
导入spock.lang.Specification
类AccountSpec扩展规范实现DomainUnitTest{
@凌驾
闭包doWithConfig(){
{config->
config.grails.gorm.multitenance.mode=null
}
}
void“测试名称不能为null”(){
什么时候:
domain.mobile=null
然后:
!domain.validate(['mobile'])
域。错误['mobile']。代码=='nullable'
}
}
谢谢你,杰夫。非常喜欢你和Grails框架谢谢你,杰夫。非常喜欢你和Grails框架