Mocking Tests

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Mocking Tests

jpcook01
This post has NOT been accepted by the mailing list yet.
Hi,

I have a fairly simple app which is pulling messages off a jms and transforming them with xslt before writing a file to disk.

I have lots of unit tests to test that the content in the file is correct but really these are integration tests as they are waiting for the file to be written to disk before testing the contents. So as more and more tests are added they take a long time to run.

So I was wondering if there was a clever way to mock out the file:.. call so the contents of the message is tested without having to write it to disk. I don't want to have to add .to("mock:somewhere") to all my routes and I don't want to have to replicate the code in my routebuilders in the tests themselves.

I've had a look at https://cwiki.apache.org/CAMEL/test.html and https://cwiki.apache.org/CAMEL/spring-testing.html and had a search at old posts but it still isn't clear to me.

Thanks
Jon
Reply | Threaded
Open this post in threaded view
|

Re: Mocking Tests

Ashwin Karpe
This post has NOT been accepted by the mailing list yet.
Hi,

The example below sets up a test route that looks for files in a directory and sends it to mock endpoint to test if a file has arrived in the directory when a message is sent on a JMS route.

Check out my JUnit test...

Hope this helps.

Cheers,

Ashwin...

================================================
public class FooBarRouteTest extends CamelTestSupport {
    private static final transient Log LOG = LogFactory.getLog(FooBarRouteTest.class);
    protected CamelContext camelContext;
    protected ProducerTemplate producer;
    private static ClassPathXmlApplicationContext ctx;    
    @EndpointInject(uri="mock:done")
    private MockEndpoint mockResult;

    @Before
    public void setUp() throws Exception {
    ctx =
    new ClassPathXmlApplicationContext("/META-INF/spring/spring.xml");
    camelContext = (CamelContext) serverContext.getBean("camelContext");
    }

    @After
    public void tearDownServer() {
        if (ctx != null) {
            ctx.stop();
        }
       
    }
       
   @Test
   public void testFooBarRoute() throws Exception {
        producer = camelContext.createProducerTemplate();
        mockResult = camelContext.getEndpoint("mock:done", MockEndpoint.class);
        mockResult.expectedMessageCount(1);

        String payload="Test payload";

        producer.sendBody("activemq:queue:foo", payload);
        mockResult.assertIsSatisfied();
                           
  }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            public void configure() {
                from("file:mydirectory").to("mock:done");
            }
        };
    }
}
---------------------------------------------------------
Ashwin Karpe
Apache Camel Committer & Sr Principal Consultant
Red Hat
http://www.redhat.com

Blog:http://opensourceknowledge.blogspot.com
---------------------------------------------------------