小编典典

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

spring-boot

我正在尝试模拟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

阅读 385

收藏
2020-05-30

共1个答案

小编典典

好的,在重新阅读您的评论和更新的问题之后,我想我现在已经明白了您的意思了……您的测试还无法进行。

尝试这个:

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

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

2020-05-30