camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James Strachan" <james.strac...@gmail.com>
Subject Re: only testMocksAreValid is executed by Camel/Spring unit test !!
Date Fri, 16 Jan 2009 16:47:03 GMT
2009/1/16 cmoulliard <cmoulliard@gmail.com>:
>
> Sorry but even if i place the two assertions before, nothing appears on the
> log
>
>    @Test
>    public void testUnMarshallMessage() throws Exception {
>
>        resultEndpoint.expectedBodiesReceived(models);
>        resultEndpoint.assertIsSatisfied();

models is a non empty array of bodies right?

If so try debugging or printing the list of exchanges? We've zillions
of tests which use this mechanism to extract the exchanges from the
endpoint; so am pretty sure it works


>
>        for(Exchange exch : resultEndpoint.getExchanges()) {
>                models = (List<HashMap<String, Object>>) exch.getIn().getBody();
>
>                Iterator it = models.iterator();
>
>                LOG.info("Before list iteration");
>
>                while(it.hasNext()){
>                        modelObjects = (HashMap<String, Object>) it.next();
>                        LOG.info("while iteration");
>                        for (String key : modelObjects.keySet()) {
>                                LOG.info("get object");
>                                Object obj = modelObjects.get(key);
>                                LOG.info("Data : " + obj.toString());
>                        }
>                }
>
>        }
>
>
>
> James.Strachan wrote:
>>
>> 2009/1/16 cmoulliard <cmoulliard@gmail.com>:
>>>
>>> I try this but nothing is displayed on the console :
>>>
>>>    @Test
>>>    public void testUnMarshallMessage() throws Exception {
>>>
>>>        for(Exchange exch : resultEndpoint.getExchanges()) {
>>>                models = (List<HashMap<String, Object>>)
>>> exch.getIn().getBody();
>>>
>>>                Iterator it = models.iterator();
>>>
>>>                LOG.info("Before list iteration");
>>>
>>>                while(it.hasNext()){
>>>                        modelObjects = (HashMap<String, Object>)
>>> it.next();
>>>                        LOG.info("while iteration");
>>>                        for (String key : modelObjects.keySet()) {
>>>                                LOG.info("get object");
>>>                                Object obj = modelObjects.get(key);
>>>                                LOG.info("Data : " + obj.toString());
>>>                        }
>>>                }
>>>
>>>        }
>>
>> As I said in the previous mail - you must setup the expectations and
>> assert they are satisfied *before* you look at the received exchanges.
>> So move these next two lines *before* the for loop.
>>
>>>        resultEndpoint.expectedBodiesReceived(models);
>>>        resultEndpoint.assertIsSatisfied();
>>>
>>>
>>>
>>> James.Strachan wrote:
>>>>
>>>> 2009/1/16 cmoulliard <cmoulliard@gmail.com>:
>>>>>
>>>>> @James,
>>>>>
>>>>> I'm a little bit bored because I don't know if the following code works
>>>>> or
>>>>> not. The test is passed but I don't know if assertEquals is executed.
>>>>
>>>> You could always add a counter inside the loop and assert its
>>>> incremented once :)
>>>>
>>>>
>>>>>    @Test
>>>>>    public void testUnMarshallMessage() throws Exception {
>>>>>
>>>>>        resultEndpoint.expectedMessageCount(1);
>>>>>
>>>>>        for(Exchange exch : resultEndpoint.getExchanges()) {
>>>>>                models = (List<HashMap<String, Object>>)
>>>>> exch.getIn().getBody();
>>>>>
>>>>>                Iterator it = models.iterator();
>>>>>                double count = 0;
>>>>>                while(it.hasNext()){
>>>>>                        modelObjects = (HashMap<String, Object>)
>>>>> it.next();
>>>>>                        count++;
>>>>>                }
>>>>>
>>>>>                assertEquals(100, count);
>>>>>
>>>>>        }
>>>>>
>>>>>        resultEndpoint.assertIsSatisfied();
>>>>
>>>> the above line needs to be moved above the for loop - more below...
>>>>
>>>>>    }
>>>>>
>>>>> I have different questions :
>>>>>
>>>>> - Can we mix mockendpoint assertion with junit assertions ?
>>>>
>>>> Sure.
>>>>
>>>> The trick is - you execute the resultEndpoint.assertIsSatisfied();
>>>> code first. This then pauses the test until asynchronously the
>>>> messages arrive and expectations are met - or things timeout and the
>>>> test fails. Then after this point - you can grab the received messages
>>>> and perform any particular JUnit assertions you want on the received
>>>> messages.
>>>>
>>>> In psuedocode a mock endpoint test looks like
>>>>
>>>> * get mock endpoints and add expectations
>>>> * assert expectations are met
>>>> * perform any extra JUnit assertions on received messages (or
>>>> endpoints, beans whatever)
>>>>
>>>> Another option is to add an assertion to be evaluated as the messages
>>>> arrive - e.g.
>>>>
>>>> mockEndpoint.allMessages().predicate().xpath('/foo/bar = 'abc'");
>>>> mockEndpoint.message(3).predicate().xpath('/foo/bar = 'abc'");
>>>>
>>>> but its often easier to just do the assertions after the expectations
>>>> are met (though the downside is that more messages could arrive after
>>>> this point if you have an async publisher sending continuously to the
>>>> mock endpoint).
>>>>
>>>> --
>>>> James
>>>> -------
>>>> http://macstrac.blogspot.com/
>>>>
>>>> Open Source Integration
>>>> http://fusesource.com/
>>>>
>>>>
>>>
>>>
>>> -----
>>> Charles Moulliard
>>> SOA Architect
>>>
>>> My Blog :  http://cmoulliard.blogspot.com/
>>> http://cmoulliard.blogspot.com/
>>> --
>>> View this message in context:
>>> http://www.nabble.com/only-testMocksAreValid-is-executed-by-Camel-Spring-unit-test-%21%21-tp21436628s22882p21502317.html
>>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>>
>>>
>>
>>
>>
>> --
>> James
>> -------
>> http://macstrac.blogspot.com/
>>
>> Open Source Integration
>> http://fusesource.com/
>>
>>
>
>
> -----
> Charles Moulliard
> SOA Architect
>
> My Blog :  http://cmoulliard.blogspot.com/ http://cmoulliard.blogspot.com/
> --
> View this message in context: http://www.nabble.com/only-testMocksAreValid-is-executed-by-Camel-Spring-unit-test-%21%21-tp21436628s22882p21503138.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>



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

Open Source Integration
http://fusesource.com/

Mime
View raw message