如何在测试 Camel 应用程序时模拟 Amazon SQS 终端节点



我正在编写一个使用 Camel 监听 SQS 队列的 Spring Boot 应用程序。路由定义:

from("aws-sqs://my-s3-notification-queue" +
"?amazonSQSClient=#sqsClient" +
"&deleteAfterRead=false")
.unmarshal().json(JsonLibrary.Jackson, S3EventNotification.class)
.bean(s3NotificationHandler);

sqsClient定义为@Configuration文件中的@Bean

运行测试时,我想模拟 SQS 终端节点,以便我实际上不会连接到 AWS。我该怎么做?我试过编写我的测试,例如

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@MockEndpoints
public class ApplicationTests {
@Test
public void contextLoads() {
}
}

如果我正确阅读了文档,这应该可以工作,但它仍然尝试连接到 AWS。我错过了什么?

您可以使用本地堆栈使用此,您可以从本地系统模拟多个 AWS 服务,而无需连接 AWS。

不确定我的答案对您有多大帮助,因为我不知道阿帕奇骆驼。我正在嘲笑HttpServletRequest对象,并假设可以在这里使用相同的概念。

所以,在你的测试中

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@MockEndpoints
public class ApplicationTests {
@Mock
public MyRoute myRoute;
@Before
public void init() {  
when(myRoute.configure()).thenReturn("what you are returning");
}
@Test
public void contextLoads() {
}
}

你的myRoute类看起来类似于,

class MyRoute{
public void configure(){
from("aws-sqs://my-s3-notification-queue" +
"?amazonSQSClient=#sqsClient" +
"&deleteAfterRead=false")
.unmarshal().json(JsonLibrary.Jackson, S3EventNotification.class)
.bean(s3NotificationHandler);
}
}

看看这里: http://people.apache.org/~dkulp/camel/aws-sqs.html 我也在寻求解决同样的问题,但我的问题更具体。我不想在测试时与 aws 交谈。因此,我想使用本地堆栈来模拟 aws 资源。但是想不出一种方法来做到这一点。我目前面临的挑战是,骆驼路由应该定义为:"aws-sqs:queuename.."但是当我运行localstack时,我的队列端点就像:"http://localist:37010/queue/queuename"。

Camel有一个Junit测试套件,支持模拟这些端点。 http://camel.apache.org/camel-test.html

我们实际上使用 Spock 进行单元测试,遵循相同的模式相当容易。这就是我们的设置方式:

DefaultCamelContext context
MessagePipeline messagePipeline
MockEndpoint resultEndpoint
ProducerTemplate producerTemplate
def setupMessagePipeLine() {
context = new DefaultCamelContext()
context.addRoutes(messagePipeline)
context.start()
producerTemplate = context.createProducerTemplate()
resultEndpoint = MockEndpoint.resolve(context, 'mock:log:out')
}

这里 MessagePipelines 是扩展 RouteBuilder 的类,输入路径是'direct:start',输出'mock:log:out'。希望这有所帮助。

最新更新