You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Costash <co...@yahoo.com> on 2013/07/29 18:15:33 UTC
Camel testing
Hi,
I've just started to use camel, and I'm trying to understand and create some
unit tests, but i don t understand how Mok is working or how unit tests
should look like (i've read the tutorials and documentation about camel
test).
What do i mean by not understanging is:
i have a defined route:
=========================
public class MyRouteBuilder extends RouteBuilder{
.....
@Override
public void configure() throws Exception {
from("jms:queue:inputQueueName").process(new Processor()
{.........}).to("jms:queue:outputputQueueName");
======================
in all the examples found , a new Class extends CamelTestSupport {
and also creating the rounting inside, so it can be moked:
===============
@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result");
}
};
}
================
so the route is defined exactly here, so it can be moked and get the
endpoints.
BUT, what about to test an allready created route?
I can't say .to("mock:result");
i suposed it should be:
=========
@Override
protected RouteBuilder createRouteBuilder() {
return new MyRouteBuilder();
}
==========
So my questions are:
1. how can i test a route? (i can't define the rules of routing inside the
test class by overwriting the createRouteBuilder() , because i already have
the route implementation => duplicate code)
2. Should my MyRouteBuilder have specific methods implemented so i can mok
it after in the test class?
3. is it there another way to test the output messages of a router besides
Mok?
- I did it in one way, but i think is not the "correct" one:
- In a Test Class, I've created a Connection factory to a jms
queue and creating a context
- I've added the desired route to be tested into the context
( context.addRoute(new MyRoutebuilder()))
- Started the context (so my router is running in
"backgroud" let s say)
- i've put a message into the input queue of the router with
another test Router (from file to queue)
- i've taken the message from output queue of the router with
a test router (From message to file)
- compare the file with an expected one.
But this is bad as concept for unit tests (let s say Junits), i think.
4. Do you a link, or something to a generic test example of a camel route?
PS: Sorry if the are stupid questions
Thanks,
Vlad
--
View this message in context: http://camel.465427.n5.nabble.com/Camel-testing-tp5736444.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Camel testing
Posted by Costash <co...@yahoo.com>.
Thanks a lot!
Vlad
--
View this message in context: http://camel.465427.n5.nabble.com/Camel-testing-tp5736444p5736579.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Camel testing
Posted by Willem jiang <wi...@gmail.com>.
Hi
You can use replace the jms component with seda component just like this
camelContext.addComponent("jms", sedaComponent );
The you don't need to create a new jms connection per test.
--
Willem Jiang
Red Hat, Inc.
FuseSource is now part of Red Hat
Web: http://www.fusesource.com | http://www.redhat.com
Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/) (English)
http://jnn.iteye.com (http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang
Weibo: 姜宁willem
On Tuesday, July 30, 2013 at 6:17 PM, Costash wrote:
> Hello,
>
> Thanks a lot. I did it. (see below) Now i have another question :)
> My Endpoints are connecting to the real queue, and send the message there.
> What if i don't want a real connection to a queue,* can the endpoint be
> somehow virtualize for unit tests*?
> Ofcourse i can use active MQ instead of real MQ, but we will have same stuff
> = connection itself.
>
>
> I mean, to take the endoints from context, somehow vritualize not to have a
> real conection(just spmething called Endpoint), and send a message to that
> endpoint.
>
> i think you got the idea: to use Endpoints in general instead of real one
> (with connection)
>
>
> Thanks,
> Vlad
>
>
> package main;
>
> import org.apache.camel.CamelContext;
> import org.apache.camel.Produce;
> import org.apache.camel.ProducerTemplate;
> import org.apache.camel.builder.AdviceWithRouteBuilder;
> import org.apache.camel.builder.RouteBuilder;
> import org.apache.camel.component.jms.JmsComponent;
> import org.apache.camel.component.mock.MockEndpoint;
> import org.apache.camel.impl.DefaultCamelContext;
> import org.apache.camel.test.junit4.CamelTestSupport;
> import org.apache.log4j.PropertyConfigurator;
> import org.junit.BeforeClass;
> import org.junit.Test;
>
> import com.ibm.mq.jms.MQQueueConnectionFactory;
> import com.ibm.msg.client.wmq.WMQConstants;
>
>
>
>
>
>
> /**
> * @author vlad.costache
> *
> *
> */
> public class TestWithMokk extends CamelTestSupport {
>
>
> @Produce(uri = "jms:queue:msb.2761100008.mpos")
> protected ProducerTemplate template;
>
> @BeforeClass
> public static void setConn(){
> PropertyConfigurator.configure("./log4j.properties");
>
>
>
>
> }
>
> /* (non-Javadoc)
> * @see org.apache.camel.test.junit4.CamelTestSupport#createCamelContext()
> */
> @Override
> protected CamelContext createCamelContext() throws Exception {
> MQQueueConnectionFactory factory = new MQQueueConnectionFactory();
> factory.setQueueManager("MQSD.ESSWS008");
> factory.setChannel("MSB.S0.SVRCONN");
> factory.setHostName("10.23.8.218");
> factory.setPort(1414);
> factory.setTransportType(WMQConstants. WMQ_CM_CLIENT);
>
>
> JmsComponent jmsComponent =
> JmsComponent.jmsComponentAutoAcknowledge(factory);
>
> CamelContext camelContext = new DefaultCamelContext();
> camelContext.addComponent("jms", jmsComponent );
> return camelContext;
>
> }
>
> @Test
> public void testSendMatchingMessage() throws Exception {
>
>
>
>
> context.getRouteDefinitions().get(0).adviceWith(context, new
> AdviceWithRouteBuilder() {
> @Override
> public void configure() throws Exception {
> // mock all endpoints
> mockEndpoints();
> }
> });
>
>
> String sendbody = "<send/>";
> String expectedBody = "<matched/>";
>
> MockEndpoint mockEndpoint =
> getMockEndpoint("mock:jms:queue:msb.2761100008.invoice");
> mockEndpoint.expectedBodiesReceived(expectedBody);
>
> template.sendBodyAndHeader(expectedBody, "foo", "bar");
> mockEndpoint.assertIsSatisfied();
> }
>
>
>
>
> @Override
> protected RouteBuilder createRouteBuilder() throws Exception {
> return new MyRouteBuilder();
>
> }
>
> }
>
>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Camel-testing-tp5736444p5736480.html
> Sent from the Camel - Users mailing list archive at Nabble.com (http://Nabble.com).
Re: AW: Camel testing
Posted by Costash <co...@yahoo.com>.
Hello,
Thanks a lot. I did it. (see below) Now i have another question :)
My Endpoints are connecting to the real queue, and send the message there.
What if i don't want a real connection to a queue,* can the endpoint be
somehow virtualize for unit tests*?
Ofcourse i can use active MQ instead of real MQ, but we will have same stuff
= connection itself.
I mean, to take the endoints from context, somehow vritualize not to have a
real conection(just spmething called Endpoint), and send a message to that
endpoint.
i think you got the idea: to use Endpoints in general instead of real one
(with connection)
Thanks,
Vlad
package main;
import org.apache.camel.CamelContext;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.apache.log4j.PropertyConfigurator;
import org.junit.BeforeClass;
import org.junit.Test;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.msg.client.wmq.WMQConstants;
/**
* @author vlad.costache
*
*
*/
public class TestWithMokk extends CamelTestSupport {
@Produce(uri = "jms:queue:msb.2761100008.mpos")
protected ProducerTemplate template;
@BeforeClass
public static void setConn(){
PropertyConfigurator.configure("./log4j.properties");
}
/* (non-Javadoc)
* @see org.apache.camel.test.junit4.CamelTestSupport#createCamelContext()
*/
@Override
protected CamelContext createCamelContext() throws Exception {
MQQueueConnectionFactory factory = new MQQueueConnectionFactory();
factory.setQueueManager("MQSD.ESSWS008");
factory.setChannel("MSB.S0.SVRCONN");
factory.setHostName("10.23.8.218");
factory.setPort(1414);
factory.setTransportType(WMQConstants. WMQ_CM_CLIENT);
JmsComponent jmsComponent =
JmsComponent.jmsComponentAutoAcknowledge(factory);
CamelContext camelContext = new DefaultCamelContext();
camelContext.addComponent("jms", jmsComponent );
return camelContext;
}
@Test
public void testSendMatchingMessage() throws Exception {
context.getRouteDefinitions().get(0).adviceWith(context, new
AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
// mock all endpoints
mockEndpoints();
}
});
String sendbody = "<send/>";
String expectedBody = "<matched/>";
MockEndpoint mockEndpoint =
getMockEndpoint("mock:jms:queue:msb.2761100008.invoice");
mockEndpoint.expectedBodiesReceived(expectedBody);
template.sendBodyAndHeader(expectedBody, "foo", "bar");
mockEndpoint.assertIsSatisfied();
}
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new MyRouteBuilder();
}
}
--
View this message in context: http://camel.465427.n5.nabble.com/Camel-testing-tp5736444p5736480.html
Sent from the Camel - Users mailing list archive at Nabble.com.
AW: Camel testing
Posted by "Jan Matèrne (jhm)" <ap...@materne.de>.
And the configure()-methode in the test class just returns a RouteBuilder.
It doesn't have to return a new created one ...
public class MyTest extends CamelTestSupport {
@Override
protected RouteBuilder createRouteBuilder() {
RouteBuilder rb = new MyRouteBuilder();
// adviceWith ...
// "inject" new endpoint uris
rb.setFromUri("direct:in");
rb.setToDatabase("mock:db");
return rb;
}
}
Jan
> -----Ursprüngliche Nachricht-----
> Von: Christian Posta [mailto:christian.posta@gmail.com]
> Gesendet: Dienstag, 30. Juli 2013 05:10
> An: users@camel.apache.org
> Betreff: Re: Camel testing
>
> So keep in mind that the "endpoints" to a route are just
> java.lang.String... So you can do:
>
> String startRoute = "jms:incoming";
> String endRoute = "jms:outgoing";
>
> from(startRoute)......to(endRoute)
>
> And then have that string injectable, so you can do
> setEndRoute("mock:endpointName")...
>
> But even if you don't want to do that, you can check out "adviceWith"
> which wraps the existing endpoint with interesting behavior, including
> mocking it out, and/or adding interceptors.
>
> You should check out the doco at http://camel.apache.org/mock.html You
> can also check out this refcard that covers essential camel components,
> including the Mock component:
> http://refcardz.dzone.com/refcardz/essential-camel-components
> And lastly, but certainly not least.. the camel source code is packed
> with unit tests. There are no better examples than the unit tests from
> the camel code base:
> http://camel.apache.org/how-can-i-get-the-source-code.html
>
> Cheers,
> Christian
>
>
> On Mon, Jul 29, 2013 at 9:15 AM, Costash <co...@yahoo.com>
> wrote:
>
> > Hi,
> >
> > I've just started to use camel, and I'm trying to understand and
> > create some unit tests, but i don t understand how Mok is working or
> > how unit tests should look like (i've read the tutorials and
> > documentation about camel test).
> > What do i mean by not understanging is:
> >
> > i have a defined route:
> > =========================
> > public class MyRouteBuilder extends RouteBuilder{ .....
> > @Override
> > public void configure() throws Exception {
> > from("jms:queue:inputQueueName").process(new Processor()
> > {.........}).to("jms:queue:outputputQueueName");
> > ======================
> >
> > in all the examples found , a new Class extends CamelTestSupport {
> >
> > and also creating the rounting inside, so it can be moked:
> > ===============
> > @Override
> > protected RouteBuilder createRouteBuilder() {
> > return new RouteBuilder() {
> > public void configure() {
> >
> >
> >
> from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:re
> sult");
> > }
> > };
> > }
> > ================
> > so the route is defined exactly here, so it can be moked and get the
> > endpoints.
> >
> > BUT, what about to test an allready created route?
> > I can't say .to("mock:result");
> >
> > i suposed it should be:
> >
> > =========
> > @Override
> > protected RouteBuilder createRouteBuilder() { return new
> > MyRouteBuilder(); } ==========
> >
> >
> > So my questions are:
> > 1. how can i test a route? (i can't define the rules of routing
> inside
> > the test class by overwriting the createRouteBuilder() , because i
> > already have the route implementation => duplicate code) 2. Should my
> > MyRouteBuilder have specific methods implemented so i can mok it
> after
> > in the test class?
> > 3. is it there another way to test the output messages of a router
> > besides Mok?
> > - I did it in one way, but i think is not the "correct" one:
> > - In a Test Class, I've created a Connection factory
> to
> > a jms queue and creating a context
> > - I've added the desired route to be tested into the
> > context ( context.addRoute(new MyRoutebuilder()))
> > - Started the context (so my router is running in
> > "backgroud" let s say)
> > - i've put a message into the input queue of the
> router
> > with another test Router (from file to queue)
> > - i've taken the message from output queue of the
> > router with a test router (From message to file)
> > - compare the file with an expected one.
> >
> > But this is bad as concept for unit tests (let s say Junits), i
> think.
> >
> > 4. Do you a link, or something to a generic test example of a camel
> route?
> >
> > PS: Sorry if the are stupid questions
> >
> > Thanks,
> > Vlad
> >
> >
> >
> > --
> > View this message in context:
> > http://camel.465427.n5.nabble.com/Camel-testing-tp5736444.html
> > Sent from the Camel - Users mailing list archive at Nabble.com.
> >
>
>
>
> --
> *Christian Posta*
> http://www.christianposta.com/blog
> twitter: @christianposta
Re: Camel testing
Posted by Christian Posta <ch...@gmail.com>.
So keep in mind that the "endpoints" to a route are just
java.lang.String... So you can do:
String startRoute = "jms:incoming";
String endRoute = "jms:outgoing";
from(startRoute)......to(endRoute)
And then have that string injectable, so you can do
setEndRoute("mock:endpointName")...
But even if you don't want to do that, you can check out "adviceWith" which
wraps the existing endpoint with interesting behavior, including mocking it
out, and/or adding interceptors.
You should check out the doco at http://camel.apache.org/mock.html
You can also check out this refcard that covers essential camel components,
including the Mock component:
http://refcardz.dzone.com/refcardz/essential-camel-components
And lastly, but certainly not least.. the camel source code is packed with
unit tests. There are no better examples than the unit tests from the camel
code base:
http://camel.apache.org/how-can-i-get-the-source-code.html
Cheers,
Christian
On Mon, Jul 29, 2013 at 9:15 AM, Costash <co...@yahoo.com> wrote:
> Hi,
>
> I've just started to use camel, and I'm trying to understand and create
> some
> unit tests, but i don t understand how Mok is working or how unit tests
> should look like (i've read the tutorials and documentation about camel
> test).
> What do i mean by not understanging is:
>
> i have a defined route:
> =========================
> public class MyRouteBuilder extends RouteBuilder{
> .....
> @Override
> public void configure() throws Exception {
> from("jms:queue:inputQueueName").process(new Processor()
> {.........}).to("jms:queue:outputputQueueName");
> ======================
>
> in all the examples found , a new Class extends CamelTestSupport {
>
> and also creating the rounting inside, so it can be moked:
> ===============
> @Override
> protected RouteBuilder createRouteBuilder() {
> return new RouteBuilder() {
> public void configure() {
>
>
> from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result");
> }
> };
> }
> ================
> so the route is defined exactly here, so it can be moked and get the
> endpoints.
>
> BUT, what about to test an allready created route?
> I can't say .to("mock:result");
>
> i suposed it should be:
>
> =========
> @Override
> protected RouteBuilder createRouteBuilder() {
> return new MyRouteBuilder();
> }
> ==========
>
>
> So my questions are:
> 1. how can i test a route? (i can't define the rules of routing inside the
> test class by overwriting the createRouteBuilder() , because i already have
> the route implementation => duplicate code)
> 2. Should my MyRouteBuilder have specific methods implemented so i can mok
> it after in the test class?
> 3. is it there another way to test the output messages of a router besides
> Mok?
> - I did it in one way, but i think is not the "correct" one:
> - In a Test Class, I've created a Connection factory to a
> jms
> queue and creating a context
> - I've added the desired route to be tested into the context
> ( context.addRoute(new MyRoutebuilder()))
> - Started the context (so my router is running in
> "backgroud" let s say)
> - i've put a message into the input queue of the router with
> another test Router (from file to queue)
> - i've taken the message from output queue of the router
> with
> a test router (From message to file)
> - compare the file with an expected one.
>
> But this is bad as concept for unit tests (let s say Junits), i think.
>
> 4. Do you a link, or something to a generic test example of a camel route?
>
> PS: Sorry if the are stupid questions
>
> Thanks,
> Vlad
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Camel-testing-tp5736444.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
--
*Christian Posta*
http://www.christianposta.com/blog
twitter: @christianposta