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

时间:2018-06-26 14:17:25

标签: java spring-boot junit apache-camel

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

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

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

在运行测试时,我想模拟SQS终结点,以便实际上不连接到AWS。我该怎么做呢?我试过像这样写测试

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@MockEndpoints
public class ApplicationTests {

    @Test
    public void contextLoads() {
    }

}

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

4 个答案:

答案 0 :(得分:1)

在这里看看:http://people.apache.org/~dkulp/camel/aws-sqs.html 我也希望解决相同的问题,但是我的问题更具体。我不想在测试时与aws交谈。因此,我想使用localstack模拟aws资源。但是找不到解决办法。我目前面临的挑战是,骆驼路线应该定义为:“ aws-sqs:queuename ..”,但是当我运行localstack时,我的队列端点就像:“ http://localist:37010/queue/queuename”。

答案 1 :(得分:0)

不确定我的答案对您有多大帮助,因为我不知道 apache骆驼。我在模拟 HttpServletRequest 对象,并假设可以在此处使用相同的概念。

所以,在您的测试中

export function refresh() {
  // Do some local CRUD here.
  return {
    type: NO-OP
  };
}

您的 myRoute 类看起来类似于

@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() {
    }
}

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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'。希望这会有所帮助。