Apache Camel Beans 单元测试

4

我在我的应用程序中使用了Apache Camel和Spring Boot。目前,我正在进行单元测试。

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

    private String value;
    

单元测试

  • 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));
         }
      }
    

如何进行模拟测试

   .bean(DataService.class, "processData")

在单元测试中,返回一个模拟数据对象,其默认的字符串变量为"Unit test success"。然后测试一下,看看路由是否会返回模拟对象而不是带有"Hello World"字符串变量的对象?
2个回答

1

可能看起来有些晚了,但我也遇到了你描述的问题,并找到了一种简单的方法来“模拟”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);
}

0

您可以在您的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();
    }

}

因此,您可以像往常一样模拟DataService

另一种选择是在路由中使用beanRef("beanName", "methodName")


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接