gpt4 book ai didi

java - 测试Apache Camel Junit的所有进程和路由

转载 作者:行者123 更新时间:2023-12-02 11:36:59 24 4
gpt4 key购买 nike

我正在尝试验证路由的所有进程,但在开始中断后启动工作正常。请为此提供一些有用的引用或示例。提前致谢。

测试.txt:

F1:L1
F2:L2
F3:L3

Customer.java

    private String firstName;
private String lastName;
// getters and setters
@Override
public String toString(){
return firstName +":::" + lastName;
}
}

JUnit 和路由:

    public class FileTest7 extends CamelTestSupport {

@EndpointInject(uri = "direct:teststart")
private Endpoint start;

@EndpointInject(uri = "mock:direct:process1")
private MockEndpoint mockProcess1;

@EndpointInject(uri = "mock:direct:process2")
private MockEndpoint mockProcess2;

@EndpointInject(uri = "mock:direct:process3")
private MockEndpoint mockProcess3;

@EndpointInject(uri = "mock:direct:write2File")
private MockEndpoint mockWrite2File;

@EndpointInject(uri = "mock:end")
private MockEndpoint mockEnd;

@Override
public boolean isUseAdviceWith() {
return true;
}

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("file:/var/file.log&noop=true").routeId("MY_ROUTE").to("direct:process1");

from("direct:process1").routeId("process1").process(exchange -> {
File file = exchange.getIn().getBody(File.class);
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
List<Customer> customers = new ArrayList<>();

String line = null;
while ((line = br.readLine()) != null) {
String[] names = line.split(",");
customers.add(new Customer(names[0], names[1]));
}
br.close();
exchange.getOut().setBody(customers);
}).to("direct:process2");

from("direct:process2").routeId("process2").split(simple("${body}")).to("direct:process3");

from("direct:process3").routeId("process3").process(exchange -> {
Customer customer = exchange.getIn().getBody(Customer.class);
String content = "Content:" + customer.toString();
exchange.getIn().setBody(content);
}).to("direct:write2File");

//Below updated
from("direct:write2File").routeId("write2File").to("file:/src/test/resources?fileName=test_out.log&fileExist=Append");

}
};
}

@Override
protected void doPostSetup() throws Exception {
context.getRouteDefinition("MY_ROUTE").adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
replaceFromWith("direct:teststart");
weaveAddLast().to("mock:end");
}
});
context.start();//updated
}

@Test
public void testUnmarshal() throws Exception {

template.sendBody("direct:teststart", new File("src/test/resources/test.txt"));


List<Customer> customers = mockProcess1.getExchanges().get(0).getIn().getBody(List.class);
System.out.println("customers:"+customers.size());

Customer customer1 = mockProcess2.getExchanges().get(0).getIn().getBody(Customer.class);
System.out.println("customer1:"+customers.toString());


String customer = mockProcess3.getExchanges().get(0).getIn().getBody(String.class);
System.out.println("customer:"+customer);

String customerString = mockWrite2File.getExchanges().get(0).getIn().getBody(String.class);
System.out.println("Customer String:"+customerString);

String customerFinal = mockEnd.getExchanges().get(0).getIn().getBody(String.class);
System.out.println("Customer String:"+customerFinal);


assertMockEndpointsSatisfied();
}

}

异常:已更新

引起:java.lang.ArrayIndexOutOfBoundsException:1 在 com.tgt.frs.collections.trm.ceds.routes.FileTest7$1.lambda$0(FileTest7.java:61) 在 org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63) 在 org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:541) 在 org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:198) 在 org.apache.camel.processor.Pipeline.process(Pipeline.java:120) 在 org.apache.camel.processor.Pipeline.process(Pipeline.java:83) 在 org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:198) 在 org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:62) 在 org.apache.camel.processor.SendProcessor.process(SendProcessor.java:145)

最佳答案

测试框架不会自动启动CamelContext,因为您正在使用AdviceWith,以允许您在路由启动之前向路由添加建议。添加建议后,在 doPostSetup 末尾添加对 startCamelContext() 的调用。

此外,append 似乎不是 file 组件的有效属性,因此您需要删除 append=true .

关于java - 测试Apache Camel Junit的所有进程和路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48869548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com