我的 RouteBuilder 类
@Component
public class MyRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
from("direct:start")
.to("mock:result"); // Or whatever the route should do
}
}
@ExtendWith(SpringExtension.class)
@CamelSpringBootTest
@SpringBootTest
@MockEndpoints // Mock all endpoints by default if needed
public class CamelRouteTest {
@Autowired
private CamelContext camelContext;
@BeforeEach
public void setUp() throws Exception {
// Assuming we have set autoStartup=false in application properties or configuration
// Use adviceWith to mock or intercept endpoints before the context starts
camelContext.getRouteDefinitions().forEach(routeDefinition -> {
try {
AdviceWith.adviceWith(routeDefinition, camelContext, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
// Perform advice logic here
// For example, intercept sending to an endpoint and do something else
interceptSendToEndpoint("direct:endpoint")
.skipSendToOriginalEndpoint()
.to("mock:result");
}
});
} catch (Exception e) {
throw new RuntimeException(e);
}
});
// Start Camel context manually after adviceWith configuration
camelContext.start();
}
@AfterEach
public void tearDown() throws Exception {
// Properly stop the Camel context after each test
if (camelContext != null) {
camelContext.stop();
}
}
我正在将camel 3.2.x升级到3.14.x。 Springboot 但测试抛出 RouteDefinition 必须指定错误! 调试测试显示camel已经启动并且没有添加任何路由!!
首先:如果您使用 AdviceWith,则必须指定在建议路由之前不启动 CamelContext。您可以通过以下方式做到这一点:
@UseAdviceWith
public class CamelRouteTest {
第二:我的@BeforeEach看起来像这样:(你必须指定你要修改的routeID)
@BeforeEach
public void setup() throws Exception {
AdviceWith.adviceWith(camelContext, "import_csv_frische_invoice",
// intercepting (amend, change the route to our needs)
r -> {
r.weaveByToUri("xslt-saxon:*").replace().to("xslt-saxon:import/stylesheet/frische/csv_to_xml_invoice.xsl");
r.weaveByToUri("direct:processInvoices").replace().to("mock:result"); // Removing the further processing and replace it with something which we can mock
});
camelContext.start();
}
第三:不需要@ExtendWith(SpringExtension.class)
第四:我有几个测试类:每个测试类都使用AdviceWith。但如果不使用@DirtiesContext,我就无法让它们运行。也许你有一个想法。要解决的问题是,每个使用@UseAdviceWith的TestClass首先要执行setup方法,当所有Testclass的setup方法完成后,camelcontext就可以启动了。让我知道也许您知道如何以优雅的方式解决这个问题。