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

时间:2019-01-29 22:19:02

标签: grails integration-testing

grails v3.3.9 jsonView 1.2.10

我有一个域类查询,该查询在运行集成测试时失败 我的域类设备的类,扩展了ManagedEntity(抽象),它扩展了RootEntity(也是抽象的)。设备有两个静态的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中)

DeviceIntegrationTestSpec

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

    given:

    Device pe = Device.getFullDeviceById(2)

    assert pe

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


    then:

        true


}

这似乎是关于我的Controller类(我未测试)的反对,该类实际上在我运行运行应用程序时正常运行(启动时没有错误)

任何人都可以解释如何解决此问题吗?不知道这是否相关 similar problem in someones plugin

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

import com.softwood.domain.Device

Device pe = Device.getFullDeviceById(2)

println pe

这一切正常,没有错误-因此与启动集成测试框架并且不加载我的所有控制器有关

对此有任何帮助,我想编写一些集成测试并且不能做,因为这是一个障碍

1 个答案:

答案 0 :(得分:0)

您有一个控制器JsonApiRestfulController,它没有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来进行验证。