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
Abstract
					This tutorial walks you through the process of using the New Camel Test Case wizard to create a test case for your route and using it test the route.
				
Overview
复制链接链接已复制到粘贴板!
			The New Camel JUnit Test Case wizard generates a boilerplate JUnit test case. This means that when you create or modify a route (for example, adding more processors to it), you'll need to modify the generated test case to add expectations and assertions specific to the new route you've created, so the test is valid for the new route.
		
Goals
复制链接链接已复制到粘贴板!
			In this tutorial you will:
		
- 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'spom.xmlfile
- run the CBRroute with the New JUnit test case
- observe the output
Prerequisites
复制链接链接已复制到粘贴板!
			To complete this tutorial you will need the CBRroute project you used in Chapter 7, To Trace a Message Through a Route
		
Note
				If you skipped any tutorial after Chapter 2, To Create a New Route, you can use the prefabricated 
camelContext6.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 .
		
Deleting the existing test case
复制链接链接已复制到粘贴板!
			To delete the existing Apache Camel test case:
		
- In Project Explorer, expandsrc/test/javato expose theCamelContextXmlTestfile, 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 .Thesrc/test/java/tutorial.cbr.routedirectory should be empty.
Creating the new test case
复制链接链接已复制到粘贴板!
			To create a new Apache Camel test case:
		
- In Project Explorer, selectsrc/test/java.
- Right-click it to open the context menu, and then selectto 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 containsCBRroute/src/test/java.NoteIf needed, you can click to find the proper folder. to find the proper folder.
- The Package field defaults totutorial.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, entersrc/main/resources/OSGI-INF/blueprint/camelContext.xml, or use to 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. to 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 to open the Test Endpoints page, shown in Figure 8.4.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.
			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 CamelContextXmlTest file ” and the section called “Modifying the pom.xml file”.
		Modifying the CamelContextXmlTest file 
复制链接链接已复制到粘贴板!
			You need to modify the 
		
CamelContextXmlTest.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 CBRroute project to expose theCamelContextXmlTestitem.
- Double-clickCamelContextXmlTestto open the file in the route editor.
- In the route editor, click the expand button next toimport 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 linepublic 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 withoutput4Endpoint.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 updatedCamelContextXmlTest.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-clickpom.xml, located below thetargetfolder, 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 entirepom.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. 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 on the panel's menu bar to maximize the view. You can also check the surefire reports in thesurefire-reportstab 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/datafolder in Project Explorer (see Figure 8.1, “Trace-generated messages”).
Further reading
复制链接链接已复制到粘贴板!
			To learn more about JUnit testing:
		
- see JUnit
 
     
     
     
     
     
     
     
     
     
     
    