You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Henri Tremblay <he...@gmail.com> on 2012/11/04 10:09:48 UTC

Order of unit testing

Hi,

I've started to play with Camel and loving it. However, I'm still having
problems with the testing.

I want to do an integration test of a complete message processing. Here's
the flow, please focus on the solution, not on the fact that the
architecture is a nonsense, that part isn't really my fault (but I can use
any useful ideas)

Here it goes:

   1. Message received from JMS
   2. Stored in DB
   3. Read from DB with a consumer
   4. Sent to a direct route
   5. Status changed
   6. Sent to another system in REST
   7. The other system will change the status in DB and create new DB
   entries
   8. Read from DB with another consumer the new DB entries
   9. Sent to the same direct route
   10. Sent them to the same system in REST

My questions are

   - I want to check the message when entering in both direct route. How
   calls like expectedMessageCount are working? Is it ordered? So I should put
   an expectedMessageCount(1) for the first call and another for the second a
   bit below in my test
   - I want to be able to check the status changed at point 5 before it is
   changed again at step 7. How can I do that?
   - What's the ordering of things like
   expectedMessageCount, expectedBodyReceived? When they are specified in a
   row, they always apply to the same message? Is message(0) working the same
   way?

If it can help understanding the questions, I'm an EasyMock dude. So I'm
trying to map the Camel expectation to their equivalent EasyMock behavior.

Any help?

Thanks,
Henri

Re: Order of unit testing

Posted by Henri Tremblay <he...@gmail.com>.
I had the time to build a little test case and so I'll answer (a bit sadly)
my own questions.

*1 & 2: *Nothing is ordered. It just wait until everything is done. We can
call that an unordered mock
*3: *Yes. Every "expect" are about all messages. So if you have two
messages, you will need to expectCount(3) and expected 2 headers. It's like
having a bunch of asserts.
*4:* If it called afterwards. In the assertMockEndpointsSatisfied
*5: *My fault. I should have read more carefully the advicewith
page.  weaveAddAfter is perfect.

------------------------------------------------------------------------------------------------------
public class HenriRouteBuilderTest extends CamelTestSupport {

  @Override
  public String isMockEndpoints() {
    return "*";
  }

  @Test
  public void testToconfigure() throws Exception {

    getMockEndpoint("mock:result").expectedMessageCount(2);
    getMockEndpoint("mock:direct:start1").expectedMessageCount(1);
    getMockEndpoint("mock:direct:start2").expectedMessageCount(1);
    getMockEndpoint("mock:result").expectedBodiesReceived("Henri1",
"Henri2"); // can't specify only one body

    final AtomicBoolean b = new AtomicBoolean();


getMockEndpoint("mock:direct:start2").message(0).body(Message.class).in(new
Predicate() {

      @Override
      public boolean matches(Exchange exchange) {
        b.set(true);
        return exchange.getIn().getBody().equals("Henri2");
      }
    });

    template.sendBody("direct:start1", "Henri1");
    template.sendBody("direct:start2", "Henri2");

    assertFalse(b.get());

    assertMockEndpointsSatisfied();

    assertTrue(b.get());
  }

  @Override
  protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

      @Override
      public void configure() {
        from("direct:start1").to("mock:result");
        from("direct:start2").to("mock:result");
      }
    };

  }

}



On 5 November 2012 14:06, Henri Tremblay <he...@gmail.com> wrote:

> I've already read these two links and managed to do nice unit tests for
> simple routes. But I still don't have answers to my questions. I'll check
> the webinar. Meanwhile, I'll try to ask simpler questions.
>
> I'm working with
>   @Override
>   public String isMockEndpoints() {
>     return "*";
>   }
> and a test always ends with assertMockEndpointsSatisfied();
>
> *1-*
> MockEndpoint jdbc = getMockEndpoint(...)
> MockEndpoint seda = getMockEndpoint(...)
> jdbc.expectedMessageCount(1);
> seda.expectedMessageCount(1);
>
> My understanding is that assertMockEndpointsSatisfied will wait for the
> jdbc and then the seda message one after the other. Am I right?
>
> *2-*
> MockEndpoint jdbc = getMockEndpoint(...)
> MockEndpoint seda = getMockEndpoint(...)
> jdbc.expectedMessageCount(1);
> seda.expectedMessageCount(1);
> jdbc.expectedMessageCount(1);
>
> Same here, waiting for jdbc, then seda, then jdbc. If that right?
>
> *3-*
> MockEndpoint jdbc = getMockEndpoint(...)
> jdbc..expectedMessageCount(1);
> jdbc..expectedHeaderReceived("header", "value");
>
> Only one message will be received and its header will be checked.
>
> *4-*
> MockEndpoint jdbc = getMockEndpoint(...)
> jdbc.expectedMessageCount(1);
> jdbc.message(0).body(Message.class).in(new Predicate() {
>     public boolean matches(Exchange exchange) {
>         Message m = exchange.getIn().getBody(Message.class);
>         return m.getStatus().equals(Message.STATUS_IP);
>     }
> }
>
> I'm not sure the implementation of the Predicate is the best way to do it
> but then, about the ordering, my understanding is that the message(0) will
> check the first message by this mock. However, I don't know if it's called
> a soon as there's one message available.
>
> *5-*
> Getting the message from the database before it is modified. I probably
> can do that with an advice. However, advices seem to be before the actual
> endpoint is called. Is there a way to act after?
>
> Thanks
> -
> Henri
>
> On 5 November 2012 11:29, Claus Ibsen <cl...@gmail.com> wrote:
>
>> On Sun, Nov 4, 2012 at 10:09 AM, Henri Tremblay
>> <he...@gmail.com> wrote:
>> > Hi,
>> >
>> > I've started to play with Camel and loving it. However, I'm still having
>> > problems with the testing.
>> >
>> > I want to do an integration test of a complete message processing.
>> Here's
>> > the flow, please focus on the solution, not on the fact that the
>> > architecture is a nonsense, that part isn't really my fault (but I can
>> use
>> > any useful ideas)
>> >
>> > Here it goes:
>> >
>> >    1. Message received from JMS
>> >    2. Stored in DB
>> >    3. Read from DB with a consumer
>> >    4. Sent to a direct route
>> >    5. Status changed
>> >    6. Sent to another system in REST
>> >    7. The other system will change the status in DB and create new DB
>> >    entries
>> >    8. Read from DB with another consumer the new DB entries
>> >    9. Sent to the same direct route
>> >    10. Sent them to the same system in REST
>> >
>> > My questions are
>> >
>> >    - I want to check the message when entering in both direct route. How
>> >    calls like expectedMessageCount are working? Is it ordered? So I
>> should put
>> >    an expectedMessageCount(1) for the first call and another for the
>> second a
>> >    bit below in my test
>>
>> expected message count just checks that X number of messages was
>> received on the mock.
>> You can read the javadoc of the methods to learn more about them.
>>
>> If you use the same mock in both routes, then you can set expectation
>> that a message with content A is received
>> before message with content B etc.
>>
>> I suggest to dig in the links from Christian and if you got the time
>> then David V. did a webinar recently about unit testing with Camel.
>> Its called "Are you Camel routes ready for production".
>> http://fusesource.com/resources/video-archived-webinars/
>>
>>
>>
>> >    - I want to be able to check the status changed at point 5 before it
>> is
>> >    changed again at step 7. How can I do that?
>> >    - What's the ordering of things like
>> >    expectedMessageCount, expectedBodyReceived? When they are specified
>> in a
>> >    row, they always apply to the same message? Is message(0) working
>> the same
>> >    way?
>> >
>>
>> Check the javaddoc, for example expectedBodyReceived expects X
>> messages received in the order,
>> having the body value you specify, eg
>>
>> expectedBodyReceived("foo", "bar");
>>
>> would expect 2 messages, and the 1st has "foo" in message body, the
>> 2nd has "bar" in message body etc.
>>
>>
>> > If it can help understanding the questions, I'm an EasyMock dude. So I'm
>> > trying to map the Camel expectation to their equivalent EasyMock
>> behavior.
>> >
>> > Any help?
>> >
>> > Thanks,
>> > Henri
>>
>>
>>
>> --
>> Claus Ibsen
>> -----------------
>> Red Hat, Inc.
>> FuseSource is now part of Red Hat
>> Email: cibsen@redhat.com
>> Web: http://fusesource.com
>> Twitter: davsclaus
>> Blog: http://davsclaus.com
>> Author of Camel in Action: http://www.manning.com/ibsen
>>
>
>

Re: Order of unit testing

Posted by Henri Tremblay <he...@gmail.com>.
I've already read these two links and managed to do nice unit tests for
simple routes. But I still don't have answers to my questions. I'll check
the webinar. Meanwhile, I'll try to ask simpler questions.

I'm working with
  @Override
  public String isMockEndpoints() {
    return "*";
  }
