我在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
答案 0 :(得分:0)
确保检查您的项目结构。属性文件应该在类路径上,以便Spring Boot可以找到并使用它。 例如,Maven在此处定义的项目结构:https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
对于Maven,您的配置文件应放在以下目录中:
src/main/resources/application.yml
src/test/resources/application.yml
答案 1 :(得分:0)
似乎未创建bean(因此出现null错误)。
尝试之一:
@Configuration
@EnableConfigurationProperties(SnmpProperties.class)
来源:https://www.baeldung.com/configuration-properties-in-spring-boot
答案 2 :(得分:0)
@CamelSpringBootTest
@SpringBootTest(classes = [SnmpTrapReceiverCamelApplication::class])
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@DisableJmx(false)
@ExtendWith(MockitoExtension::class)
@EnableAutoConfiguration
class SnmpTrapRouteTest {
object TestSnmpConstants {
const val SNMP_REAL_ENDPOINT_ID = "snmp-trap-route"
const val SNMP_DIRECT_REPLACEMENT_ENDPOINT = "direct:snmp-from"
const val TRAP_REQUEST_ID = 123456789
const val TRAP_OID = "1.2.3.4.5"
const val TRAP_PAYLOAD = "snmp-trap-payload"
}
@MockBean
lateinit var repository: IPduEventRepository
@Produce
lateinit var producerTemplate: ProducerTemplate
@Autowired
lateinit var camelContext: CamelContext
@Test
@Throws(Exception::class)
fun `Should call save method on the repository when PDU TRAP event supplied`() {
// Replace our SNMP consumer route with a dummy route than can be called from a producer internally.
// Since our snmp endpoint is an asynchronous consumer (meaning it only receives data from external events)
// we need to use the "direct:" component to allow a producer to internally call what is ordinarily an external
// event-driven endpoint. Otherwise we will get a Connection Refused error, as we cannot access the external
// system/socket.
AdviceWithRouteBuilder.adviceWith(camelContext, TestSnmpConstants.SNMP_REAL_ENDPOINT_ID) { routeBuilder ->
routeBuilder.replaceFromWith(TestSnmpConstants.SNMP_DIRECT_REPLACEMENT_ENDPOINT)
}
// Create the PDU object to send to the SNMP endpoint
val trap = PDU()
trap.type = PDU.TRAP
trap.requestID = Integer32(TestSnmpConstants.TRAP_REQUEST_ID)
trap.add(VariableBinding(OID(TestSnmpConstants.TRAP_OID), OctetString(TestSnmpConstants.TRAP_PAYLOAD)))
// "direct:" endpoints only send DefaultMessage objects. These are not castable to SnmpMessage objects,
// so need to overwrite the exchange IN message to be an SnmpMessage object
val exchange = DefaultExchange(camelContext)
exchange.setIn(SnmpMessage(camelContext, trap))
// ProducerTemplates need a default endpoint specified.
// The ProducerTemplate provides us with a producer that can directly deliver messages to consumers defined
// in the camelContext, using the "direct:" component (see above)
producerTemplate.setDefaultEndpointUri(TestSnmpConstants.SNMP_DIRECT_REPLACEMENT_ENDPOINT)
producerTemplate.send(exchange)
// Verify that the repository.save() was invoked
verify(repository, atLeast(1)).save(any())
}
}