Spring MVC と統合された外部 REST インターフェイスにデータを送信する単純な Camel ルートがあります。
@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 を使用した残りのエンドポイントの統合テストがあります。
@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 ですが、問題ないようです。私はCamelを初めて使用するので、テストを間違った方法で配線していると思います。提案をいただければ幸いです。