and a test always ends with assertMockEndpointsSatisfied();

*1-*
MockEndpoint jdbc = getMockEndpoint(...)
MockEndpoint seda = getMockEndpoint(...)
jdbc.expectedMessageCount(1);
seda.expectedMessageCount(1);

My understanding is that assertMockEndpointsSatisfied will wait for the
jdbc and then the seda message one after the other. Am I right?

*2-*
MockEndpoint jdbc = getMockEndpoint(...)
MockEndpoint seda = getMockEndpoint(...)
jdbc.expectedMessageCount(1);
seda.expectedMessageCount(1);
jdbc.expectedMessageCount(1);

Same here, waiting for jdbc, then seda, then jdbc. If that right?

*3-*
MockEndpoint jdbc = getMockEndpoint(...)
jdbc..expectedMessageCount(1);
jdbc..expectedHeaderReceived("header", "value");

Only one message will be received and its header will be checked.

*4-*
MockEndpoint jdbc = getMockEndpoint(...)
jdbc.expectedMessageCount(1);
jdbc.message(0).body(Message.class).in(new Predicate() {
    public boolean matches(Exchange exchange) {
        Message m = exchange.getIn().getBody(Message.class);
        return m.getStatus().equals(Message.STATUS_IP);
    }
}

I'm not sure the implementation of the Predicate is the best way to do it
but then, about the ordering, my understanding is that the message(0) will
check the first message by this mock. However, I don't know if it's called
a soon as there's one message available.

*5-*
Getting the message from the database before it is modified. I probably can
do that with an advice. However, advices seem to be before the actual
endpoint is called. Is there a way to act after?

Thanks
-
Henri

On 5 November 2012 11:29, Claus Ibsen <cl...@gmail.com> wrote:

> On Sun, Nov 4, 2012 at 10:09 AM, Henri Tremblay
> <he...@gmail.com> wrote:
> > Hi,
> >
> > I've started to play with Camel and loving it. However, I'm still having
> > problems with the testing.
> >
> > I want to do an integration test of a complete message processing. Here's
> > the flow, please focus on the solution, not on the fact that the
> > architecture is a nonsense, that part isn't really my fault (but I can
> use
> > any useful ideas)
> >
> > Here it goes:
> >
> >    1. Message received from JMS
> >    2. Stored in DB
> >    3. Read from DB with a consumer
> >    4. Sent to a direct route
> >    5. Status changed
> >    6. Sent to another system in REST
> >    7. The other system will change the status in DB and create new DB
> >    entries
> >    8. Read from DB with another consumer the new DB entries
> >    9. Sent to the same direct route
> >    10. Sent them to the same system in REST
> >
> > My questions are
> >
> >    - I want to check the message when entering in both direct route. How
> >    calls like expectedMessageCount are working? Is it ordered? So I
> should put
> >    an expectedMessageCount(1) for the first call and another for the
> second a
> >    bit below in my test
>
> expected message count just checks that X number of messages was
> received on the mock.
> You can read the javadoc of the methods to learn more about them.
>
> If you use the same mock in both routes, then you can set expectation
> that a message with content A is received
> before message with content B etc.
>
> I suggest to dig in the links from Christian and if you got the time
> then David V. did a webinar recently about unit testing with Camel.
> Its called "Are you Camel routes ready for production".
> http://fusesource.com/resources/video-archived-webinars/
>
>
>
> >    - I want to be able to check the status changed at point 5 before it
> is
> >    changed again at step 7. How can I do that?
> >    - What's the ordering of things like
> >    expectedMessageCount, expectedBodyReceived? When they are specified
> in a
> >    row, they always apply to the same message? Is message(0) working the
> same
> >    way?
> >
>
> Check the javaddoc, for example expectedBodyReceived expects X
> messages received in the order,
> having the body value you specify, eg
>
> expectedBodyReceived("foo", "bar");
>
> would expect 2 messages, and the 1st has "foo" in message body, the
> 2nd has "bar" in message body etc.
>
>
> > If it can help understanding the questions, I'm an EasyMock dude. So I'm
> > trying to map the Camel expectation to their equivalent EasyMock
> behavior.
> >
> > Any help?
> >
> > Thanks,
> > Henri
>
>
>
> --
> Claus Ibsen
> -----------------
> Red Hat, Inc.
> FuseSource is now part of Red Hat
> Email: cibsen@redhat.com
> Web: http://fusesource.com
> Twitter: davsclaus
> Blog: http://davsclaus.com
> Author of Camel in Action: http://www.manning.com/ibsen
>

