我在src/test/resources/application.yml
. 但我无法在我的单元测试中加载这些值。我有以下课程:
@ConfigurationProperties("snmp")
open class SnmpProperties {
var port: Int = 1611
lateinit var protocol: String
lateinit var host: String
override fun toString(): String {
return "SnmpProperties(port=$port, protocol='$protocol', host='$host')"
}
}
在生产代码中,从/src/main/resources/application.yml
.
snmp:
port: 1161
protocol: udp
host: 0.0.0.0
单元测试类:
@CamelSpringBootTest
@SpringBootApplication
@EnableAutoConfiguration
open class SnmpRouteTest : CamelTestSupport() {
@Autowired
lateinit var snmpProperties: SnmpProperties
@Mock
lateinit var repository: IPduEventRepository
@InjectMocks
lateinit var snmpTrapRoute: SnmpTrapRoute
@Before
fun setup() {
initMocks(this)
}
我试图为test
每个文件添加一个配置application.yml
文件以查看添加是否@ActiveProfiles("test")
有效,但它没有。
src/main/resources/application.yml & src/test/resources/application.yml
# Test profile
spring:
profiles: test
snmp:
port: 1161
protocol: udp
host: 0.0.0.0
我还创建了一个 TestConfiguration 类,该类创建SnmpProperties
bean 并使用以下命令将其自动装配到测试类中@EnableConfigurationProperties(TestConfiguration::class)
:
@Configuration
@EnableConfigurationProperties(SnmpProperties::class)
open class TestConfiguration {
@Bean
open fun snmpProperties() = SnmpProperties()
}
再一次,不去了。我得到的错误是:
Cannot instantiate @InjectMocks field named 'snmpTrapRoute' of type 'class org.meanwhile.in.hell.camel.snmp.receiver.route.SnmpRoute'.
You haven't provided the instance at field declaration so I tried to construct the instance.
However the constructor or the initialization block threw an exception : Parameter specified as non-null is null: method org.meanwhile.in.hell.camel.snmp.receiver.route.SnmpTrapRoute.<init>, parameter snmpProperties