Apache Camel Routes Testing in Spring Boot – Spring Boot中的Apache Camel路由测试

最后修改: 2022年 3月 17日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Apache Camel is a powerful open-source integration framework implementing a number of the known Enterprise Integration Patterns.

Apache Camel是一个强大的开源集成框架,实现了许多已知的企业集成模式

In this tutorial, we’ll learn how to write reliable, self-contained unit tests for our Camel routes.

在本教程中,我们将学习如何为我们的Camel路由编写可靠、独立的单元测试

First, we’ll start by creating a basic Camel application using Spring Boot. Then we’ll take a look at how we can use Camel’s Spring test support API with JUnit 5 to test our application.

首先,我们将首先使用Spring Boot创建一个基本的Camel应用程序。然后,我们将看看我们如何使用Camel的Spring测试支持API与JUnit 5来测试我们的应用程序。

2. Dependencies

2.依赖性

Assuming we have our project setup and configured to work with Spring Boot and Camel.

假设我们的项目已经设置和配置,以便与Spring Boot和Camel一起工作。

Then, we’ll need to add the camel-test-spring-junit5 dependency to our pom.xml:

然后,我们需要将camel-test-spring-junit5依赖性添加到我们的pom.xml

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-test-spring-junit5</artifactId>
    <version>3.15.0</version>
    <scope>test</scope>
</dependency>

As the name suggests, this dependency is specifically for our unit tests.

顾名思义,这个依赖是专门为我们的单元测试准备的。

3. Defining a Simple Camel Spring Boot Application

3.定义一个简单的Camel Spring Boot应用程序

Throughout this tutorial, the focus of our tests will be a simple Apache Camel Spring Boot application.

在本教程中,我们测试的重点将是一个简单的Apache Camel Spring Boot应用程序。

So let’s start by defining our application entry point:

因此,让我们从定义我们的应用程序入口点开始。

@SpringBootApplication
public class GreetingsFileSpringApplication {

    public static void main(String[] args) {
        SpringApplication.run(GreetingsFileSpringApplication.class, args);
    }
}

As we can see, this is a standard Spring Boot application.

我们可以看到,这是一个标准的Spring Boot应用程序。

3.1. Creating a Route

3.1.创建路线

Next, we’ll define a fairly elementary route:

接下来,我们将定义一个相当基本的路线。

@Component
public class GreetingsFileRouter extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        
        from("direct:start")
          .routeId("greetings-route")
          .setBody(constant("Hello Baeldung Readers!"))
          .to("file:output");
    }
}

To quickly recap, a route in Apache Camel is a fundamental building block, normally formed of a sequence of steps, executed in order by Camel, that consumes and processes a message.

简单地说,Apache Camel中的路由是一个基本构件,通常由一连串的步骤组成,由Camel按顺序执行,消耗和处理一个消息。

As we can see in our trivial example, we configure our route to consume messages from a direct endpoint called start.

正如我们在微不足道的例子中所看到的,我们将路由配置为从一个名为start直接端点消费消息。

Then, we set the message body to contain a string Hello Baeldung Readers! and write the contents of our message exchange using the file component to a file directory called output.

然后,我们将消息正文设置为包含一个字符串Hello Baeldung Readers!,并使用文件组件将我们的消息交换内容写入一个名为output的文件目录。

We also give our route an id called greetings-route. Using ids in our routes is generally considered good practice and can help us when we come to test our routes.

我们也给我们的路由一个ID,叫做greetings-route。在我们的路由中使用id通常被认为是好的做法,当我们来测试我们的路由时可以帮助我们。

3.2. Running Our Application

3.2.运行我们的应用程序

To conclude this section, if we run our application and send a message to our direct consumer endpoint, we should see our greetings text inside a file in our output directory. If we don’t specify a filename, Camel will create one for us:

最后,如果我们运行我们的应用程序并向我们的直接消费者端点发送消息,我们应该在输出目录中的一个文件中看到我们的问候语文本。如果我们不指定文件名,Camel将为我们创建一个。

$ cat output/D97099B6B2958D2-0000000000000000 
Hello Baeldung Readers!

