我无法对Multitenant进行单元测试grails域类。我得到
rg.spockframework.runtime.ConditionFailedWithExceptionError at AccountSpec.groovy:26由以下原因引起:AccountSpec.groovy:26上的org.grails.datastore.mapping.multitenancy.exceptions.TenantNotFoundException
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
}
}
我无法对Multitenant进行单元测试grails域类。我在AccountSpec.groovy:26遇到rg.spockframework.runtime.ConditionFailedWithExceptionError,原因是:org.grails ....
正确的做法取决于您实际要完成的工作。由于这是一个单元测试,因此我假设您正在尝试测试Account
验证并完全放弃多租户。有多种方法可以完成此操作,一种简单的方法是在测试中禁用多租户: