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

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

问题描述

我正在用 Camel 做一个简单的单元测试.我想要做的就是从文件(在资源下)读取 JSON 内容,将其发送到 Java 类进行验证——这是我试图测试的路线.无论我做什么,模板(我用来sendBody(json) 总是空的.这是我的代码:

I am doing a simple unit test with Camel. All I want to do is to read JSON content 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. 最初结果终点也始终为空.(我使用了 FilterTest.java 供参考).然后我不得不做一个明确的

  1. Initially the result end point also was always null. (I used FilterTest.java for reference). Then I had to do an explicit

 resultEndpoint = getMockEndpoint("mock:result");

解决这个问题.

然后我读到我必须覆盖createRegistry,但我不知道如何绑定.我只是用了我的验证类的名字,不知道这样对不对.

Then I read that I had to override the createRegistry, but I did not know how to bind. I just used the name of my validation class, but I don’t know if this is right.

但模板始终为空.空指针异常 (NPE) 位于

But the template is always null. The null pointer exception (NPE) is at

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

如有必要,还请指点我一些阅读材料.Apache Camel 文档链接到的参考代码甚至没有我在 setUp 方法中所做的 Camel 的开头.

Please also point me to some reading if necessary. The reference code that Apache Camel documentation link to did not even have the starting of the Camel that I do in the setUp method.

推荐答案

我认为您已经错过了 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进行Camel单元测试,模板始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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