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.Questo contenuto non è disponibile nella lingua selezionata.
Chapter 8. To Test a Route with JUnit
Abstract
Overview Copia collegamentoCollegamento copiato negli appunti!
Goals Copia collegamentoCollegamento copiato negli appunti!
- delete the existing JUnit test case
- generate a new JUnit test case for the CBRroute project
- modify the newly generated JUnit test case
- modify the CBRroute project's
pom.xml
file - run the CBRroute with the New JUnit test case
- observe the output
Prerequisites Copia collegamentoCollegamento copiato negli appunti!
camelContext6.xml
file to work through this tutorial (for details, see Chapter 1, Using the Fuse Tooling Resource Files).
/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
Deleting the existing test case Copia collegamentoCollegamento copiato negli appunti!
- In Project Explorer, expand
src/test/java
to expose theCamelContextXmlTest
file, as shown in Figure 8.2.Figure 8.2. CamelContextXmlTest.java file location
- Right-click it to open the context menu, and select.A dialog box opens asking you to confirm deletion of the test case file.
- Click.
- Verify that the existing test case has been deleted by right-clicking CBRroute and selecting .The
src/test/java/tutorial.cbr.route
directory should be empty.
Creating the new test case Copia collegamentoCollegamento copiato negli appunti!
- In Project Explorer, select
src/test/java
. - Right-click it to open the context menu, and then select
to open the New Camel JUnit Test Case wizard, as shown in Figure 8.3. Figure 8.3. New Camel JUnit Test Case wizard
- Make sure the Source folder field contains
CBRroute/src/test/java
.NoteIf needed, you can clickto find the proper folder.
- The Package field defaults to
tutorial.cbr.route
. To include the test case in a different package, enter the name of the package. - In the Camel XML file under test field, enter
src/main/resources/OSGI-INF/blueprint/camelContext.xml
, or useto open a file explorer, configured to screen for XML files, to locate the file.
NoteThe Name field defaults to CamelContextXmlTest for the name of the test file. - Click Test Endpoints page, shown in Figure 8.4.to open the
Figure 8.4. Test Endpoints page
- By default, all endpoints are selected and will be included in the test case. Leave them selected for this tutorial.NoteYou can select or deselect all endpoints by clicking the Select All or Deselect All button, or you can select and deselect individual endpoints by clicking the check box next to each.
- Click.NoteIf prompted, add JUnit to the build path.
src/test/java
. The class implementing the test case opens in the tooling's Java editor:
pom.xml
, as described in the section called “Modifying the CamelContextXmlTest file ” and the section called “Modifying the pom.xml file”.
Modifying the CamelContextXmlTest file Copia collegamentoCollegamento copiato negli appunti!
CamelContextXmlTest.java
file to:
- import several classes that support required file functions
- create variables for holding the content of the various source
.xml
files - read the content of the source
.xml
files - define appropriate expectations
- In Project Explorer, expand the CBRroute project to expose the
CamelContextXmlTest
item. - Double-click
CamelContextXmlTest
to open the file in the route editor. - In the route editor, click the expand button next to
import org.apache.camel.EndpointInject;
to expand the list. - Add the three lines shown here:
- 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"));
shown here: - 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
output4Endpoint.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
CamelContextXmlTest.java
file has the required modifications. It should look something like this:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Modifying the pom.xml file Copia collegamentoCollegamento copiato negli appunti!
pom.xml
file.
- In Project Explorer, double-click
pom.xml
, located below thetarget
folder, to open the file in the route editor. - Click the pom.xml tab 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.xml
file should look like this:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Running the JUnit test Copia collegamentoCollegamento copiato negli appunti!
- Switch to JBoss perspective to free up more workspace.
- Select the project root,
CBRroute
, in the Project Explorer. - Open the context menu.
- Select
. NoteBy default, 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:
Figure 8.5. Successful JUnit run
If the test fails, you'll see something like this:Figure 8.6. Failed JUnit run
NoteJUnit will fail if your execution environment is not set to Java SE 7 or above. 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. Click the button next to the Execution environment: field to locate and select a Java SE 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. You can also check the surefire reports in the
surefire-reports
tab in Project Explorer, as shown in Figure 8.7.Figure 8.7. JUnit surefire reports
Before you run the JUnit test case again, delete any JUnit-generated test messages from the CBRroute project's/src/data
folder in Project Explorer (see Figure 8.1, “Trace-generated messages”).
Further reading Copia collegamentoCollegamento copiato negli appunti!
- see JUnit