简体   繁体   English

Apache Camel Beans 单元测试

[英]Apache Camel Beans Unit Testing

I am using Apache Camel with Spring boot in my application.我在我的应用程序中使用 Apache Camel 和 Spring Boot。 Currently I am working on a Unit test.目前我正在进行单元测试。

Java Code Java 代码

  • DataRoute class数据路由类

    from("direct:getData") .routeId("getData") .bean(DataService.class, "processData") .marshal().json(JsonLibrary.Jackson) .end();
  • DataService class数据服务类

    public Data processData() { return new Data("Hello World"); }
  • Data Class with getters, setters and Jackson toString method具有 getter、setter 和 Jackson toString 方法的数据类

    private String value;

Unit test单元测试

  • BaseCamelContextUnitText BaseCamelContextUnitText

     public abstract class BaseCamelContextUnitTest extends CamelTestSupport { @Autowired private DataService dataService; @Produce private ProducerTemplate producerTemplate; public CamelContext getCamelContext() { return camelContext; } @Override protected Context createJndiContext() throws Exception { JndiContext context = new JndiContext(); context.bind("dataService", dataService); return context; } @Test public void shouldProcessData() throws Exception { RouteDefinition routeDef = getCamelContext().getRouteDefinition("getData"); routeDef.adviceWith((ModelCamelContext) getCamelContext(), new RouteBuilder() { @Override public void configure() throws Exception { from("direct:getData") .pipeline("bean:dataService?method=processData"); } }); getCamelContext().start(); String responseData = "{" + "\"value\":\"Unit test success\"" + "}"; Object response = producerTemplate.sendBody("direct:getData", ExchangePattern.InOut, null); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ((InputStreamCache) response).writeTo(byteArrayOutputStream); assertThat(new String(byteArrayOutputStream.toByteArray()), is(responseData)); } }

How do I mock我如何嘲笑

   .bean(DataService.class, "processData")

in the unit test to return a mock Data Object with its default String variable as say "Unit test success" and then test to see if the route would give back the mocked Object instead of the Object with "Hello World" String variable?在单元测试中返回一个带有默认字符串变量的模拟数据对象,例如“单元测试成功”,然后测试该路由是否会返回模拟对象而不是带有“Hello World”字符串变量的对象?

You can autowired the DataService in your DataRoute like您可以在DataRoute中自动装配DataService ,例如

@Component
public class DataRoute extends RouteBuilder {

    @Autowired
    private DataService dataService;

    @Override
    public void configure() throws Exception {
        from("direct:getData")
        .routeId("getData")
        .bean(dataService, "processData")
        .marshal().json(JsonLibrary.Jackson)
        .end();
    }

}

So you can mock the DataService as usual.因此,您可以像往常一样模拟DataService

An alternative is using beanRef("beanName", "methodName") in the Route.另一种方法是在路由中使用beanRef("beanName", "methodName")

This may seem's a late response, but I faced the same thing as described in your case, and I come across a simple way to "mock" the bean step, by using the DefaultRegistry to register the mocked bean into Camel registry, for example :这似乎是一个迟到的响应,但我遇到了与您的案例中描述的相同的事情,并且我遇到了一种“模拟”bean 步骤的简单方法,即使用DefaultRegistry将模拟的 bean 注册到 Camel 注册表中,例如:

@Test
 public void shouldProcessData() throws Exception {
   ...
   ...
   DataService dataService = new DataService(...);
   stubBean("dataService", dataService); // put this before calling context.start();

   context.start();
   ...
   ...
}


/**
 * This method inject a stub bean into the registry
 * @param beanName the name of the bean being injected
 * @param instance the stub instance
 */
void stubBean(String beanName, Object instance) {
    DefaultRegistry registry = context.getRegistry(DefaultRegistry.class);
    registry.bind(beanName, instance);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM