gpt4 book ai didi

java - 使用 Multi-Tenancy 对 Grails 域类进行单元测试

转载 作者:行者123 更新时间:2023-12-02 08:38:59 25 4
gpt4 key购买 nike

我无法对 Multi-Tenancy 的 grails 域类进行单元测试。我正在得到

AccountSpec.groovy 处的 rg.spockframework.runtime.ConditionFailedWithExceptionError:26 原因:AccountSpec.groovy 处的 org.grails.datastore.mapping.multitenancy.exceptions.TenantNotFoundException:26

 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

}
}

帐户单元测试

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'
}


}

自定义租户解析器

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

最佳答案

正确的做法取决于您实际想要实现的目标。由于这是一个单元测试,我假设您正在尝试测试 Account 验证并将 Multi-Tenancy 完全排除在外。有多种方法可以实现这一点,一种简单的方法是在测试中禁用 Multi-Tenancy :

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'
}
}

关于java - 使用 Multi-Tenancy 对 Grails 域类进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61500961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com