当我开始测试路由时,我的真实端点正确接收消息,但是MockEnpoint对象不会增加receivedCount并且测试失败并且:
java.lang.AssertionError:mock:// direct:test.ts.api.location已收到 消息计数。预期:< 1>但是:< 0>
路线:
<route id="test_to_ts" autoStartup="true">
<from uri="activemq:test.ts" />
<choice>
<when>
<simple>${header.type} == 'location'</simple>
<to uri="direct:test.ts.api.location" />
</when>
...
<choice>
</route>
这是我的测试课程:
@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "file:src/main/config/context.xml", loader= TestContextLoader.class)
@ShutdownTimeout(value = 30, timeUnit = TimeUnit.SECONDS)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints(value = "direct:test.ts.api.location")
public class RouteTest {
@EndpointInject(uri = "mock:direct:test.ts.api.location")
private MockEndpoint locationEndpont;
@Produce(uri = "activemq:test.ts")
private ProducerTemplate template;
@Test
public void testSendMessageTypeLocation() throws Exception {
String strBody = "body";
locationEndpont.setExpectedMessageCount(1);
locationEndpont.setResultWaitTime(10000);
template.sendBodyAndHeader("activemq:test.ts", strBody, "type", "location");
locationEndpont.assertIsSatisfied();
}
它看起来像我的模拟没有与端点链接,但我看不出原因。
春季版:3.2.11 骆驼版:2.13.3
UPD1:我删除了注释并使RouteTest扩展了CamelSpringTestSupport并覆盖了createApplicationContext(),isMockEndpoints()方法 - 这是有效的。 仍然不明白我的代码中带注释的问题
UPD2:更改后的代码:
public abstract class AbstractRouteTest extends CamelSpringTestSupport {
protected String[] routesToStart;
protected String[] routeMocksPattern;
protected long result_wait_time = 1000;
protected long assert_period_time = 100;
@Before
public void setUp() throws Exception {
super.setUp();
}
@Override
public String isMockEndpoints() {
boolean first = true;
StringBuilder sb = new StringBuilder();
sb.append("(");
for (String mock : routeMocksPattern) {
if (first) sb.append("|");
sb.append(mock);
}
sb.append(")");
return sb.toString();
}
@Override
protected void startCamelContext() throws Exception {
context().setAutoStartup(false);
super.startCamelContext();
for (String routeId : routesToStart) {
context().startRoute(routeId);
}
}
@Override
public boolean isCreateCamelContextPerClass() {
return true;
}
protected abstract AbstractApplicationContext createApplicationContext();
}
}
public class RouteTest extends AbstractRouteTest {
private static final String CONTEXT_PATH = "src/test/resources/messages_test_to_ts/spring-test-context.xml";
private static final String START_ENDPOINT = "activemq:test.ts";
@Produce(uri = START_ENDPOINT)
private ProducerTemplate template;
@EndpointInject(uri = "mock:logdb:test")
private MockEndpoint logdbEndpoint;
@EndpointInject(uri = "mock:direct:test.ts.api.location")
private MockEndpoint locationEndpoint;
public MdmToTSLocationRouteTest() {
routesToStart = new String[] {"test_to_ts", "test_ts_api_location", "log_the_error"};
routeMocksPattern = new String[]
{"(logdb:test.*sendToTS.*TEST_TO_TS.*(success).*)", "direct:test.ts.api.location"};
}
@Override
public void setUp() throws Exception {
super.setUp();
locationEndpoint.setResultWaitTime(result_wait_time);
logdbEndpoint.setResultWaitTime(result_wait_time);
locationEndpoint.setAssertPeriod(assert_period_time);
logdbEndpoint.setAssertPeriod(assert_period_time);
}
@Test
public void testSendLocationMessage() throws IOException, InterruptedException, SQLException {
String body = TestUtil.readFile("src/test/resources/messages_test_to_ts/location.xml", StandardCharsets.UTF_8);
String expectedBody = TestUtil.readFile("src/test/resources/messages_test_to_ts/location_unmarshal.xml", StandardCharsets.UTF_8).replaceAll("\\r", "");
// set expectations
locationEndpoint.expectedHeaderReceived("type", "location");
locationEndpoint.expectedBodiesReceived(body);
locationEndpoint.setExpectedMessageCount(1);
logdbEndpoint.expectedHeaderReceived("type", "location");
logdbEndpoint.expectedBodiesReceived(expectedBody);
logdbEndpoint.setExpectedMessageCount(1);
// send message
template.sendBodyAndHeader(body, "type", "location");
// assert
locationEndpoint.assertIsSatisfied();
logdbEndpoint.assertIsSatisfied();
}
@Override
protected AbstractApplicationContext createApplicationContext() {
AbstractApplicationContext context = new TestContextLoader(CONTEXT_PATH).createApplicationContext();
LocationUploader process = (LocationUploader) context.getBean("locationUploadBean");
OracleDataSource oraDSMock = Mockito.mock(OracleDataSource.class);
WrapperManager wrapperManagerMock = Mockito.mock(WrapperManager.class);
Statement stmtMock = Mockito.mock(Statement.class);
OracleCallableStatement oraCSMock = Mockito.mock(OracleCallableStatement.class);
Wrapper wrapperMock = Mockito.mock(Wrapper.class);
Connection connMock = Mockito.mock(Connection.class);
try {
Mockito.when(oraDSMock.getConnection()).thenReturn(connMock);
Mockito.when(connMock.createStatement()).thenReturn(stmtMock);
Mockito.when(connMock.prepareCall(Mockito.anyString())).thenReturn(oraCSMock);
Mockito.when(wrapperManagerMock.getWrapper(java.util.List.class, "ORA_SCHEME.T_TEST_LOCATION_TAB")).thenReturn(wrapperMock);
} catch (SQLException e) {
throw new RuntimeException(e);
}
process.setDataSource(oraDSMock);
process.setManager(wrapperManagerMock);
return context;
}
}