我試圖測試使用from(x).to(y).log("SuccessKey123")
和的駱駝路由。阿帕奇駱駝 - 測試日誌消息
如何在消息成功處理時測試Camel日誌「SuccessKey123」,或者在拋出HttpOperationFailedException時記錄「ErrorKey123」?
我RouteBuilder():
@Component
public class myHttp4RouteBuilder extends SpringRouteBuilder {
public static final ID = "foo";
@Override
public void configure() throws Exception {
onException(HttpOperationFailedException.class)
.log("ErrorKey123")
.to(ERROR_QUEUE)
.handled(true);
from(AWS_SQS_ENDPOINT)
.to(REST_API_ENDPOINT)
.log("SuccessKey123");
}
}
的TestClass:
public class myHttp4RouteBuilderTest {
@Produce(uri = MOCK_ROUTE_FROM)
protected ProducerTemplate template;
@EndpointInject(uri = MOCK_ROUTE_TO)
private MockEndpoint mockEndpoint;
@Autowired
private CamelContext camelContext;
@Before
public void setup() throws Exception{
RouteDefinition rd = camelContext.getRouteDefinition(myHttp4RouteBuilder.ID);
rd.adviceWith(camelContext, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
replaceFromWith(MOCK_ROUTE_FROM);
weaveByToUri(ERROR_QUEUE)
.replace()
.to(MOCK_ROUTE_TO);
}
});
}
@Test
@DirtiesContext
public void testSuccess() throws Exception {
// throw an HttpOperationFailedException
mockEndpoint.whenAnyExchangeReceived(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
throw new HttpOperationFailedException("Exception", 400, null, null, null, null);
}
});
//
//
// How can I test here that camel logs "ErrorKey123"
//
//
template.sendBody(MOCK_ROUTE_FROM, "some content");
mockEndpoint.assertIsSatisfied();
}
}
非常感謝您!
也看到這樣的回答:https://stackoverflow.com/questions/1827677/how-to-do-a-junit-assert-on-a-message-in-a-logger –