Camel端点模拟nullpointer异常

时间:2017-08-26 09:31:59

标签: junit apache-camel jbossfuse switchyard

我试图使用camel中的模拟端点模拟switchyard端点switchyard:// TohpGetAccountReports。在junit test中定义的mockAccountReportEndpoint返回null并且测试id因nullpointer exception而失败。请告诉我我的代码有什么问题?

骆驼路线:

    <route id="accountReportRoute" streamCache="true">
        <from uri="direct:accountReport" />
        <doTry>
            <setProperty propertyName="hpResponse">
                <constant>A</constant>
            </setProperty>
            <to
                uri="xslt:xslt/account_reports_manager/create_get_accountreport_request.xslt" />

            <log message="GetAccountReports Request : ${body}" logName="esb.hp.InModelProvider.AccountReport"
                loggingLevel="TRACE" />
            <to
                id="AccountReportEndpoint" uri="switchyard://TohpGetAccountReports?operationName=GetAccountReports"  />

            <log message="GetAccountReports Response : ${body}" logName="esb.hp.InModelProvider.AccountReport"
                loggingLevel="TRACE" />
            <to
                uri="xslt:xslt/account_reports_manager/create_get_accountreport_response.xslt" />

            <doCatch>
                <exception>java.lang.Exception</exception>
                <handled>
                    <constant>false</constant>
                </handled>
                <log
                    message="Exception in hp Model provider accountReportRoute ${exception.stacktrace} "
                    logName="esb.hp.InModelProvider.AccountReport" loggingLevel="ERROR" />
            </doCatch>
        </doTry>
        <log message="Workaround for handling account report soap fault"
            logName="esb.hp.InModelProvider.AccountReport" loggingLevel="DEBUG" />
    </route>

junit测试类

package com.company.esb.hp.in.modelprovider.test;        
import java.util.Map;        
import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.DefaultExchange;
import org.apache.camel.model.ModelCamelContext;
import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.switchyard.ServiceDomain;
import org.switchyard.component.test.mixins.cdi.CDIMixIn;
import org.switchyard.component.test.mixins.http.HTTPMixIn;
import org.switchyard.test.BeforeDeploy;
import org.switchyard.test.Invoker;
import org.switchyard.test.ServiceOperation;
import org.switchyard.test.SwitchYardRunner;
import org.switchyard.test.SwitchYardTestCaseConfig;
import org.switchyard.test.SwitchYardTestKit;
import org.switchyard.transform.config.model.TransfohpwitchYardScanner;

import com.company.esb.common.test.esbSwitchyardTest;
import com.company.esb.hp.in.modelprovider.test.util.RiskInProviderUtil;

@SwitchYardTestCaseConfig(config = SwitchYardTestCaseConfig.SWITCHYARD_XML, mixins = {
        CDIMixIn.class, HTTPMixIn.class }, scanners = { TransfohpwitchYardScanner.class })
@RunWith(SwitchYardRunner.class)
public class hpInModelProviderTest extends esbSwitchyardTest {
    private static final Logger LOG = Logger
            .getLogger(hpInModelProviderTest.class);
    private SwitchYardTestKit testKit;

    public static final String getAcctResult = "TohpGetAccountReports";

    @EndpointInject(uri = "mock://switchyard:TohpGetAccountReports*")
    protected MockEndpoint mockAccountReportEndpoint;



    @BeforeDeploy
    public void setProperties() {
        System.setProperty("esb.hp.inmodelprovider.url", "http://pusslasrws1451.company.biz");

    }

    @Before
    public void installServices() {
        simulateesbServices(testKit);
    }

    @Test
    public void testAccountReportDirectRouteSuccess() throws Exception {
        String request = readFile("xml/endtoend/success/GetJobsResults/get_job_status-response.xml");
        ServiceDomain domain = testKit.getServiceDomain();
        //testKit.removeService(getAcctResult);
        //testKit.registerInOutService(getAcctResult,
            //  new GetAccountReportsServiceMockExchangeHandler());
        CamelContext ctx = (CamelContext) domain
                .getProperty("CamelContextProperty");
        Exchange ex = new DefaultExchange(ctx);
        ModelCamelContext modelContext = (ModelCamelContext) ctx;
        modelContext.getRouteDefinition("accountReportRoute").adviceWith(modelContext, new AdviceWithRouteBuilder() {
            @Override
            public void configure() throws Exception {

                weaveById("AccountReportEndpoint").replace().to(mockAccountReportEndpoint);
            }
        });
        mockAccountReportEndpoint.expectedMessageCount(1);
        mockAccountReportEndpoint.returnReplyBody(new Expression() {
            @Override
            public <T> T evaluate(Exchange exchange, Class<T> type) {
                return exchange.getContext().getTypeConverter().convertTo(type, RiskInProviderUtil.readFile("xml/endtoend/success/GetJobsResults/get_account_report_service-response.xml"));
            }
        });

        // MockEndpoint endpoint = getMockEndpoint("mock:switchyard:service"); 
        ex.setProperty("sToken", "f3669d3a-5a2b-4b99-a8df-3aabfd4aae58");
        ex.setProperty("sAccId", "196497");
        ex.getIn().setBody(request);
        ProducerTemplate producer = ctx.createProducerTemplate();
        producer.send("direct:accountReport", ex);
        System.out.println(ex.getIn().getBody(String.class));
    }

    @Override
    protected void addSetupProperties(Map<String, String> map) {

    }

}

1 个答案:

答案 0 :(得分:1)

  

..完成建议后,必须通过在上下文实例上调用start方法启动CamelContext。

     

http://camel.apache.org/advicewith.html#AdviceWith-UsingisUseAdviceWith()

尝试覆盖isUseAdviceWith()方法(应返回true)并在完成adviceWith后启动上下文。如果它已经启动,那么再次尝试stop()start()它(冷启动)。