0

我在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 类,该类创建SnmpPropertiesbean 并使用以下命令将其自动装配到测试类中@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
4

3 回答 3

0

看起来 bean 没有创建(因此出现 null 错误)。

尝试:

  • @Configuration在您的 SnmpProperties 配置类之上添加
  • 添加@EnableConfigurationProperties(SnmpProperties.class)在您的测试类之上

来源:https ://www.baeldung.com/configuration-properties-in-spring-boot

于 2019-11-11T16:50:59.277 回答
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
于 2019-11-11T16:30:58.107 回答
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())
    }
}
于 2020-11-20T11:50:48.923 回答