gpt4 book ai didi

grails - Camel 模拟端点没有收到任何消息

转载 作者:行者123 更新时间:2023-12-02 01:57:35 25 4
gpt4 key购买 nike

非常简单的路线:

import org.apache.camel.builder.RouteBuilder

class TestRoutes extends RouteBuilder {
@Override
void configure(){
from("direct:foo").to("mock:bar")
}
}

非常简单的测试用例:

package mocktest

import static org.junit.Assert.*
import org.junit.*

import org.apache.camel.CamelContext
import org.apache.camel.ProducerTemplate
import org.apache.camel.test.junit4.CamelTestSupport

class TestRouteTests extends CamelTestSupport {

def CamelContext camelContext
def ProducerTemplate producerTemplate

@Test
void testSomething() {

getMockEndpoint('mock:bar').expectedMessageCount(1)
producerTemplate.sendBody('direct:foo', "Hello World")

assertMockEndpointsSatisfied()
}
}

得到这个:

| Failure:  testSomething(mocktest.TestRouteTests)
| java.lang.AssertionError: mock://bar Received message count. Expected: <1> but was: <0>

尝试使用 camel:2.12.1, camel:2.11.1今天试图解决我的头。仅包含此问题的完整测试 grails 项目位于:https://github.com/gutsal-arsen/mocktest

谁能帮我解决一下?

更新 1

  @Test
void testSimpleRoute() {
def mockEndpoint
mockEndpoint = camelContext.getEndpoint('mock:bar')
//mockEndpoint = getMockEndpoint('mock:bar')

mockEndpoint.expectedMessageCount(1)

producerTemplate.sendBody('direct:foo', 'Hello World')

mockEndpoint.assertIsSatisfied()
}

这个没有:

   @Test
void testSimpleRoute() {
def mockEndpoint
//mockEndpoint = camelContext.getEndpoint('mock:bar')
mockEndpoint = getMockEndpoint('mock:bar')

mockEndpoint.expectedMessageCount(1)

producerTemplate.sendBody('direct:foo', 'Hello World')

mockEndpoint.assertIsSatisfied()
}

所以问题出在 getMockEndpoint() 方法内部。

最佳答案

为了解决这个问题,我们应该覆盖 CamelTestSupport#createCamelContext 方法,因为默认实现创建单独的 camel 上下文,它不包含我们的路由(因此,模拟端点是假的)。

像这样:

   protected CamelContext createCamelContext() throws Exception {                                                                                                                         
return camelContext;
}

完整的源代码如下:

package mocktest

import static org.junit.Assert.*
import org.junit.*

import org.apache.camel.CamelContext
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.ProducerTemplate
import org.apache.camel.test.junit4.CamelTestSupport
import org.apache.camel.builder.RouteBuilder

import org.apache.camel.processor.interceptor.Tracer
import org.apache.camel.processor.interceptor.DefaultTraceFormatter
import org.apache.camel.LoggingLevel

class TestRouteTests extends CamelTestSupport {

def CamelContext camelContext
def ProducerTemplate producerTemplate

protected CamelContext createCamelContext() throws Exception {
return camelContext;
}

@Test
void testSomething() {
def mockEndpoint
//mockEndpoint = camelContext.getEndpoint('mock:bar') // this works
mockEndpoint = getMockEndpoint('mock:bar') // this works now also

mockEndpoint.expectedMessageCount(1)
producerTemplate.sendBody('direct:foo', "Hello World")

assertMockEndpointsSatisfied()
}
}

关于grails - Camel 模拟端点没有收到任何消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19119238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com