Java Camel:ClassCastException:DefaultMessage无法强制转换为类SnmpMessage

Java Camel:ClassCastException:DefaultMessage无法强制转换为类SnmpMessage,java,unit-testing,kotlin,apache-camel,junit5,Java,Unit Testing,Kotlin,Apache Camel,Junit5,当我尝试调用下面的testTemplate.sendBody(String,Object)时,我从单元测试中得到以下ClassCastException: SnmpRoute.kt .process { exchange -> val message = exchange.getIn() as SnmpMessage @RunWith(CamelSpringBootRunner::class) @CamelSpringBootTest @SpringBootTest @Dirti

当我尝试调用下面的
testTemplate.sendBody(String,Object)
时,我从单元测试中得到以下ClassCastException:

SnmpRoute.kt

.process { exchange ->
    val message = exchange.getIn() as SnmpMessage
@RunWith(CamelSpringBootRunner::class)
@CamelSpringBootTest
@SpringBootTest
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@DisableJmx(false)
@MockEndpoints("log:*")
class SnmpRouteTest {

    object SnmpConstants {
        const val SNMP_TRAP = "<snmp><entry><oid>1.3.6.1.2.1.1.3.0</oid><value>6 days, 3:44:57.82</value></entry><entry><oid>1.3.6.1.6.3.1.1.4.1.0</oid><value>1.3.6.1.4.1.8072.2.3.0.1</value></entry><entry><oid>1.3.6.1.4.1.8072.2.3.2.1</oid><value>123456</value></entry></snmp>"
    }

    @Autowired
    lateinit var camelContext: CamelContext

    @Produce
    lateinit var testTemplate: ProducerTemplate

    ...
    ...

    @Test
    @Throws(Exception::class)
    fun testSnmpRoute() {

        AdviceWithRouteBuilder.adviceWith(camelContext, "CamelSnmpTrapRoute") { routeBuilder -> routeBuilder.replaceFromWith(SnmpConstants.DIRECT_SNMP_ENDPOINT) }

        testTemplate.sendBody(SnmpConstants.DIRECT_SNMP_ENDPOINT, SnmpConstants.SNMP_TRAP)

        ...
    }
}
SnmpRouteTest.kt

.process { exchange ->
    val message = exchange.getIn() as SnmpMessage
@RunWith(CamelSpringBootRunner::class)
@CamelSpringBootTest
@SpringBootTest
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@DisableJmx(false)
@MockEndpoints("log:*")
class SnmpRouteTest {

    object SnmpConstants {
        const val SNMP_TRAP = "<snmp><entry><oid>1.3.6.1.2.1.1.3.0</oid><value>6 days, 3:44:57.82</value></entry><entry><oid>1.3.6.1.6.3.1.1.4.1.0</oid><value>1.3.6.1.4.1.8072.2.3.0.1</value></entry><entry><oid>1.3.6.1.4.1.8072.2.3.2.1</oid><value>123456</value></entry></snmp>"
    }

    @Autowired
    lateinit var camelContext: CamelContext

    @Produce
    lateinit var testTemplate: ProducerTemplate

    ...
    ...

    @Test
    @Throws(Exception::class)
    fun testSnmpRoute() {

        AdviceWithRouteBuilder.adviceWith(camelContext, "CamelSnmpTrapRoute") { routeBuilder -> routeBuilder.replaceFromWith(SnmpConstants.DIRECT_SNMP_ENDPOINT) }

        testTemplate.sendBody(SnmpConstants.DIRECT_SNMP_ENDPOINT, SnmpConstants.SNMP_TRAP)

        ...
    }
}
我试图构造一个
SnmpMessage
对象,并在
sendBody()
调用中使用它,因为当我使用
snmptrap
实用程序手动测试此路由时,我在日志中看到以下内容:

Get In[SnmpMessage: <snmp><entry><oid>1.3.6.1.2.1.1.3.0</oid><value>12 days, 8:40:47.70</value></entry><entry><oid>1.3.6.1.6.3.1.1.4.1.0</oid><value>1.3.6.1.4.1.8072.2.3.0.1</value></entry><entry><oid>1.3.6.1.4.1.8072.2.3.2.1</oid><value>123456</value></entry></snmp>]
进入[SnmpMessage:1.3.6.1.2.1.1.3.012天,8:40:47.701.3.6.1.6.3.1.1.4.1.01.3.6.1.4.1.8072.2.3.0.11.3.6.1.4.1.8072.2.2.2.1123456]
然而,这种方法也有同样的问题

我正在使用ApacheCamel
v3.0.0-RC3


感谢@ShellDragon在此方面提供的帮助。

您的处理器正在转换为SmppMessage,但您的单元测试将消费者(从端点)从smpp替换为direct组件,因此消息实现是DefaultMessage。

这对我来说是有效的。我需要用
SnmpMessage
覆盖
exchange.getIn()
消息,并添加
PDU
对象,而不是XML字符串块

@CamelSpringBootTest
@SpringBootTest(classes = [SnmpTrapReceiverApplication::class])
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@DisableJmx(false)
@ExtendWith(MockitoExtension::class)
@EnableAutoConfiguration
class SnmpTrapRouteTest {

    @MockBean
    lateinit var repo: PojoRepo

    @Produce
    lateinit var producerTemplate: ProducerTemplate

    @Autowired
    lateinit var camelContext: CamelContext

    @Test
    @Throws(Exception::class)
    fun testSnmpRoute() {

        AdviceWithRouteBuilder.adviceWith(camelContext, "snmp-trap-route") { routeBuilder ->
            routeBuilder.replaceFromWith("direct:snmp-from")
        }

        // Create a PDU object to send to the SNMP endpoint, rather than SNMP XML
        val trap = PDU()
        trap.type = PDU.TRAP
        trap.requestID = Integer32(123456789)
        trap.add(VariableBinding(OID("1.2.3.4.5"), OctetString("snmp-trap-payload")))

        // Create a new DefaultExchange and add an SnmpMessage object as the in-message,
        // constructed with the camelContext and the PDU object
        val exchange = DefaultExchange(camelContext)
        exchange.setIn(SnmpMessage(camelContext, trap))
        producerTemplate.setDefaultEndpointUri("direct:snmp-from")
        producerTemplate.send(exchange)

        verify(repo, atLeast(1)).save(any())
    }
}

由于我使用的是JUnit 5,我将
@RunWith(camelspringboontrunner::class)
改为
@CamelSpringBootTest
,因为在下载
3.0.0-RC3
发布源代码后,我发现有一条评论这么说。

我认为克劳斯·易卜生的回答很有解释性。你已经设法破解了吗?@ShellDragon:我终于破解了。把它贴在下面作为答案。谢谢你在这方面的帮助!太好了,非常感谢克劳西布森提供的信息。帮我指明了正确的方向!