You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Christian Weichselbaum <c....@cortical.io> on 2015/02/20 21:38:09 UTC

Assertion Problem - ExpectedBodiesReceived

Hello, 

I’m having troubles with testing a camel route. Now I reduced the test and the route to almost nothing and was able to pinpoint the problem but am still struggling with finding a solution.

The first snippet here does fail with following assertion problem: java.lang.AssertionError: mock://result Received message count. Expected: <1> but was: <0>
---
mock.expectedMessageCount(1);
mock.expectedBodiesReceived(posTypesExpected);
template.sendBody("apple");
mock.assertIsSatisfied();
---
if I comment the 2nd line and omit the “expectedBodiesReceived” then <1> is expected and <1> is received as message count. Why do the first 4 lines fail and why does omitting the expectedBodies method  solve the problem?
---
mock.expectedMessageCount(1);
//mock.expectedBodiesReceived(posTypesExpected);
template.sendBody("apple");
mock.assertIsSatisfied();
---


Thank you very much for any help.

Here is the JUnit Test:

@RunWith(CamelSpringJUnit4ClassRunner.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints("mock:result")
@ContextConfiguration("classpath:META-INF/spring/TestCamel.xml")
public class TestCamel extends CamelTestSupport {
    
    @EndpointInject(uri = "mock:result")
    protected MockEndpoint mock;
 
    @Produce(uri = "vm:retrievePOS_Start")
    protected ProducerTemplate template;
 
    /**
     * 
     * @throws java.lang.Exception
     */
    @Before
    public void setUp() throws Exception {
    }
    
    /**
     * 
     * @throws java.lang.Exception
     */
    @After
    public void tearDown() throws Exception {
    }
    
    @Test
    public final void test() throws InterruptedException {
        List<WiktionaryPosType> posTypesExpected = new ArrayList<WiktionaryPosType>();
        posTypesExpected.add(WiktionaryPosType.NOUN);
        mock.expectedMessageCount(1);
        mock.expectedBodiesReceived(posTypesExpected);
        template.sendBody("apple");
        mock.assertIsSatisfied();
    }

And that’s how the spring xml looks like:

<?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.xsd
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">


    <camelContext id="testCamel"
        xmlns="http://camel.apache.org/schema/spring">

        <route>
            <from uri="vm:retrievePOS_Start" />

            <to uri="mock:result" />
        </route>

    </camelContext>

</beans>

Mit freundlichen Grüßen,
With kind regards,

Christian Weichselbaum
Senior Programmer & Data Scientist

cortical.io
Mariahilferstrasse 4, 1070 Vienna, Austria
+43 676 725 1954
c.weichselbaum@cortical.io
http://cortical.io






Re: Assertion Problem - ExpectedBodiesReceived

Posted by Minh Tran <da...@gmail.com>.
All the expected bodies does is calls the equals method on your expected object against the object that actually came through. So if you told it to expect a list and you gave it a string, then it won’t pass.

On 21 Feb 2015, at 8:34 am, Christian Weichselbaum <c....@cortical.io> wrote:

> I just found out that the problem was that the error message did mislead me. When I’m trying to get Strings through the route, expectedBodiesReceived works perfectly fine. If I do send List<Object> across the routes, then I’m struggling with the expectedBodiesReceived() method. 
> 
> 
> On 20 Feb 2015, at 21:38, Christian Weichselbaum <c....@cortical.io> wrote:
> 
>> Hello, 
>> 
>> I’m having troubles with testing a camel route. Now I reduced the test and the route to almost nothing and was able to pinpoint the problem but am still struggling with finding a solution.
>> 
>> The first snippet here does fail with following assertion problem: java.lang.AssertionError: mock://result Received message count. Expected: <1> but was: <0>
>> ---
>> mock.expectedMessageCount(1);
>> mock.expectedBodiesReceived(posTypesExpected);
>> template.sendBody("apple");
>> mock.assertIsSatisfied();
>> ---
>> if I comment the 2nd line and omit the “expectedBodiesReceived” then <1> is expected and <1> is received as message count. Why do the first 4 lines fail and why does omitting the expectedBodies method  solve the problem?
>> ---
>> mock.expectedMessageCount(1);
>> //mock.expectedBodiesReceived(posTypesExpected);
>> template.sendBody("apple");
>> mock.assertIsSatisfied();
>> ---
>> 
>> 
>> Thank you very much for any help.
>> 
>> Here is the JUnit Test:
>> 
>> @RunWith(CamelSpringJUnit4ClassRunner.class)
>> @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
>> @MockEndpoints("mock:result")
>> @ContextConfiguration("classpath:META-INF/spring/TestCamel.xml")
>> public class TestCamel extends CamelTestSupport {
>> 
>>    @EndpointInject(uri = "mock:result")
>>    protected MockEndpoint mock;
>> 
>>    @Produce(uri = "vm:retrievePOS_Start")
>>    protected ProducerTemplate template;
>> 
>>    /**
>>     * 
>>     * @throws java.lang.Exception
>>     */
>>    @Before
>>    public void setUp() throws Exception {
>>    }
>> 
>>    /**
>>     * 
>>     * @throws java.lang.Exception
>>     */
>>    @After
>>    public void tearDown() throws Exception {
>>    }
>> 
>>    @Test
>>    public final void test() throws InterruptedException {
>>        List<WiktionaryPosType> posTypesExpected = new ArrayList<WiktionaryPosType>();
>>        posTypesExpected.add(WiktionaryPosType.NOUN);
>>        mock.expectedMessageCount(1);
>>        mock.expectedBodiesReceived(posTypesExpected);
>>        template.sendBody("apple");
>>        mock.assertIsSatisfied();
>>    }
>> 
>> And that’s how the spring xml looks like:
>> 
>> <?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.xsd
>>        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
>> 
>> 
>>    <camelContext id="testCamel"
>>        xmlns="http://camel.apache.org/schema/spring">
>> 
>>        <route>
>>            <from uri="vm:retrievePOS_Start" />
>> 
>>            <to uri="mock:result" />
>>        </route>
>> 
>>    </camelContext>
>> 
>> </beans>
>> 
>> Mit freundlichen Grüßen,
>> With kind regards,
>> 
>> Christian Weichselbaum
>> Senior Programmer & Data Scientist
>> 
>> cortical.io
>> Mariahilferstrasse 4, 1070 Vienna, Austria
>> +43 676 725 1954
>> c.weichselbaum@cortical.io
>> http://cortical.io
>> 
>> 
>> 
>> <facebook_32.png><linkedin_32.png><twitter_32.png><google_32.png>
>> 
> 


Re: Assertion Problem - ExpectedBodiesReceived

Posted by Christian Weichselbaum <c....@cortical.io>.
I just found out that the problem was that the error message did mislead me. When I’m trying to get Strings through the route, expectedBodiesReceived works perfectly fine. If I do send List<Object> across the routes, then I’m struggling with the expectedBodiesReceived() method. 


On 20 Feb 2015, at 21:38, Christian Weichselbaum <c....@cortical.io> wrote:

> Hello, 
> 
> I’m having troubles with testing a camel route. Now I reduced the test and the route to almost nothing and was able to pinpoint the problem but am still struggling with finding a solution.
> 
> The first snippet here does fail with following assertion problem: java.lang.AssertionError: mock://result Received message count. Expected: <1> but was: <0>
> ---
> mock.expectedMessageCount(1);
> mock.expectedBodiesReceived(posTypesExpected);
> template.sendBody("apple");
> mock.assertIsSatisfied();
> ---
> if I comment the 2nd line and omit the “expectedBodiesReceived” then <1> is expected and <1> is received as message count. Why do the first 4 lines fail and why does omitting the expectedBodies method  solve the problem?
> ---
> mock.expectedMessageCount(1);
> //mock.expectedBodiesReceived(posTypesExpected);
> template.sendBody("apple");
> mock.assertIsSatisfied();
> ---
> 
> 
> Thank you very much for any help.
> 
> Here is the JUnit Test:
> 
> @RunWith(CamelSpringJUnit4ClassRunner.class)
> @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
> @MockEndpoints("mock:result")
> @ContextConfiguration("classpath:META-INF/spring/TestCamel.xml")
> public class TestCamel extends CamelTestSupport {
>     
>     @EndpointInject(uri = "mock:result")
>     protected MockEndpoint mock;
>  
>     @Produce(uri = "vm:retrievePOS_Start")
>     protected ProducerTemplate template;
>  
>     /**
>      * 
>      * @throws java.lang.Exception
>      */
>     @Before
>     public void setUp() throws Exception {
>     }
>     
>     /**
>      * 
>      * @throws java.lang.Exception
>      */
>     @After
>     public void tearDown() throws Exception {
>     }
>     
>     @Test
>     public final void test() throws InterruptedException {
>         List<WiktionaryPosType> posTypesExpected = new ArrayList<WiktionaryPosType>();
>         posTypesExpected.add(WiktionaryPosType.NOUN);
>         mock.expectedMessageCount(1);
>         mock.expectedBodiesReceived(posTypesExpected);
>         template.sendBody("apple");
>         mock.assertIsSatisfied();
>     }
> 
> And that’s how the spring xml looks like:
> 
> <?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.xsd
>         http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
> 
> 
>     <camelContext id="testCamel"
>         xmlns="http://camel.apache.org/schema/spring">
> 
>         <route>
>             <from uri="vm:retrievePOS_Start" />
> 
>             <to uri="mock:result" />
>         </route>
> 
>     </camelContext>
> 
> </beans>
> 
> Mit freundlichen Grüßen,
> With kind regards,
> 
> Christian Weichselbaum
> Senior Programmer & Data Scientist
> 
> cortical.io
> Mariahilferstrasse 4, 1070 Vienna, Austria
> +43 676 725 1954
> c.weichselbaum@cortical.io
> http://cortical.io
> 
> 
> 
> <facebook_32.png><linkedin_32.png><twitter_32.png><google_32.png>
>