Your application context looks good for me, I just found your test case
just extends a wrong class, you need to use
AbstractJUnit4SpringContextTests.
You can find the example here[1]
[1]
https://svn.apache.org/repos/asf/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsResequencerTest.java
Willem
gnanda wrote:
hi All, I am new to camel and junit test
I am trying to create a junit test for my router
I am getting below error, where would I specify connectionFactory? please
suggest.
<testcase time="1.062" classname="com.pf.simplecamelconsumer.SCSRouteTest"
name="testValidXml">
<error message="java.lang.IllegalArgumentException: connectionFactory must
be specified"
type="org.apache.camel.RuntimeCamelException">org.apache.camel.RuntimeCamelException:
java.lang.IllegalArgumentException: connectionFactory must be specified at
org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1055)
at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:100) at
org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:98)
at
org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:111)
at
org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:329)
at
com.pf.simplecamelconsumer.SCSRouteTest.testValidXml(SCSRouteTest.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597) at
junit.framework.TestCase.runTest(TestCase.java:168) at
junit.framework.TestCase.runBare(TestCase.java:134) at
junit.framework.TestResult$1.protect(TestResult.java:110) at
junit.framework.TestResult.runProtected(TestResult.java:128) at
junit.framework.TestResult.run(TestResult.java:113) at
junit.framework.TestCase.run(TestCase.java:124) at
junit.framework.TestSuite.runTest(TestSuite.java:232) at
junit.framework.TestSuite.run(TestSuite.java:227) at
org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:81)
at
org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
at
org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
at
org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177) at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597) at
org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
at
org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)
Caused by: java.lang.IllegalArgumentException: connectionFactory must be
specified at
org.apache.camel.util.ObjectHelper.notNull(ObjectHelper.java:244) at
org.apache.camel.component.jms.JmsConfiguration.createConnectionFactory(JmsConfiguration.java:1172)
at
org.apache.camel.component.jms.JmsConfiguration.getConnectionFactory(JmsConfiguration.java:521)
at
org.apache.camel.component.jms.JmsConfiguration.createTemplateConnectionFactory(JmsConfiguration.java:1189)
at
org.apache.camel.component.jms.JmsConfiguration.getTemplateConnectionFactory(JmsConfiguration.java:558)
at
org.apache.camel.component.jms.JmsConfiguration.createInOnlyTemplate(JmsConfiguration.java:459)
at
org.apache.camel.component.jms.JmsEndpoint.createInOnlyTemplate(JmsEndpoint.java:195)
at
org.apache.camel.component.jms.JmsProducer.getInOnlyTemplate(JmsProducer.java:401)
at org.apache.camel.component.jms.JmsProducer.doSend(JmsProducer.java:343)
at
org.apache.camel.component.jms.JmsProducer.processInOnly(JmsProducer.java:320)
at org.apache.camel.component.jms.JmsProducer.process(JmsProducer.java:150)
at
org.apache.camel.impl.ProducerCache$1.doInProducer(ProducerCache.java:179)
at
org.apache.camel.impl.ProducerCache$1.doInProducer(ProducerCache.java:161)
at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:146)
at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:160)
at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:98) ... 27
more</error>
here is my camel context file named SCSRouteTest-context.xml
located under test/resources/com/pf/simplecamelconsumer
<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-2.0.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
">
<!--
from("seda:a").to("seda:b");
-->
<bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
<property name="configuration" ref="jmsConfig" />
</bean>
<bean id="jmsConnectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://LPF004689:61616" />
<property name="redeliveryPolicy" ref="redeliveryPolicy" />
<!--
<property name="transactionManager"
ref="jmsTransactionManager" />
<property name="transacted" value="true" />
-->
</bean>
<bean id="redeliveryPolicy"
class="org.apache.activemq.RedeliveryPolicy">
<property name="maximumRedeliveries" value="1" />
</bean>
<bean id="jmsConfig"
class="org.apache.camel.component.jms.JmsConfiguration">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="concurrentConsumers" value="5" />
<property name="transactionManager" ref="jmsTransactionManager"
/>
<property name="transacted" value="true" />
<property name="deliveryPersistent" value="true"/>
<!-- <property name="explicitQosEnabled" value="true"/> TODO check
-->
</bean>
<bean id="jmsTransactionManager"
class="org.springframework.jms.connection.JmsTransactionManager">
<property name="connectionFactory" ref="jmsConnectionFactory" />
</bean>
<bean id="PROPAGATION_REQUIRED"
class="org.apache.camel.spring.spi.SpringTransactionPolicy">
<property name="transactionManager" ref="jmsTransactionManager"
/>
<property name="propagationBehaviorName"
value="PROPAGATION_REQUIRED" />
</bean>
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"
trace="true">
<package>com.pf.simplecamelconsumer</package>
</camelContext>
</beans>
<!--
here is my junit test code
package com.pf.simplecamelconsumer;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.CamelTestSupport;
import org.apache.camel.Produce;
import org.springframework.test.annotation.DirtiesContext;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.camel.CamelContext;
import org.apache.commons.io.IOUtils;
//This test will load a Spring XML configuration file called
MyCamelTest-context.xml from the classpath in the same package structure as
the MyCamelTest class and initialize it along with any Camel routes we
define inside it, then inject the CamelContext instance into our test case.
@ContextConfiguration
public class SCSRouteTest extends CamelTestSupport {
@Autowired
protected CamelContext camelContext;
@EndpointInject(uri = "mock:result")
protected MockEndpoint resultEndpoint;
@Produce(uri = "jms:queue:trust01")
protected ProducerTemplate template;
/*
* @DirtiesContext on the test methods to force Spring Testing to
* automatically reload the CamelContext after each test method - this
* ensures that the tests
*
* don't clash with each other (e.g. one test method sending to an endpoint
* that is then reused in another test method).
*/
@Test
@DirtiesContext
public void testValidXml() throws Exception {
String expectedBody = getSampleGoodMessages();
this.resultEndpoint.expectedMessageCount(1);
this.template.sendBody(this.getSampleGoodMessages());
resultEndpoint.expectedBodiesReceived(expectedBody);
resultEndpoint.expectedHeaderReceived("content-type",
"application/soap+xml;charset=UTF-8");
resultEndpoint
.expectedHeaderReceived("content-type",
this.getHTTPURI());
this.resultEndpoint.assertIsSatisfied(10000);
resultEndpoint.assertIsSatisfied();
;
}
private String getHTTPURI() throws IOException {
final InputStream iStream = this.getClass().getResourceAsStream(
"/header.txt");
return IOUtils.toString(iStream);
}
private String getSampleGoodMessages() throws IOException {
final InputStream iStream = this.getClass().getResourceAsStream(
"/sampleMessages.xml");
return IOUtils.toString(iStream);
}
private String getSampleBadXML() throws IOException {
final InputStream iStream = this.getClass().getResourceAsStream(
"/sampleBadXml.xml");
return IOUtils.toString(iStream);
}
}