当前位置: 首页 > 知识库问答 >
问题:

Grails集成测试失败,“通过构造函数参数表示的不满意依赖”

常彭薄
2023-03-14

GrailsV3。3.9和jsonView 1.2.10

我有一个域类查询,在运行与类my domain class Device的集成测试时失败,该类扩展了ManagedEntity(abstract),它扩展了RootEntity(也是abstract)。该设备有两个静态withCriteria查询来执行即时抓取。

域类设备。groovy:

Device extends ManagedEntity {
OrgRoleInstance org
Site site
Location location
NetworkDomain domain //can only be in one domain or zero
ProviderNetwork vfNetwork  //can be part of one CSP network domain
//simpler option than deviceRoles - not an entity in this case but  a join table
Collection<Resource.ResourceRoleType> roles = [] // creates device_roles table no versioning */
Collection<FlexAttribute> attributes = []
Collection<Equipment> buildConfiguration = []
Collection<Interface> interfaces = []
Collection<Alias> aliasNames = []


boolean freeStanding = false
boolean testDevice = false
Product product  //ref to portfolio offering if exists
String deviceStatus = "Operational"  //or Ceased or ...
String licenceType  //e.g. for cisco 903 would be one of  "metro servcices", or "metro Ip services", "metro aggregation services"
String licence = "none"
String memory
String storage
String numberOfCpu
Software runtimeOS

boolean isTestEntity () {
    testDevice
}

boolean isFreeStanding () {
    freeStanding
}

static hasMany = [deviceRoles: Resource, roles: Resource.ResourceRoleType, attributes:FlexAttribute, buildConfiguration: Equipment, interfaces:Interface, aliasNames:Alias]

static belongsTo = [org:OrgRoleInstance]  //dont at providerNetwork as belongs to as we dont want cascade delete



static constraints = {
    org nullable:true
    site nullable:true
    location nullable:true
    roles nullable:true
    domain nullable:true  , validator : {NetworkDomain domain, Device dev ->
        //assumes org has been set
        if (domain == null)
            return true
        if (dev.org == null)
            log.debug "org was null, trying to validate domain is in orgs.domains list - so org must be set first"
        NetworkDomain[] validDomains = dev?.org?.domains ?: []
        boolean test = validDomains.contains(domain)
        test
    }
    vfNetwork nullable:true , validator : {ProviderNetwork vfNetwork, Device dev ->
        if (vfNetwork == null)  return true
        OrgRoleInstance vf = OrgRoleInstance.findByNameAndRole ("Vodafone", OrgRoleInstance.OrgRoleType.ServiceProvider)
        ProviderNetwork[] networks = vf?.providerNetworks ?: []
        boolean test = networks.contains (vfNetwork)
        if (test == false)
            log.debug "Vodafone provider does not yet have any ProviderNetworks to validate to, please create and save any provider networks before assigning to device, then save "
        test
    }//ensure its in vf's list of provider networks }*/
    product nullable:true
    deviceStatus nullable:true
    licenceType nullable:true
    licence nullable:true
    memory nullable:true
    storage nullable:true
    numberOfCpu nullable:true
    runtimeOS nullable:true
    attributes nullable:true
    buildConfiguration nullable:true
    interfaces nullable:true
    aliasNames nullable:true
}

String toString () {
    "Device (manHostname:$manHostName, opState:$opStatus)[id:$id]"
}

//Queries
static Device getFullDeviceById (Serializable id) {
    Device.withCriteria (uniqueResult:true) {
        join 'domain'
        join 'providerNetwork'
        join 'site'
        join 'location'
        join 'runtimeOS'
        fetchMode 'product', FetchMode.SELECT
        fetchMode 'interfaces', FetchMode.SELECT
        fetchMode 'attributes', FetchMode.SELECT
        fetchMode 'aliasNames', FetchMode.SELECT
        fetchMode 'buildConfiguration', FetchMode.SELECT

        idEq (id as Long)
    }
}

//Queries
static List<Device> getFullDeviceBySite (Serializable sid) {
    Device.withCriteria (uniqueResult:true) {
        join 'domain'
        join 'providerNetwork'
        join 'site'
        join 'location'
        join 'runtimeOS'
        fetchMode 'product', FetchMode.SELECT
        fetchMode 'interfaces', FetchMode.SELECT
        fetchMode 'attributes', FetchMode.SELECT
        fetchMode 'aliasNames', FetchMode.SELECT
        fetchMode 'buildConfiguration', FetchMode.SELECT

        site {idEq (sid as Long)}
    }
}
}

我的测试集成测试失败了(从测试报告中可以看出)

<testcase time="0.0" name="build relationship between two CI " classname="com.softwood.domain.DeviceIntegSpecSpec">

<failure type="org.springframework.beans.factory.UnsatisfiedDependencyException" message="org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.softwood.controller.JsonApiRestfulController': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.Class<?>' available: expected at least 1 bean which qualifies as autowire candidate. 

我的缩减测试如下所示,它只调用静态查询(数据在引导中加载):

设备集成测试规范

在两个CI之间建立关系(){

    given:

    Device pe = Device.getFullDeviceById(2)

    assert pe

    when : "build a ce and relate the CE and PE  "


    then:

        true


}

这似乎是对我的控制器类(我没有测试)的反对,当我运行应用程序时,它实际上运行良好(启动时没有错误)。

我怎样才能解决这个问题?我不确定这是否相关:

某人的插件中也存在类似的问题

如果我启动Groovy控制台并像这样运行查询:

import com.softwood.domain.Device

Device pe = Device.getFullDeviceById(2)

println pe

这运行得很好,没有错误——因此这与启动集成测试框架有关,而不是加载所有控制器。我想写一些集成测试,但不能,因为这是一个拦截器。

共有1个答案

师腾
2023-03-14

您有一个没有no-arg构造函数的控制器。构造函数需要一个Class参数。Spring无法知道要在那里传递什么Class,因此这被认为是无效的。您可能希望该控制器是一个抽象的父类。

编辑:

https://github.com/jeffbrown/williamwoodmanconstructor的项目简化了你的情况,消除了大部分不相关的东西。https://github.com/jeffbrown/williamwoodmanconstructor/blob/cb799545dfa76f78e8203e68cfadb09aed604544/grails-app/controllers/williamwoodmanconstructor/JsonApiRestfulController.groovy就是问题所在。

package williamwoodmanconstructor

import grails.rest.RestfulController

class JsonApiRestfulController<T> extends RestfulController<T> {

    JsonApiRestfulController(Class<T> c) {
        super(c, false)
    }
}

这是无效的。您可以通过运行应用程序并向发送请求来验证这一点http://localhost:8080/jsonApiRestful/index.

 类似资料: