Apache Camel Bean单元测试

wbgh16ku  于 2022-11-07  发布在  Apache
关注(0)|答案(2)|浏览(201)

我正在我的应用程序中使用带有Sping Boot 的Apache Camel。目前我正在进行单元测试。
Java程式码

  • 数据路由类
from("direct:getData")
.routeId("getData")
.bean(DataService.class, "processData")
.marshal().json(JsonLibrary.Jackson)
.end();
  • 数据服务类
public Data processData() {
    return new Data("Hello World");
}
  • 带有getter、setter和JacksontoString方法的数据类
private String value;

单元测试

  • 基本驼峰上下文单位文本
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")

在单元测试中返回一个模拟数据对象,其默认字符串变量为“单元测试成功”,然后测试路由是否给予模拟对象,而不是返回带有“Hello World”字符串变量的对象?

wgeznvg7

wgeznvg71#

这似乎是一个迟来的回应,但我遇到了与您的案例中描述的相同的事情,我遇到了一个“模拟”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);
}
eoigrqb6

eoigrqb62#

您可以在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")

相关问题