当我尝试在下面调用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对此提供帮助。
答案 0 :(得分:1)
您的处理器正在转换为SmppMessage,但是您的单元测试将消费者(从端点)从smpp替换为直接组件,因此消息实现为DefaultMessage。
答案 1 :(得分:0)
这对我有用。我需要用exchange.getIn()
覆盖SnmpMessage
消息,并添加一个PDU
对象,而不是XML String块。
@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
发行源之后,我发现有一条评论说要这样做。