294.4. 단위 테스트 Camel 경로
서비스 구성 요소는 POJO이며 (OSGi가 아닌) 단위 테스트에 대한 특별한 요구 사항이 없습니다. 그러나 Camel SCR에 특정한 몇 가지 기술이 있거나 보다 쉽게 테스트를 수행할 수 있습니다.
다음은 camel-archetype-scr 에 의해 생성된 단위 테스트 예제입니다.
// This file was generated from org.apache.camel.archetypes/camel-archetype-scr/2.15-SNAPSHOT
package example;
import java.util.List;
import org.apache.camel.scr.internal.ScrHelper;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.component.mock.MockComponent;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.model.RouteDefinition;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class CamelScrExampleTest {
Logger log = LoggerFactory.getLogger(getClass());
@Rule
public TestName testName = new TestName();
CamelScrExample integration;
ModelCamelContext context;
@Before
public void setUp() throws Exception {
log.info("*******************************************************************");
log.info("Test: " + testName.getMethodName());
log.info("*******************************************************************");
// Set property prefix for unit testing
System.setProperty(CamelScrExample.PROPERTY_PREFIX, "unit");
// Prepare the integration
integration = new CamelScrExample();
integration.prepare(null, ScrHelper.getScrProperties(integration.getClass().getName()));
context = integration.getContext();
// Disable JMX for test
context.disableJMX();
// Fake a component for test
context.addComponent("amq", new MockComponent());
}
@After
public void tearDown() throws Exception {
integration.stop();
}
@Test
public void testRoutes() throws Exception {
// Adjust routes
List<RouteDefinition> routes = context.getRouteDefinitions();
routes.get(0).adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
// Replace "from" endpoint with direct:start
replaceFromWith("direct:start");
// Mock and skip result endpoint
mockEndpoints("log:*");
}
});
MockEndpoint resultEndpoint = context.getEndpoint("mock:log:foo", MockEndpoint.class);
// resultEndpoint.expectedMessageCount(1); // If you want to just check the number of messages
resultEndpoint.expectedBodiesReceived("hello"); // If you want to check the contents
// Start the integration
integration.run();
// Send the test message
context.createProducerTemplate().sendBody("direct:start", "hello");
resultEndpoint.assertIsSatisfied();
}
}
이제, 흥미로운 비트를 하나씩 살펴보겠습니다.
속성 접두사 사용
// Set property prefix for unit testing
System.setProperty(CamelScrExample.PROPERTY_PREFIX, "unit");
이를 통해 속성의 앞에 "unit"을 추가하여 구성 부분을 덮어쓸 수 있습니다. 예를 들어 unit.from 은 단위 테스트를 위해 에서 재정의됩니다.
경로를 실행할 수 있는 런타임 환경 간의 차이점을 처리하는 데 접두사를 사용할 수 있습니다. 개발, 테스트 및 프로덕션 환경을 통해 변경되지 않은 번들을 이동하는 것이 일반적인 사용 사례입니다.
주석에서 테스트 구성 가져오기
integration.prepare(null, ScrHelper.getScrProperties(integration.getClass().getName()));
여기서는 OSGi 환경에서 사용되는 것과 동일한 속성을 사용하여 테스트에서 서비스 구성 요소를 구성합니다.
테스트를 위한 주요 구성 요소
// Fake a component for test
context.addComponent("amq", new MockComponent());테스트에서 사용할 수 없는 구성 요소는 다음과 같이 감소하여 경로를 시작할 수 있습니다.
테스트를 위해 경로 조정
// Adjust routes
List<RouteDefinition> routes = context.getRouteDefinitions();
routes.get(0).adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
// Replace "from" endpoint with direct:start
replaceFromWith("direct:start");
// Mock and skip result endpoint
mockEndpoints("log:*");
}
});Camel의 AdviceWith 기능을 사용하면 테스트를 위해 경로를 수정할 수 있습니다.
경로 시작
// Start the integration
integration.run();여기에서 서비스 구성 요소를 시작하고 해당 경로와 함께 라우팅을 시작합니다.
테스트 메시지 보내기
// Send the test message
context.createProducerTemplate().sendBody("direct:start", "hello");여기에서 우리는 테스트의 경로에 메시지를 보냅니다.