在JUnit测试中模拟Apache Camel的位置 - java

我正在尝试模拟Camel Routes的inout路径,但我不知道如何提供模拟输入和输出路径,请帮助我解决此问题。

application.properties

inputFilePath = src/main/resources/in
outputFilePath = src/main/resources/out

application-test.properties

inputFilePath = src/test/java/in
outputFilePath = src/test/java/out

路由器和处理器:

@Component
public class FileLineByLineRouter extends RouteBuilder {

    @Value("${inputFilePath}")
    private String inputFilePath;

    @Value("${outputFilePath}")
    private String outputFilePath;

    @Override
    public void configure() throws Exception {
        from("file://" + inputFilePath + "?delete=true").routeId("FileLineByLineRoute").marshal().string("UTF-8")
                .split(body().tokenize("\n")).streaming().process(getFileParsingProcessor())
                .to("file://" + outputFilePath + "?fileExist=Append").end();
    }

    @Bean
    public Processor getFileParsingProcessor() {

        return new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
                String order = exchange.getIn().getBody(String.class);
                order = order + ": " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss S").format(new Date()) + "\n";
                exchange.getIn().setBody(order);
            }
        };
    }
}

Junit测试代码:

    @RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class })
@SpringBootTest(classes = FileLineByLineRouter.class)
@ActiveProfiles("test")
@EnableAutoConfiguration
public class FileLineByLineRouterTest2 extends CamelTestSupport {

    @Autowired
    protected CamelContext camelContext;

    @Test
    public void test() throws Exception {
        camelContext.start();
        Thread.sleep(2000);
        File outDir = new File("src/test/java/out");
        System.out.println(outDir.getAbsolutePath());
        assertTrue(outDir.isDirectory());
        assertTrue(outDir.listFiles().length != 0);
    }    
}

日志:

114  SpringCamelContext      : Route: FileLineByLineRoute started and consuming from: file://src/test/java/in?delete=true
116  SpringCamelContext      : Total 1 routes, of which 1 are started.
122  SpringCamelContext      : Apache Camel 2.19.1 (CamelContext: camel-1) started in 0.582 seconds
138  FileLineByLineRouterTest2     : Started FileLineByLineRouterTest2 in 10.064 seconds (JVM running for 12.063)
179  FileLineByLineRouterTest2     : ********************************************************************************
180  FileLineByLineRouterTest2     : Testing: test(FileLineByLineRouterTest2)
180  FileLineByLineRouterTest2     : ********************************************************************************
222  o.apache.camel.impl.DefaultCamelContext  : Apache Camel 2.19.1 (CamelContext: camel-2) is starting
223  o.a.c.m.DefaultManagementStrategy        : JMX is disabled
238  o.a.c.i.converter.DefaultTypeConverter   : Loaded 193 type converters
239  o.apache.camel.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
239  o.apache.camel.impl.DefaultCamelContext  : Total 0 routes, of which 0 are started.
239  o.apache.camel.impl.DefaultCamelContext  : Apache Camel 2.19.1 (CamelContext: camel-2) started in 0.017 seconds
239  SpringCamelContext      : Apache Camel 2.19.1 (CamelContext: camel-1) is starting
239  SpringCamelContext      : Total 1 routes, of which 1 are started.
239  SpringCamelContext      : Apache Camel 2.19.1 (CamelContext: camel-1) started in 0.000 seconds
C:\Users\workspace\CamelProject\src\test\java\out
241  FileLineByLineRouterTest2     : ********************************************************************************
241  FileLineByLineRouterTest2     : Testing done: test(FileLineByLineRouterTest2)
241  FileLineByLineRouterTest2     : Took: 0.002 seconds (2 millis)
241  FileLineByLineRouterTest2     : ********************************************************************************
242  o.apache.camel.impl.DefaultCamelContext  : Apache Camel 2.19.1 (CamelContext: camel-2) is shutting down
314  o.apache.camel.impl.DefaultCamelContext  : Apache Camel 2.19.1 (CamelContext: camel-2) uptime 0.092 seconds
318  o.apache.camel.impl.DefaultCamelContext  : Apache Camel 2.19.1 (CamelContext: camel-2) is shutdown in 0.071 seconds
336   o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@394df057: startup date [Mon Jan 08 17:32:43 IST 2018]; root of context hierarchy
344   o.a.camel.spring.SpringCamelContext      : Apache Camel 2.19.1 (CamelContext: camel-1) is shutting down
346   o.a.camel.impl.DefaultShutdownStrategy   : Starting to graceful shutdown 1 routes (timeout 300 seconds)
356  INFO 19900 --- [ - ShutdownTask] o.a.camel.impl.DefaultShutdownStrategy   : Route: FileLineByLineRoute shutdown complete, was consuming from: file://src/test/java/in?delete=true
356   o.a.camel.impl.DefaultShutdownStrategy   : Graceful shutdown of 1 routes completed in 0 seconds
362   o.a.camel.spring.SpringCamelContext      : Apache Camel 2.19.1 (CamelContext: camel-1) uptime 0.123 seconds
362   o.a.camel.spring.SpringCamelContext      : Apache Camel 2.19.1 (CamelContext: camel-1) is shutdown in 0.018 seconds

参考方案

好吧,在重新阅读您的评论和更新的问题之后,我想我现在已经明白了您的意思了……您的测试还没有开始。

试试这个:

在您的Testclass中删除extends CamelTestSupport。这是基于注释的测试支持的替代方法。
在测试中删除camelContext.start()。我可能将您的建议示例弄糊涂了。使用@UseAdviceWith注释类时,只需自己启动上下文。
最后,请稍等。出于示例的原因,在测试中插入Thread.sleep(10000)可以让文件有时间进行处理。

可以使用Camel NotifyBuilder(http://camel.apache.org/notifybuilder.html)代替固定的睡眠

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

java:继承 - java

有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…

Camel Netty UDP侦听器在0.0.0.0上侦听并且未接收到数据包 - java

我是Camel,Netty和UDP的新手,但我已经对此进行了一段时间的研究,但仍然不知道发生了什么。我要做的就是使用Camel和Netty实现UDP侦听器(当前在Windows 7上,但会将项目迁移到Linux)。我的spring配置如下:<camel:camelContext id="test"> <camel:rou…

Java-如何将此字符串转换为日期? - java

我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…

Java:从类中查找项目名称 - java

仅通过类的实例,如何使用Java反射或类似方法查找项目名称?如果不是,项目名称(我真正想要的是)可以找到程序包名称吗? 参考方案 项目只是IDE使用的简单组织工具,因此项目名称不是类或JVM中包含的信息。要获取软件包,请使用Class#getPackage()。然后,可以调用Package#getName()将包作为您在代码的包声明中看到的String来获取…