/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package camelinaction; import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.test.junit4.CamelTestSupport; import org.junit.Test; /** * Our first unit test using the Mock component * * @version $Revision$ */ public class FirstMockTest extends CamelTestSupport { @Override protected CamelContext createCamelContext() throws Exception { CamelContext context = super.createCamelContext(); // replace JMS with SEDA which we can do in this case as seda is a very very basic // in memory JMS broker ;). This is of course only possible to switch for a few components. context.addComponent("jms", context.getComponent("seda")); return context; } @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { // a simple route which listen on messages on a JMS topic from("jms:topic:quote").to("mock:quote"); } }; } @Test public void testQuote() throws Exception { // get the mock endpoint MockEndpoint quote = getMockEndpoint("mock:quote"); // set expectations such as 1 message should arrive quote.expectedMessageCount(1); // fire in a message to Camel template.sendBody("jms:topic:quote", "Camel rocks"); // verify the result quote.assertIsSatisfied(); } @Test public void testSameMessageArrived() throws Exception { // get the mock endpoint MockEndpoint quote = getMockEndpoint("mock:quote"); // set expectations that the same message arrived as we send quote.expectedBodiesReceived("Camel rocks"); // fire in a message to Camel template.sendBody("jms:topic:quote", "Camel rocks"); // verify the result quote.assertIsSatisfied(); } @Test public void testTwoMessages() throws Exception { // get the mock endpoint MockEndpoint quote = getMockEndpoint("mock:quote"); // set expectations the two messages arrives in any order quote.expectedBodiesReceivedInAnyOrder("Camel rocks", "Hello Camel"); // fire in a messages to Camel template.sendBody("jms:topic:quote", "Hello Camel"); template.sendBody("jms:topic:quote", "Camel rocks"); // verify the result quote.assertIsSatisfied(); } @Test public void testTwoMessagesOrdered() throws Exception { // get the mock endpoint MockEndpoint quote = getMockEndpoint("mock:quote"); // set expectations the two messages arrives in specified order quote.expectedBodiesReceived("Hello Camel", "Camel rocks"); // fire in a messages to Camel template.sendBody("jms:topic:quote", "Hello Camel"); template.sendBody("jms:topic:quote", "Camel rocks"); // verify the result quote.assertIsSatisfied(); } @Test public void testContains() throws Exception { // get the mock endpoint MockEndpoint quote = getMockEndpoint("mock:quote"); // set expectations the two messages arrives in specified order quote.expectedMessageCount(2); // all messages should contain the Camel word quote.allMessages().body().contains("Camel"); // fire in a messages to Camel template.sendBody("jms:topic:quote", "Hello Camel"); template.sendBody("jms:topic:quote", "Camel rocks"); // verify the result quote.assertIsSatisfied(); } }