4. A Word on Testing

4.关于测试的说法

In general, when writing clean tests, we shouldn’t depend on external services or filesystems that we might not be able to control or might suddenly stop working. This could have adverse effects on our test results.

一般来说,在编写干净的测试时,我们不应该依赖外部服务或文件系统,因为我们可能无法控制或可能突然停止工作。这可能会对我们的测试结果产生不利影响。

We also don’t want to write code in our routes specifically for our unit tests. Thankfully Camel has a set of extensions and APIs specifically for testing. So we can think of this as a kind of testing kit.

我们也不想在我们的路由中专门为单元测试写代码。幸好Camel有一套专门用于测试的扩展和API。所以我们可以把它看作是一种测试工具。

The kit makes it easier to test our Camel applications by sending messages to routes and checking that messages are received as expected.

该工具包通过向路由发送消息并检查消息是否按预期收到,使测试我们的Camel应用程序变得更加容易。

5. Testing Using @MockEndpoints

5.使用@MockEndpoints进行测试

With the last section in mind, let’s go ahead and write our first unit test:

考虑到最后一节,让我们继续写我们的第一个单元测试。

@SpringBootTest
@CamelSpringBootTest
@MockEndpoints("file:output")
class GreetingsFileRouterUnitTest {

    @Autowired
    private ProducerTemplate template;

    @EndpointInject("mock:file:output")
    private MockEndpoint mock;

    @Test
    void whenSendBody_thenGreetingReceivedSuccessfully() throws InterruptedException {
        mock.expectedBodiesReceived("Hello Baeldung Readers!");
        template.sendBody("direct:start", null);
        mock.assertIsSatisfied();
    }
}

Let’s walk through the key parts of our test.

让我们走过我们测试的关键部分。

First, we start by decorating our test class with three annotations:

首先,我们先用三个注解来装饰我们的测试类。

  • The @SpringBootTest annotation will ensure that our test bootstraps the Spring application context
  • We also use the @CamelSpringBootRunner, which brings Spring-based Camel test support to our Boot based tests
  • Finally, we add the @MockEndpoints annotation, which tells Camel which endpoints we want to make mocks for

Next, we autowire a ProducerTemplate object, which is an interface that allows us to send message exchanges to endpoints.

接下来,我们自动连接一个ProducerTemplate对象,它是一个允许我们向端点发送消息交换的接口。

Another key component is the MockEndpoint that we inject using @EndpointInject. Using this annotation tells Camel when the route starts, we want to inject our mock endpoint.

另一个关键组件是MockEndpoint,我们使用@EndpointInject来注入。使用这个注解告诉Camel,当路由开始时,我们要注入我们的模拟端点。

Now that we have all of our test setups in place, we can write our test, which consists of three steps:

现在,我们已经有了所有的测试设置,我们可以编写我们的测试,其中包括三个步骤。

  • First, lets set an expectation that our mock endpoint will receive the given message body
  • Then we’ll send a message to our direct:start endpoint using our template. Note, we’ll send a null body as our route doesn’t manipulate the incoming message body
  • To conclude our test, we use the assertIsSatisfied method to validate that our initial expectation on our mock endpoint has been satisfied

This confirms that our test is working correctly. Awesome! We now have a way to write self-contained, independent unit tests using Camel test support utilities.

这证实了我们的测试工作是正确的。棒极了!我们现在有了一种方法,可以使用Camel测试支持工具编写自足的、独立的单元测试。

6. Conclusion

6.结论

In this article, we learned how to test our Apache Camel routes in Spring Boot. First, we’ve learned how to create a simple Camel application with one route using Spring Boot.

在这篇文章中,我们学习了如何在Spring Boot中测试我们的Apache Camel路由。首先,我们学习了如何使用Spring Boot创建一个有一个路由的简单Camel应用程序。

Then learned about the recommended approach for testing our routes using some of the features available to us in Apache Camel’s built-in test support project.

然后了解了使用Apache Camel内置测试支持项目中的一些功能来测试我们的路由的推荐方法。

As always, the full source code of the article is available over on GitHub.

一如既往,文章的完整源代码可在GitHub上获得