首页 > 解决方案 > Grails 集成测试失败,并显示“通过构造函数参数表示的依赖关系不满足”

问题描述

Grails v3.3.9 和 jsonView 1.2.10

我有一个域类查询在运行与我的域类设备类的集成测试时失败,扩展了扩展 RootEntity(也是抽象的)的 ManagedEntity(抽象)。设备有两个静态的 withCriteria 查询来做急切的获取。

域类 Device.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. 

我的缩减测试看起来像这样,它只是调用静态查询(数据在 Bootstrap 中加载):

设备集成测试规范

void "建立两个 CI 之间的关系"() {

    given:

    Device pe = Device.getFullDeviceById(2)

    assert pe

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


    then:

        true


}

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

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

某人的插件中的类似问题

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

import com.softwood.domain.Device

Device pe = Device.getFullDeviceById(2)

println pe

这运行良好,没有错误 - 所以这与启动集成测试框架而不是加载我所有的控制器有关。我想写一些集成测试但不能,因为这是一个障碍。

标签: grailsintegration-testing

解决方案


您有JsonApiRestfulController一个没有无参数构造函数的控制器。您的构造函数需要一个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发送请求来验证这一点。


推荐阅读