使用cametestsupport进行骆驼单元测试,模板始终为null [英] camel unit test with cametestsupport, template is always null

查看:86
本文介绍了使用cametestsupport进行骆驼单元测试,模板始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用骆驼做一个简单的单元测试.我要做的就是从文件中(在资源下)读取一个json,将其发送到java类进行验证-这是我尝试测试的方法.无论我做什么,模板(用于sendBody(json)的模板始终为null.这是我的代码

I am doing a simple unit test with camel. All I want to do is to read a json from a file (under resources), send it to a java class for validation - this is the route that I am trying to test. Whatever I do, the template (which I use to sendBody(json) is always null. Here is my code

public class RouteTests extends CamelTestSupport {

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

    @Produce(uri = "direct:start")
    protected ProducerTemplate template;

    @Autowired
    JSONObject testJson;

    @Before
    public void setUp() throws Exception {
        try {
            final ObjectMapper objectmapper = new ObjectMapper();
            final ClassLoader loader = Thread.currentThread().getContextClassLoader();
            final InputStream stream = loader.getResourceAsStream("test.json");
            testJson = new JSONObject ((Map)objectmapper.readValue(stream, Map.class));
            //start camel
            context = new DefaultCamelContext();
            context.addRoutes(createRouteBuilder());
            context.start();
        } catch (IOException e) {
        }
    }

    @Test
    public void testSendMatchingMessage() throws Exception {
//        resultEndpoint.expectedBodiesReceived(expectedBody);
        resultEndpoint = getMockEndpoint("mock:result");
//        resultEndpoint = context.getEndpoint("mock:result", MockEndpoint.class);
        resultEndpoint.expectedMessageCount(1);
        template.sendBody("direct:start", testJson);
        resultEndpoint.assertIsSatisfied();
    }

    @Override
    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() {
                from("direct:start")
                        .filter().method(ValidationProcessor.class, "validate")
                        .to("mock:result");
            }
        };
    }

    @Override
    protected JndiRegistry createRegistry() throws Exception {
        JndiRegistry jndi = super.createRegistry();
        jndi.bind("ValidationProcessor", new ValidationProcessor",());

        return jndi;
    }
}

我面临的问题: 1.最初,结果终点也始终为null. (我使用了

Problems I faced: 1. Initially the result end point also was always null. (I used http://svn.apache.org/repos/asf/camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java for reference). Then I had to do an explicit

resultEndpoint = getMockEndpoint("mock:result");

解决该问题.

  1. 然后,我读到我必须重写createRegistry,但是我不知道如何绑定.我只是使用了验证类的名称,但我不知道这是否正确.

但是模板始终为空. NPE位于

But the template is always null. The NPE is at

template.sendBody("direct:start", testJson);

很长一段时间以来,我一直在努力解决这个问题,我终于向你们寻求帮助.真的很累:(

I have been trying to resolve this for a long time and I finally turn to you guys for help. Really am tired :(

如果需要的话,还请给我指出一些阅读材料. apache camel docs链接到的参考代码甚至没有我在setUp方法中所做的骆驼的开始.甚至不确定是否需要. 请帮忙!

Please also point me to some reading if necessary. The reference code that apache camel docs link to did not even have the starting of the camel that I do in the setUp method. Not even sure if it is needed. Please help !

金枪鱼

推荐答案

我认为您已经错过了CamelTestSupport对您有用的许多真正有用的东西.它具有自己的setUp方法,您应该重写该方法.我相信您的测试应该看起来像这样:

I think you've missed out on a lot of the really helpful stuff that CamelTestSupport does for you. It has its own setUp method that you should override. I believe your test should really look something like this:

public class RouteTests extends CamelTestSupport {

    private JSONObject testJson;

    @Override
    public void setUp() throws Exception {
        // REALLY important to call super
        super.setUp();

        ObjectMapper objectmapper = new ObjectMapper();
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        InputStream stream = loader.getResourceAsStream("test.json");
        testJson = new JSONObject(objectmapper.readValue(stream, Map.class));
    }

    @Override
    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            @Override
            public void configure() {
                from("direct:start")
                        .filter().method(ValidationProcessor.class, "validate")
                        .to("mock:result");
            }
        };
    }

    @Test
    public void testSendMatchingMessage() throws Exception {
        MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
        resultEndpoint.expectedMessageCount(1);
        template.sendBody("direct:start", testJson);
        resultEndpoint.assertIsSatisfied();
    }
}

实际上,我将完全删除setUp的替代项,并将对测试数据的读取放入测试方法本身.这样就可以清楚地知道数据的用途,并且可以消除testJson字段.

Actually I would remove the override of setUp altogether and put the reading of the test data in to the test method itself. Then it's clear what the data is being used for and you can eliminate the testJson field.

public class RouteTests extends CamelTestSupport {

    @Override
    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            @Override
            public void configure() {
                from("direct:start")
                        .filter().method(ValidationProcessor.class, "validate")
                        .to("mock:result");
            }
        };
    }

    @Test
    public void testSendMatchingMessage() throws Exception {
        ObjectMapper objectmapper = new ObjectMapper();
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        InputStream stream = loader.getResourceAsStream("test.json");
        JSONObject testJson = new JSONObject(objectmapper.readValue(stream, Map.class));

        MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
        resultEndpoint.expectedMessageCount(1);
        template.sendBody("direct:start", testJson);
        resultEndpoint.assertIsSatisfied();
    }
}

那里,简单得多.

这篇关于使用cametestsupport进行骆驼单元测试,模板始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