我有一个简单的 Camel 路由,可以将数据发送到与 Spring MVC 集成的外部 REST 接口。
@RestController
public class MyController {
@Autowired
private camelService camelService;
@RequestMapping(method = RequestMethod.POST, value = "/test")
@ResponseStatus(HttpStatus.CREATED)
public TestModel createEV(@Valid @RequestBody TestModel testModel) {
camelService.publishTestCase(testModel);
return testModel;
}
}
@Service
public class CamelService {
@Produce(uri = "direct:test")
private ProducerTemplate producerTemplate;
@Override
public void publishTestCase(TestModel testModel) {
producerTemplate.sendBody(testModel);
}
}
@Component
public class TestRouter extends SpringRouteBuilder
@Override
public void configure() throws Exception {
errorHandler(loggingErrorHandler(log).level(LoggingLevel.ERROR));
from("direct:test")
.routeId("testRoute")
.beanRef("mapper", "map")
.to("velocity:test.vm")
.setHeader(Exchange.HTTP_METHOD, simple("POST"))
.to("log:test?level=DEBUG&showAll=true&multiline=true&maxChars=100000")
.to("cxfrs:http://url.here");
}
}
然后使用 mockMvc 对 rest 端点进行集成测试,以模拟外部 camel 端点。
@ContextConfiguration(loader = SpringApplicationContextLoader, classes = Application)
@WebIntegrationTest
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class MyControllerIntegrationTest extends Specification {
@Autowired
private CamelContext camelContext
MockMvc mockMvc
@Autowired
WebApplicationContext wac
@Shared
def setupOnceRun = false
@Shared
def validInput = new File(JSON_DIR + '/valid.json').text
def setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build()
// no way to use setupSpec as Autowired fields can't be Shared
if (!setupOnceRun) {
ModelCamelContext mcc = camelContext.adapt(ModelCamelContext);
camelContext.getRouteDefinition("testRoute).adviceWith(mcc, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
mockEndpointsAndSkip("cxfrs:http://url.here")
}
});
setupOnceRun = true
}
}
def 'valid scenario'() {
setup:
MockEndpoint mockEndpoint = (MockEndpoint) camelContext.hasEndpoint("mock:cxfrs:http://url.here")
mockEndpoint.expectedMessageCount(1)
when:
def response = mockMvc.perform(post('/test').content(validInput)).andReturn()
then:
response.getResponse().getStatus() == 201
mockEndpoint.assertIsSatisfied()
}
}
如果你自己运行它,测试就会通过,但是一旦它与其他集成测试一起包含在构建中,它就会不断产生 OutOfMemory。MaxPermSize 是 4G,看起来这不是问题。我是骆驼的新手,所以我的猜测是我以错误的方式连接测试。将不胜感激任何建议。