这是用户在 2024-3-28 22:16 为 https://camel.apache.org/manual/testing.html 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?

Testing 测试

Testing is a crucial activity in any piece of software development or integration. Typically, Camel users would use various different technologies wired together in a variety of EIPs with different endpoints, languages, bean integration, and dependency injection, so it’s very easy for things to go wrong!. Testing is the crucial weapon to ensure that things work as you would expect.
测试是软件开发或集成中至关重要的活动。通常,Camel 用户会使用各种不同的技术,将它们与不同的 EIP、端点、语言、Bean 集成和依赖注入相结合,因此很容易出现问题!测试是确保事情按照您的期望工作的关键武器。

Camel is a Java library, so you can easily wire up tests in JUnit. However, the Camel project has tried to make the testing of Camel as easy and powerful as possible, so we have introduced the following features.
Camel 是一个 Java 库,因此您可以轻松地在 JUnit 中进行测试。然而,Camel 项目已经尽力使 Camel 的测试变得简单而强大,因此我们引入了以下功能。

Testing modules 测试模块

The following modules are supported:

Component 组件 Description 描述


JUnit 5: Is an older standalone Java library letting you easily create Camel test cases using a single Java class for all your configuration and routing without.
JUnit 5:是一个较旧的独立 Java 库,可以让您使用单个 Java 类轻松创建 Camel 测试用例,无需进行任何配置和路由。


JUnit 5: Used for testing Camel in Camel Main mode
JUnit 5:用于在 Camel Main 模式下测试 Camel


JUnit 5: Used for testing Camel with Spring / Spring Boot
JUnit 5:用于测试使用 Spring / Spring Boot 的 Camel


Camel Test Infra: Camel Test Infra is a set of modules that leverage modern JUnit 5 features to abstract the provisioning and execution of test infrastructure. Among other things, it provides abstraction of the infrastructure (based on Test Containers - being the de-facto successor of the camel-testcontainers components) as well as JUnit 5 extensions for the Camel Context itself.
骆驼测试基础设施:骆驼测试基础设施是一组利用现代 JUnit 5 功能来抽象测试基础设施的模块。除其他功能外,它还提供了基于测试容器的基础设施抽象(作为骆驼测试容器组件的事实上的继任者),以及用于骆驼上下文本身的 JUnit 5 扩展。

If you are using Camel Quarkus then you can find information in its documentation how to do testing with Quarkus and Camel.
如果您正在使用 Camel Quarkus,那么您可以在其文档中找到有关如何使用 Quarkus 和 Camel 进行测试的信息。

In all approaches the test classes look pretty much the same in that they all reuse the Camel binding and injection annotations.
在所有的方法中,测试类看起来基本相同,因为它们都重用了 Camel 绑定和注入的注解。

For more details on the different testing modules, then see their respective documentation from the links in the table above.

Testing functionality 测试功能

Camel provides a set of features that are common to use when writing unit or integration tests with Camel.
Camel 提供了一组常用的功能,用于编写单元测试或集成测试。

Testing endpoints 测试终端点

Camel provides a number of endpoints which can make testing easier.

Name 姓名 Description 描述

Mock 嘲笑

For testing routes and mediation rules using mocks and allowing assertions to be added to an endpoint.

DataSet 数据集

For load & soak testing this endpoint provides a way to create huge numbers of messages for sending to components and asserting that they are consumed correctly.

DataSet Test 数据集测试

Used for automatic load a set of expected message from another endpoint which is then compared to the messages that arrives at this endpoint.

The main endpoint is the Mock endpoint which allows expectations to be added to different endpoints; you can then run your tests and assert that your expectations are met at the end.

Stubbing out physical transport technologies

If you wish to test out a route but want to avoid actually using real physical transport (for example to unit test a transformation route rather than performing a full integration test) then the following endpoints can be useful:

Name 姓名 Description 描述

Direct 直接

Direct invocation of the consumer from the producer so that single threaded (non-SEDA) in VM invocation is performed which can be useful to mock out physical transports
直接从生产者调用消费者,以便在虚拟机中执行单线程(非 SEDA)调用,这对于模拟物理传输是有用的


Deliver messages asynchronously to consumers via a BlockingQueue which is good for testing asynchronous transports
通过 BlockingQueue 以异步方式将消息传递给消费者,这对于测试异步传输非常有用


Works like SEDA but does not validate the endpoint URI, which makes stubbing very easy.
工作原理类似于 SEDA,但不验证端点 URI,这使得存根非常容易。

Testing existing routes 测试现有路线

Camel provides some features to aid during testing of existing routes where you cannot or will not use Mock etc. For example, you may have a production ready route which you want to test with some 3rd party API which sends messages into this route.
Camel 提供了一些功能,用于在测试现有路由时提供帮助,其中您不能或不会使用 Mock 等。例如,您可能有一个准备好的生产路由,您希望使用某些第三方 API 进行测试,该 API 将消息发送到该路由中。

Name 姓名 Description 描述

NotifyBuilder 通知构建器

Allows you to be notified when a certain condition has occurred. For example when the route has completed 5 messages. You can build complex expressions to match your criteria when to be notified.
允许在特定条件发生时通知您。例如,当路由完成 5 条消息时。您可以构建复杂的表达式来匹配您的通知条件。

AdviceWith 建议与

Allows you to advice (enhance) an existing route using a RouteBuilder style. For example, you can send (or send and skip) message to a Mock endpoint for validating the message send by Camel is as expected.
允许您使用 RouteBuilder 风格来增强现有路由。例如,您可以将消息发送到模拟端点以验证 Camel 发送的消息是否符合预期。

Third Party Testing libraries

There are a number of third party testing libraries that Camel users have found useful.
有许多第三方测试库被 Camel 用户认为很有用。

Name 姓名 Description 描述

Citrus Integration Framework

Framework for automated integration tests supporting a wide range of message protocols and data formats

Citrus Yaks 柑橘牦牛

YAKS is a framework to enable Cloud Native BDD testing on Kubernetes
YAKS 是一个在 Kubernetes 上实现云原生 BDD 测试的框架