A common way of testing routes is to use JUnit. The design time tooling includes a wizard that simplifies creating a JUnit test case for your routes. The wizard uses the endpoints you specify to generate the starting point code and configuration for the test.
![]() | Note |
|---|---|
After you create the boilerplate JUnit test case, you need to modify it to add expectations and assertions specific to the route that you've created or modified, so the test is valid for the route. |
Before you create a new JUnit test case, you need to perform a preliminary task:
If you are replacing an existing JUnit test case, you need to delete it before you create a new one. See Deleting and existing JUnit test case.
If you are creating a new JUnit test case in a project that hasn't one, you need to first
create the project_root/src/test/java
folder for the test case, and add the folder to the build path. See Creating and adding the src/test/java folder to the build path.
In Project Explorer, expand the project's root node to
expose the <root_project>/src/test/java
folder.
Locate the JUnit test case file in the /src/test/java
folder.
Depending on which DSL the project is based on, the JUnit test case file is
named BlueprintXmlTest.java or
CamelContextXmlTest.java.
Right-click the JUnit test case .java file to open the context menu, and then
select .
The JUnit test case .java file disappears from Project
Explorer.
You can now create a new JUnit test case.
In Project Explorer, right-click the project's root to open the context menu.
Select > to open the Create a new folder resource wizard.
In the wizard's project tree pane, expand the project's root node and select
the src folder.
Make sure <project_root>/src appears in the Enter or
select the parent folder
field.
In Folder name, enter /test/java.
This folder will store the new JUnit test case you create.
Click .
In Project Explorer, the new /test/java folder appears
under the src folder:
In Project Explorer, right-click the
/src/test/java folder to open the context menu.
Select > to add the /src/test/java folder to the
build path.
You can now create a new JUnit test case.
To create a new JUnit test case for your route:
In Project Explorer, select the routing context
.xml file
in your project.
Right-click it to open the context menu, and then select > to open the New Camel JUnit Test Case wizard, as shown in Figure 5, “New Camel JUnit Test Case wizard”.
Alternatively, you can open the wizard by selecting > > > > from the menu bar.
In Source folder, accept the default location of the source code for the test case, or enter another location.
You can click
to search for a location.
In Package, accept the default package name for the generated test code, or enter another package name.
You can click
to search for a package.
In Camel XML file under test, accept the default pathname of the routing context file that contains the route you want to test, or enter another pathname.
You can click
to search for a context file.
In Name, accept the default name for the generated test class, or enter another name.
Select the method stubs you want to include in the generated code.
If you want to include the default generated comments in the generated code, check the Generate comments box.
Click
to open the Test Endpoints page. For
example, Figure 6, “New Camel JUnit Test Case page” shows a route's input and output file
endpoints selected.
Under Available endpoints, select the endpoints you want to test. Click the checkbox next to any selected endpoint to deselect it.
Click
.
![]() | Note |
|---|---|
If 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 Java editor.
| Red Hat JBoss Fuse Tooling Tutorials, To Test a Route with JUnit |