骆驼:ClassCastException:无法将 DefaultMessage 转换为 class SnmpMessage

Camel : ClassCastException: DefaultMessage cannot be cast to class SnmpMessage

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

SnmpRoute.kt

.process { exchange ->
    val message = exchange.getIn() as SnmpMessage

SnmpRouteTest.kt

@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)

        ...
    }
}

异常

java.lang.ClassCastException: class org.apache.camel.support.DefaultMessage cannot be cast to class 
org.apache.camel.component.snmp.SnmpMessage (org.apache.camel.support.DefaultMessage and org.apache.
camel.component.snmp.SnmpMessage are in unnamed module of loader 'app')

我试图构建一个 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>]

但是我在使用这种方法时遇到了同样的问题。

我正在使用 Apache Camel v3.0.0-RC3

感谢@ShellDragon 到目前为止的帮助。

您的处理器正在转换为 SmppMessage,但您的单元测试将消费者(从端点)从 smpp 替换为直接组件,因此消息实现是 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(CamelSpringBootRunner::class) 更改为 @CamelSpringBootTest,因为在下载 3.0.0-RC3 发布源后,我发现有评论说这样做。