Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Add dependencies on Camel-Spring, and the Spring test JAR (which will automatically bring in JUnit 3.8.x) to your POM:
    Code Block
    xml
    xml
    <dependency>
        <artifactId>camel-spring</artifactId>
        <groupId>org.apache.camel</groupId>
        <version>1.4.0</version>
    </dependency>
    <dependency>
        <artifactId>spring-test</artifactId>
        <groupId>org.springframework</groupId>
        <version>2.5.5</version>
        <scope>test</scope>
    </dependency>
    
  2. Create a new unit test class in src/test/java/your-package-here, perhaps called XMLInputTest.java
  3. Make the test extend Spring's AbstractJUnit38SpringContextTests class.
  4. Create a Spring context configuration file in src/test/resources, perhaps called XMLInputTest-context.xml
  5. In the unit test class, use the class-level @ContextConfiguration annotation to indicate that a Spring context should be loaded
    • By default, this looks for a Context configuration file called TestClassName-context.xml in a subdirectory corresponding to the package of the test class. For instance, if your test class was org.apache.camel.tutorial.XMLInputTest, it would look for org/apache/camel/tutorial/XMLInputTest-context.xml
    • To override this default, use the locations attribute on the @ContextConfiguration annotation to provide specific context file locations (starting each path with a / if you don't want it to be relative to the package directory)
  6. Add a CamelContext instance variable to the test class, with the @Autowired annotation
  7. Put in an empty test method just for the moment
  8. Add the Spring <beans> element (including the Camel Namespace) with an empty <camelContext> element to the Spring context, like this:
    Code Block
    xml
    xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                                   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                               http://activemq.apache.org/camel/schema/spring
                                   http://activemq.apache.org/camel/schema/spring/camel-spring-1.3.0.xsd">
    
        <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
        </camelContext>
    </beans>
    

...

Test it by running mvn install and make sure there are no build errors. So far it doesn't test much; just that your project and test and source files are all organized correctly.

Flesh Out the Unit Test

So now we're going to write a Camel route that applies the XSLT to the sample Customer 1 input file, and makes sure that some XML output comes out:

  1. Save the input-customer1.xml file to src/test/resources
  2. Save your XSLT file (created in the previous step) to src/main/resources
  3. Write a Camel Route, either right in the Spring XML, or using the Java DSL (in another class under src/test/java somewhere). This route should:
    1. Start from the endpoint direct:start (which lets the test conveniently pass messages into the route)
    2. Call the endpoint xslt:YourXSLTFile.xsl to transform the message
    3. Send the result to the endpoint mock:finish (which lets the test verify the route output)
  4. Add a test method to the unit test class that:
    1. Get a reference to the Mock endpoint mock:finish using code like this:
      Code Block
      java
      java
      
      MockEndpoint finish = MockEndpoint.resolve(camelContext, "mock:finish");
      
    2. Set the expectedMessageCount on that endpoint to 1
    3. Get a reference to the Customer 1 input file, using code like this:
      Code Block
      java
      java
      
      InputStream in = XMLInputTest.class.getResourceAsStream("/input-partner1.xml");
      assertNotNull(in);
      
    4. Send that InputStream as a message to the direct:start endpoint, perhaps using code like this:
      Code Block
      java
      java
      
          sendMessage"direct:start", in);
      
          ...
      
          /**
           * Sends a message to the given endpoint URI with the body value and specified headers
           *
           * @param endpointUri the URI of the endpoint to send to
           * @param body        the body for the message
           */
          protected void sendMessage(String endpointUri, final Object body) {
              template.send(endpointUri, new Processor() {
                  public void process(Exchange exchange) {
                      Message in = exchange.getIn();
                      in.setBody(body);
                  }
              });
          }
      
    5. Ensure that the message made it through the route to the final endpoint, by testing all configured Mock endpoints like this:
      Code Block
      java
      java
      
      MockEndpoint.assertIsSatisfied(camelContext);
      
    6. If you like, inspect the final message body using some code like finish.getExchanges().get(0).getIn().getBody().
      • If you do this, you'll need to know what format that body is – String, byte array, InputStream, etc.
  5. Run your test with mvn install and make sure the build completes successfully.
Warning
titleTest Failures and Camel Output

If your test hangs for a while and then fails saying that the Mock Endpoint was not satisfied, that probably means that there was an error delivering the message. Perhaps the XSLT endpoint couldn't find the XSLT template? If you're mystified by the lack of log output, try adding a simple log4j.properties file to src/main/resources or src/test/resources to ensure that any Log4J output goes to the console.

Tip
titleTest Base Class

Once your test class is working, you might want to extract things like the @Autowired CamelContext and the sendMessage method to a custom base class that you extend with your other tests.

Initial Work on Customer 2 Input (CSV over HTTP)

...