You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Лень Визгалова <ro...@gmail.com> on 2016/09/27 04:56:23 UTC

CamelSpringTestSupport - NullPointer on context.getRouteDefinition

Hi,

Here is my question..

I'm trying to test an existing route with CamelSpringTestSupport, want to
mock endpoints with 'adviceWith', but there get NullPointerException
on context.getRouteDefinitions().get(0).. as there're no route definitions,
neither routes..

Please, give me a hint of what I'm doing wrong.

Thank you!


And here are details.

*test_camel.xml :*
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="
http://activemq.apache.org/schema/core"
xsi:schemaLocation="
       http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
       http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd">


    <bean id="myUpdateMessageProcessor"
class="com.myproject.myservice.camel.MyUpdateMessageProcessor"/>

<bean id="jsonObjectMapper"
class="com.myproject.myservice.jackson.CustomObjectMapper"/>

<bean id="testMessageProcessor"
class="com.myproject.myservice.camel.TestMessageProcessor" />

<bean id="myDataFormat"
class="org.apache.camel.component.jackson.JacksonDataFormat">
<constructor-arg ref="jsonObjectMapper"/>
<constructor-arg value="com.myproject.myservice.camel.UpdateRequest"/>
</bean>

<bean id="myRouteBuilder"
class="com.myproject.myservice.camel.MyRouteBuilder" >
<property name="myUpdateQueue" value="myUpdateQueue" />
<property name="enableRoute" value="true" />
<property name="camelLoggingEndPointUrl" value="log:endpoint" />
<property name="myDataFormat" ref="myDataFormat"/>
</bean>

</beans>
*_____________________________________________________________________________________________________________________*
*MyRouteBuilder :*

package com.myproject.myservice.camel;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spi.DataFormat;

public class MyRouteBuilder extends RouteBuilder {

private String myUpdateQueue;
private boolean enableRoute;
private String camelLoggingEndPointUrl;
private DataFormat myDataFormat;

@Override
public void configure() throws Exception {
if (!enableRoute) {
return;
}

onException(Exception.class).handled(true).process(new
MyExceptionHandler());
from(myUpdateQueue).routeId("myRouteBuilder")
.choice()
.when(header(TestMessageProcessor.TST).isNotNull()).bean("bean:testMessageProcessor")
.otherwise().to(camelLoggingEndPointUrl).unmarshal(myDataFormat).to("bean:myUpdateMessageProcessor");
}
public void setMyDataFormat(DataFormat myDataFormat) {
this.myDataFormat = myDataFormat;
}

public void setMyUpdateQueue(String myUpdateQueue) {
this.myUpdateQueue = myUpdateQueue;
}

public void setEnableRoute(boolean enableRoute) {
this.enableRoute = enableRoute;
}
public void setCamelLoggingEndPointUrl(String camelLoggingEndPointUrl) {
this.camelLoggingEndPointUrl = camelLoggingEndPointUrl;
}
}

*_____________________________________________________________________________________________________________________*
*MyRouteBuilderTest *:


package com.myproject.myservice.camel;

import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.spring.CamelSpringTestSupport;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.AbstractApplicationContext;

public class MyRouteBuilderTest extends CamelSpringTestSupport {


    @Autowired
    MyRouteBuilder myRouteBuilder;

    @Test
    public void test() throws Exception {

        context.getRouteDefinition("myRouteBuilder")
                .adviceWith(context, new AdviceWithRouteBuilder () {
                    @Override
                    public void configure() throws Exception {
                        // mock the for testing

                        interceptSendToEndpoint("bean:testMessageProcessor")
                                .skipSendToOriginalEndpoint()
                                .to("mock:testMessageProcessor");
                    }
                });

        context.start();

// not important as it fails here:
context.getRouteDefinition("myRouteBuilder")

        context.stop();
    }


    @Override
    public boolean isUseAdviceWith() {
        return true;
    }

    @Override
    protected AbstractApplicationContext createApplicationContext() {
        return new
org.springframework.context.support.ClassPathXmlApplicationContext("test_camel.xml");
    }
}


-- 
Best Regards,
rollerhel