On 26/11/2007, jsbournival <[EMAIL PROTECTED]> wrote:
>
> Hello,
>
> I'm a little stuck.  I want to test a Camel app using TestNG.  But I can't
> figure out how to structure my stuff:
>
> -  First, I want to start the CamelContext via the Spring Context I have in
> my webapp (/WEB-INF/context.xml)

So thats just normal Spring stuff right - add the <camelContext> in
your Spring XML and boot up a spring ApplicationContext as normal.
http://activemq.apache.org/camel/spring.html

If you want to access the CamelContext you can specify an id on that
element then look up the CamelContext by id in the ApplicationContext.
Or you could write a bean which implements CamelContextAware to get
the context injected into your bean.



> -  Then I want to be able to fire messages to various endpoints ... but I
> can't find doc on this.

Our bad for the lack of docs! Noodling some of the existing test cases
and examples in Camel might be useful (though we really should
document more!).

You can use the file endpoint to fire in a set of defined messages to
some endpoint. e.g. the Spring Example does this

        from("file:src/data?noop=true").
                to("jms:test.MyQueue");

The noop flag means not to delete/move the files after they have been
sent. For more info see
http://activemq.apache.org/camel/spring-example.html
http://activemq.apache.org/camel/file.html

If you want to programatically send messages you can write a bean &
configure it in your spring.xml and use the @EndpointInject
annotation...

http://activemq.apache.org/camel/bean-integration.html

e.g.

public class Foo {
  @EndpointInject(uri="activemq:foo.bar")
  ProducerTemplate producer;

  public void doSomething() {
    if (whatever) {
      producer.sendBody("<hello>world!</hello>");
    }
  }
}

Or you could just create a CamelTemplate and use it to send messages
however you wish.


> Testing is a priority on our future project, so this one is important and I
> want to set it up right.

Definitely - testing is vital. As Hadrian mentioned, you wanna look at
Mock endpoints...
http://activemq.apache.org/camel/mock.html

which provides an elegant solution to testing.

You could for example set expectations on some mock endpoints, fire
your messages into your endpoints and then assert things work.

To make this all a bit more obvious, I've created a little test case
showing how to use Spring and Mock Endpoints.

First here's the spring.xml
https://svn.apache.org/repos/asf/activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/spring.xml

which instantiates a bean which sets a bunch of expectations before
the CamelContext starts up; it then has an assert method to verify
whatever expectations were satisfied...
https://svn.apache.org/repos/asf/activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/mock/MyAssertions.java

Notice the use of @EndpointInject to inject the mock endpoints; then
the afterPropertiesSet() to configure the expectations, then finally
the assertEndpointsValid() method will perform whatever assertions
required.

Finally the actual JUnit test case (which could be easily ported to
TestNG if you want) just boots up a Spring ApplicationContext then
looks up the MyAssertions bean and performs the assertions.

https://svn.apache.org/repos/asf/activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/mock/BeanMockTest.java

-- 
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://open.iona.com

Reply via email to