Quantcast
Channel: Apache Timeline
Viewing all articles
Browse latest Browse all 5648

ProducerTemplate null when used to construct bean in new JndiContext in a unit test

$
0
0
I am trying to unit test a route that uses bean to dynamically construct an
sftp endpoint. This works when I run my route in a normal context, as it
seems a template is already in the registry and can be correctly injected.

My problem is when trying to construct the unit test I seem to be running
into a condition where the ProducerTemplate is still null when
createJndiContext is invoked and that results in an NPE in my bean that need
to call a method on the template.

Any help would be greatly appreciated.

Here is a simplified unit test that illustrates this:

public class BeanWithProdTemplateDependencyTest
extends CamelTestSupport

private static final Logger log =
LoggerFactory.getLogger(BeanWithProdTemplateDependencyTest.class);
private static final String FROM = "direct:start";

@EndpointInject(uri = "mock:result")
protected MockEndpoint resultEndpoint;

@Before
public void setUp() throws Exception {

super.setUp();
context.getRouteDefinition("my-cool-route").adviceWith(context, new
AdviceWithRouteBuilder()

@Override
public void configure()
throws Exception

replaceFromWith(FROM);

});
startCamelContext();

@Test
public void test()

template.sendBody(FROM, "cheese");

@Override
public boolean isUseAdviceWith()

return true;

@Override
protected Context createJndiContext()
throws Exception

JndiContext context = new JndiContext();
MyBean myBean = new MyBean(template);
context.bind("myBean", myBean);

return context;

@Override
protected RouteBuilder createRouteBuilder()
throws Exception

return new RouteBuilder()

@Override
public void configure()
throws Exception

from("jms:queue:inbox")
.routeId("my-cool-route")
.beanRef("myBean", "doStuff")
.log("Body: $body}")
.to("mock:result");

};

public class MyBean {

private final ProducerTemplate producerTemplate;

public MyBean(ProducerTemplate template)

this.producerTemplate = template;

public void doStuff() throws Exception{
// NPE here, template is null
this.producerTemplate.sendBody("seda:foo", "beer");

Viewing all articles
Browse latest Browse all 5648

Trending Articles