Re: Order of unit testing

Posted by Claus Ibsen <cl...@gmail.com>.
On Sun, Nov 4, 2012 at 10:09 AM, Henri Tremblay
<he...@gmail.com> wrote:
> Hi,
>
> I've started to play with Camel and loving it. However, I'm still having
> problems with the testing.
>
> I want to do an integration test of a complete message processing. Here's
> the flow, please focus on the solution, not on the fact that the
> architecture is a nonsense, that part isn't really my fault (but I can use
> any useful ideas)
>
> Here it goes:
>
>    1. Message received from JMS
>    2. Stored in DB
>    3. Read from DB with a consumer
>    4. Sent to a direct route
>    5. Status changed
>    6. Sent to another system in REST
>    7. The other system will change the status in DB and create new DB
>    entries
>    8. Read from DB with another consumer the new DB entries
>    9. Sent to the same direct route
>    10. Sent them to the same system in REST
>
> My questions are
>
>    - I want to check the message when entering in both direct route. How
>    calls like expectedMessageCount are working? Is it ordered? So I should put
>    an expectedMessageCount(1) for the first call and another for the second a
>    bit below in my test

expected message count just checks that X number of messages was
received on the mock.
You can read the javadoc of the methods to learn more about them.

If you use the same mock in both routes, then you can set expectation
that a message with content A is received
before message with content B etc.

I suggest to dig in the links from Christian and if you got the time
then David V. did a webinar recently about unit testing with Camel.
Its called "Are you Camel routes ready for production".
http://fusesource.com/resources/video-archived-webinars/



>    - I want to be able to check the status changed at point 5 before it is
>    changed again at step 7. How can I do that?
>    - What's the ordering of things like
>    expectedMessageCount, expectedBodyReceived? When they are specified in a
>    row, they always apply to the same message? Is message(0) working the same
>    way?
>

Check the javaddoc, for example expectedBodyReceived expects X
messages received in the order,
having the body value you specify, eg

expectedBodyReceived("foo", "bar");

would expect 2 messages, and the 1st has "foo" in message body, the
2nd has "bar" in message body etc.


> If it can help understanding the questions, I'm an EasyMock dude. So I'm
> trying to map the Camel expectation to their equivalent EasyMock behavior.
>
> Any help?
>
> Thanks,
> Henri



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: Order of unit testing

Posted by Christian Müller <ch...@gmail.com>.
Hello Henri!

Did you already checked out the following links [1], [2]?
With "adviceWith" you can manipulate your route in your test and "wire tap"
the message to a mock endpoint which can check your expectations. You can
also skip sending the message to the original endpoint (may be cause the
system is not available in your development/test environment). "weaveById"
is another powerful possibility to ...

[1] http://camel.apache.org/advicewith.html
[2] http://camel.apache.org/mock.html

Best,
Christian

On Sun, Nov 4, 2012 at 10:09 AM, Henri Tremblay <he...@gmail.com>wrote:

> Hi,
>
> I've started to play with Camel and loving it. However, I'm still having
> problems with the testing.
>
> I want to do an integration test of a complete message processing. Here's
> the flow, please focus on the solution, not on the fact that the
> architecture is a nonsense, that part isn't really my fault (but I can use
> any useful ideas)
>
> Here it goes:
>
>    1. Message received from JMS
>    2. Stored in DB
>    3. Read from DB with a consumer
>    4. Sent to a direct route
>    5. Status changed
>    6. Sent to another system in REST
>    7. The other system will change the status in DB and create new DB
>    entries
>    8. Read from DB with another consumer the new DB entries
>    9. Sent to the same direct route
>    10. Sent them to the same system in REST
>
> My questions are
>
>    - I want to check the message when entering in both direct route. How
>    calls like expectedMessageCount are working? Is it ordered? So I should
> put
>    an expectedMessageCount(1) for the first call and another for the
> second a
>    bit below in my test
>    - I want to be able to check the status changed at point 5 before it is
>    changed again at step 7. How can I do that?
>    - What's the ordering of things like
>    expectedMessageCount, expectedBodyReceived? When they are specified in a
>    row, they always apply to the same message? Is message(0) working the
> same
>    way?
>
> If it can help understanding the questions, I'm an EasyMock dude. So I'm
> trying to map the Camel expectation to their equivalent EasyMock behavior.
>
> Any help?
>
> Thanks,
> Henri
>



--