Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.此内容没有您所选择的语言版本。
Chapter 8. To Test a Route with JUnit
This tutorial shows you how to use the New Camel Test Case wizard to create a test case for your route and then test the route.
Overview 复制链接链接已复制到粘贴板!
The New Camel Test Case wizard generates a boilerplate JUnit test case. When you create or modify a route (for example, adding more processors to it), you create or modify the generated test case to add expectations and assertions specific to the route you created or updated.This ensures that the test is valid for the route.
Goals 复制链接链接已复制到粘贴板!
In this tutorial you will:
-
Create the
/src/test/folder to store the JUnit test case -
Generate the JUnit test case for the
CBRrouteproject - Modify the newly generated JUnit test case
-
Modify the
CBRrouteproject’spom.xmlfile -
Run the
CBRroutewith the new JUnit test case - Observe the output
Prerequisites 复制链接链接已复制到粘贴板!
To complete this tutorial you need the CBRroute project you used in Chapter 7, To Trace a Message Through a Route
If you skipped any tutorial after Chapter 2, To Create a New Route, you can use the prefabricated blueprintContext6.xml file to work through this tutorial (for details, see Chapter 1, Using the Fuse Tooling Resource Files).
Delete any trace-generated messages from the CBRroute project’s /src/data/ directory and /target/messages/ subdirectories in Project Explorer. Trace-generated messages begin with the ID- prefix. For example, Figure 8.1, “Trace-generated messages” shows six trace-generated messages:
Figure 8.1. Trace-generated messages
Select all trace-generated messages in batch, right-click to open the context menu, and select Delete.
Creating the src/test folder 复制链接链接已复制到粘贴板!
Before you create a JUnit test case for the CBRroute project, you must create a folder for it that is included in the build path:
-
In Project Explorer, right-click the
CBRrouteproject’s root to open the context menu, and then select menu:New[ > > Folder > ]. In the New Folder dialog, in the project tree pane, expand the
CBRroutenode and select thesrcfolder.Make sure
CBRroute/srcappears in the Enter or select the parent folder field.In Folder name, enter
/test/java:Click .
In Project Explorer, the new
src/test/javafolder appears under thesrc/main/resourcesfolder:Verify that the new
/src/test/javafolder is included in the build path.-
In Project Explorer, right-click the
/src/test/javafolder to open the context menu. Select Build Path to see the menu options:
The menu option Remove from Build Path verifies that the
/src/test/javafolder is currently included in the build path:
-
In Project Explorer, right-click the
Creating the JUnit test case 复制链接链接已复制到粘贴板!
To create a JUnit test case for the CBRroute project:
-
In Project Explorer, select
src/test/java. Right-click it to open the context menu, and then select menu:New[ > > Camel Test Case > ]:
-
In the Camel JUnit Test Case wizard, make sure the
Source folderfield containsCBRroute/src/test/java. To find the proper folder, click
.
-
In the Package field, enter
tutorial.cbr.route. This is the package that will include the new test case. Next to the Camel XML file under test field, click to open a file explorer configured to screen for XML files, and then select the
CBRrouteproject’sblueprint.xmlfile:Click .
NoteThe Name field defaults to BlueprintXmlTest.
- Click to open the Test Endpoints page.
By default, all endpoints are selected and will be included in the test case. Click .
NoteIf prompted, add JUnit to the build path.
The artifacts for the test are added to your project and appear in Project Explorer under src/test/java. The class implementing the test case opens in the tooling’s Java editor:
This generated JUnit test case is insufficient for the CBRroute project, and it will fail to run successfully. You need to modify it and the project’s pom.xml, as described in the section called “Modifying the BlueprintXmlTest file” and the section called “Modifying the pom.xml file”.
Modifying the BlueprintXmlTest file 复制链接链接已复制到粘贴板!
You must modify the BlueprintXmlTest.java file to:
- Import several classes that support required file functions
-
Create variables for holding the content of the various source
.xmlfiles -
Read the content of the source
.xmlfiles Define appropriate expectations
In Project Explorer, expand the
CBRrouteproject to expose theBlueprintXmlTest.javafile:-
Double-click
BlueprintXmlTest.javato open the file in the tooling’s Java editor. -
In the Java editor, click the expand button next to
import org.apache.camel.EndpointInject;to expand the list. Add the two lines shown below. Adding the first line will cause an error that will be resolved when you update the
pom.xmlfile as instructed in the next section.-
Scroll down to the lines that follow directly after
// Expected message bodies. Replace those lines —
protected Object[] expectedBodies={ …… expectedBody2</something>"};— with theprotected String body#;lines shown here:Scroll down to the line
public void testCamelRoute() throws Exception {, and insert directly after it the linesbody# = FileUtils.readFileToString(new File("src/data/message#.xml"), "UTF-8");shown below. These lines will indicate an error until you update thepom.xmlfile as instructed in the next section.-
Scroll down to the lines that follow directly after
// TODO Ensure expectations make sense for the route(s) we’re testing. Replace the block of code that begins with
outputEndpoint.expectedBodiesReceivedInAnyOrder(expectedBodies);and ends with …inputEndpoint.sendBody(expectedBody); }with the lines shown here:Leave the remaining code as is.
- Save the file.
Check that your updated
BlueprintXmlTest.javafile has the required modifications. It should look something like this:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Modifying the pom.xml file 复制链接链接已复制到粘贴板!
You need to add a dependency on the commons-io project to the CBRroute project’s pom.xml file:
-
In Project Explorer, double-click
pom.xml, located below thetargetfolder, to open the file in the tooling’s XML editor. -
Click the
pom.xmltab at the bottom of the page to open the file for editing. Add these lines to the end of the
<dependencies>section:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Save the file.
The contents of the entire
pom.xmlfile should look like this:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Running the JUnit test 复制链接链接已复制到粘贴板!
To run the test:
- Switch to JBoss perspective to free up more workspace.
-
Select the project root,
CBRroute, in the Project Explorer. - Open the context menu.
Select menu:Run As[ > JUnit Test].
NoteBy default, the JUnit view opens in the sidebar. (To provide a better view, drag it to the bottom, right panel that displays the Console, Servers, and Properties tabs.)
If the test runs successfully, you’ll see something like this:
NoteSometimes the test fails the first time JUnit is run on a project. Rerunning the test ususally results in a successful outcome.
When the test does fail, you’ll see something like this:
NoteJUnit will fail if your execution environment is not set to Java SE 8 or 7. The message bar at the top of the JUnit tab will display an error message indicating that it cannot find the correct SDK.
To resolve the issue, open the project’s context menu, and select menu:Run As[ > > Run Configurations > > JRE > ]. Click the button next to the Execution environment field to locate and select a Java SE 8 or 7 environment.
Examine the output and take action to resolve any test failures.
To see more of the errors displayed in the JUnit panel, click
on the panel’s menu bar to maximize the view.
Before you run the JUnit test case again, delete any JUnit-generated test messages from the CBRroute project’s
/src/datafolder in Project Explorer (see Figure 8.1, “Trace-generated messages”).
Further reading 复制链接链接已复制到粘贴板!
To learn more about JUnit testing see JUnit.