当我尝试调用下面的 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
手动测试此路由时一样code> 实用程序,我在日志中看到以下内容:
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 到目前为止提供的帮助。
最佳答案
您的处理器正在转换为 Smpp 消息,但您的单元测试将使用者(从端点)从 smpp 替换为直接组件,因此消息实现是默认消息。
关于java - Camel : ClassCastException: DefaultMessage cannot be cast to class SnmpMessage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58857663/