gpt4 book ai didi

grails - Grails集成测试因 “Unsatisfied dependency expressed through constructor parameter”而失败

转载 作者:行者123 更新时间:2023-12-02 15:43:16 28 4
gpt4 key购买 nike

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 someone's plugin

如果我启动Groovy控制台并像这样运行查询:
import com.softwood.domain.Device

Device pe = Device.getFullDeviceById(2)

println pe

这可以正常运行而没有错误-因此,这与启动集成测试框架并且不加载我的所有 Controller 有关。我想编写一些集成测试,但不能,因为这是一个阻止程序。

最佳答案

您有一个 Controller JsonApiRestfulController,它没有no-arg构造函数。您的构造函数需要Class参数。 Spring无法知道将哪些Class传递到那里,因此被认为是无效的。您可能希望该 Controller 成为抽象父类。

编辑:

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来验证。

关于grails - Grails集成测试因 “Unsatisfied dependency expressed through constructor parameter”而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54430493/

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