失败的单元测试 - 预期的文件内容但得到了GenricFile [<文件名>]

时间:2015-07-29 08:58:05

标签: apache-camel

我在Camel中进行了单元测试,用于测试从文件到JMS队列的简单路由。 路线看起来像这样:

public class File2QueueRouteBuilder extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("file:src/data/").to("jms:demo_queue");
    }
}

我通过生成带有生产者模板的文件来测试路由,并通过添加从JMS队列到模拟端点的路由从队列中获取消息。我在sel上下文中将seda注入为JMS组件,以避免依赖于activeMQ进行单元测试。

测试代码如下所示:

public class CamelRiderTest extends CamelTestSupport {

    @Override
    protected CamelContext createCamelContext() throws Exception {
        CamelContext context = super.createCamelContext();
        // use a simple in memory JMS implementation in stead of ActiveMQ as in the production code.
        context.addComponent("jms", context.getComponent("seda"));
        return context;
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        // Get the production route to test.
        RouteBuilder toTest = new File2QueueRouteBuilder();
        // Add a test route that reads the message from the queue to a mock
        toTest.includeRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("jms:demo_queue").to("mock:demo_queue");
            }
        });
        return toTest;
    }

    @Test
    public void testFile2QueueRouteBuilder() throws InterruptedException {
        // Get the mock endpoint for verification
        MockEndpoint demoQueueOutput = getMockEndpoint("mock:demo_queue");
        // Set the expected result
        demoQueueOutput.expectedMessageCount(2); // Assert that two messages are received.
        demoQueueOutput.setAssertPeriod(1000); // Asserts after 1000 millis that no more than 2 messages were received
        demoQueueOutput.allMessages().body().contains("Hello"); // Assert contents of messages
        // Use a template producer to create an input file containing "Hello world"
        template.sendBodyAndHeader("file://src/data", "Hello camel", Exchange.FILE_NAME, "hello1.txt");
        template.sendBodyAndHeader("file://src/data", "Hello again", Exchange.FILE_NAME, "hello2.txt");
        // Wait for the route to process input
        Thread.sleep(1000);
        // Assert mock endpoint expected result.
        demoQueueOutput.assertIsSatisfied();
    }
}

消息似乎按预期通过,但在尝试验证内容时出现以下错误:

15/07/29 09:38:18 INFO demo.CamelRiderTest: ********************************************************************************
15/07/29 09:38:18 INFO demo.CamelRiderTest: Testing: testFile2QueueRouteBuilder(dk.systematic.cura.demo.CamelRiderTest)
15/07/29 09:38:18 INFO demo.CamelRiderTest: ********************************************************************************
15/07/29 09:38:18 INFO impl.DefaultCamelContext: Apache Camel 2.15.2 (CamelContext: camel-1) is starting
15/07/29 09:38:18 INFO management.DefaultManagementStrategy: JMX is disabled
15/07/29 09:38:18 INFO converter.DefaultTypeConverter: Loaded 186 type converters
15/07/29 09:38:18 INFO seda.SedaEndpoint: Endpoint Endpoint[jms://demo_queue] is using shared queue: jms://demo_queue with size: 2147483647
15/07/29 09:38:18 INFO impl.DefaultCamelContext: AllowUseOriginalMessage is enabled. If access to the original message is not needed, then its recommended to turn this option off as it may improve performance.
15/07/29 09:38:18 INFO impl.DefaultCamelContext: StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
15/07/29 09:38:18 INFO impl.DefaultCamelContext: Route: route1 started and consuming from: Endpoint[jms://demo_queue]
15/07/29 09:38:18 INFO impl.DefaultCamelContext: Route: route2 started and consuming from: Endpoint[file://src/data/]
15/07/29 09:38:18 INFO impl.DefaultCamelContext: Total 2 routes, of which 2 is started.
15/07/29 09:38:18 INFO impl.DefaultCamelContext: Apache Camel 2.15.2 (CamelContext: camel-1) started in 0.137 seconds
15/07/29 09:38:19 INFO mock.MockEndpoint: Asserting: Endpoint[mock://demo_queue] is satisfied
15/07/29 09:38:19 INFO demo.CamelRiderTest: ********************************************************************************
15/07/29 09:38:19 INFO demo.CamelRiderTest: Testing done: testFile2QueueRouteBuilder(dk.systematic.cura.demo.CamelRiderTest)
15/07/29 09:38:19 INFO demo.CamelRiderTest: Took: 1.056 seconds (1056 millis)
15/07/29 09:38:19 INFO demo.CamelRiderTest: ********************************************************************************
15/07/29 09:38:19 INFO impl.DefaultCamelContext: Apache Camel 2.15.2 (CamelContext: camel-1) is shutting down
15/07/29 09:38:19 INFO impl.DefaultShutdownStrategy: Starting to graceful shutdown 2 routes (timeout 10 seconds)
15/07/29 09:38:20 INFO impl.DefaultShutdownStrategy: Route: route2 shutdown complete, was consuming from: Endpoint[file://src/data/]
15/07/29 09:38:20 INFO impl.DefaultShutdownStrategy: Route: route1 shutdown complete, was consuming from: Endpoint[jms://demo_queue]
15/07/29 09:38:20 INFO impl.DefaultShutdownStrategy: Graceful shutdown of 2 routes completed in 0 seconds
15/07/29 09:38:20 INFO impl.DefaultCamelContext: Apache Camel 2.15.2 (CamelContext: camel-1) uptime 2.202 seconds
15/07/29 09:38:20 INFO impl.DefaultCamelContext: Apache Camel 2.15.2 (CamelContext: camel-1) is shutdown in 1.006 seconds


Assertion error at index 0 on mock mock://demo_queue with predicate: Simple: body contains Hello evaluated as: GenericFile[hello1.txt] contains Hello on Exchange[hello1.txt]
java.lang.AssertionError: Assertion error at index 0 on mock mock://demo_queue with predicate: Simple: body contains Hello evaluated as: GenericFile[hello1.txt] contains Hello on Exchange[hello1.txt]
    at org.apache.camel.util.PredicateAssertHelper.assertMatches(PredicateAssertHelper.java:43)
    at org.apache.camel.component.mock.AssertionClause.applyAssertionOn(AssertionClause.java:106)
    at org.apache.camel.component.mock.MockEndpoint$18.run(MockEndpoint.java:976)
    at org.apache.camel.component.mock.MockEndpoint.doAssertIsSatisfied(MockEndpoint.java:410)
    at org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied(MockEndpoint.java:378)
    at org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied(MockEndpoint.java:366)
    at dk.systematic.cura.demo.CamelRiderTest.testFile2QueueRouteBuilder(CamelRiderTest.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:105)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
    at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

看起来我没有从队列中获取读取文件的内容,而是其他内容。我做错了什么?

我使用java 1.7(64位)和Camel 2.15.2

在Windows 7上运行

1 个答案:

答案 0 :(得分:0)

Camel File生成GenericFile-Object作为交换的主体。 在你的单元测试中,你期望一个字符串。将您的路线更改为:

from("file:src/data/").convertBodyTo(String.class).to("jms:demo_queue");