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 13:13:46 GMT
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/

Mime
View raw message