我正试图在我的单元测试中模拟我的端点。我正在使用Camel Enhanced Spring Test。
这是我的单元课:
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("development")
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, FirstbirdTestExecutionListener.class,
FlywayTestExecutionListener.class })
@ContextConfiguration(locations = { "classpath:hibernate-spring.xml", "classpath:broker.xml",
"classpath:camel/camel-commons.xml",
"classpath:camel/firstbird-application-consumer.xml", })
@FlywayTest
@MockEndpoints("bean:applicationResponseProcessor")
@DisableJmx(false)
public class FirstBirdApplicationsTest extends AbstractJUnit4SpringContextTests {
private static final String resourceUri = "src/test/resources/data/firstbird/";
@Autowired
@Qualifier("context-firstbird-applications-consumer")
protected ModelCamelContext camelContext;
@Produce(uri = "direct:firstbird.application.service")
private ProducerTemplate in;
@EndpointInject(uri = "mock:out")
private MockEndpoint mockOut;
@EndpointInject(uri = "mock:bean:applicationResponseProcessor", context = "context-firstbird-applications-consumer")
private MockEndpoint mock;
@BeforeClass
public static void setup() {
FirstbirdJsonHelper.setResourceUri(resourceUri);
}
@Test
public void inComingApplicationsTest() throws Exception {
assertEquals(ServiceStatus.Started, camelContext.getStatus());
// not wirking!
camelContext.setTracing(true);
DefaultExchange exchange = new DefaultExchange(camelContext);
mock.expectedMessageCount(1);
mockOut.expectedMessageCount(1);
// not working
MockEndpoint test = MockEndpoint.resolve(camelContext, "mock:bean:applicationResponseProcessor");
camelContext.getRouteDefinition("firstbird-incomming-applications").adviceWith(camelContext,
new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
mockEndpoints("bean:applicationResponseProcessor");
}
});
exchange.getIn().setBody("GoGo Gadget Route me!");
in.send(exchange);
// Thread.sleep(4000);
// not working
List<Exchange> receivedExchanges = mock.getReceivedExchanges();
ApplicationResponse applicationResponse = (ApplicationResponse) receivedExchanges.get(0).getIn().getBody();
// working
mockOut.assertIsSatisfied();
}
}
这是相应的路线:
<camelContext errorHandlerRef="loggingErrorHandler" id="context-firstbird-applications-consumer" trace="true" xmlns="http://camel.apache.org/schema/spring">
<route id="firstbird-incomming-applications">
<from id="firstbird-ws-producer" uri="{{firstbird.applications.service}}"/>
<log logName="firstbird.data.consumer.firstbird-ws-producer" loggingLevel="INFO" message="Send data to queue"/>
<setExchangePattern pattern="InOnly"/>
<recipientList id="webservice-recipientlist" ignoreInvalidEndpoints="true" stopOnException="false">
<simple>activemq:${header.operationName}</simple>
</recipientList>
<to uri="bean:applicationResponseProcessor"/>
<to uri="mock:out"/>
</route>
</camelContext>
基本上我想模拟 bean:applicationResponseProcessor 因此我创建了我的MockEndpoint mock 。问题是它似乎没有注入。我仍然可以在我的applicationResponseProcessor bean中调试。
唯一有效的解决方案是使用AdviceWith,如下所示:
camelContext.getRouteDefinition("firstbird-incomming-applications").adviceWith(camelContext, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
mockEndpoints("bean:applicationResponseProcessor");
}
});
我遇到的另一个问题是在JUnit Test中启用跟踪。通过
启用跟踪camelContext.setTracing(true);
不起作用。我必须在我的XML文件中明确设置跟踪。