在单元测试camel中获取body

5t7ly7z5  于 2023-04-06  发布在  Apache
关注(0)|答案(1)|浏览(234)

我试图用camel对我的一个路由执行单元测试,但是我找不到一种方法来检查从我的路由接收到的主体,并检查它是否是预期的。我如何使用或不使用模拟来执行此操作?
路线

@Component
public class MyRoute extends RouteBuilder {    

    @Override public void configure() throws Exception {        
        from("direct:MyRoute")                
        .routeId("MyRoute")                
        .marshal().json(JsonLibrary.Jackson)                
        .log("Marshal to \n${body}");    
    }
}

测试

public class MyRouteTest extends CamelTestSupport {    
    @Override 
    protected RouteBuilder createRouteBuilder() throws Exception {    
        return new MyRoute();    
    }    

    @Test
    public void testRoute() throws Exception {
        NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create();        
        Map<String,Object> map = new LinkedHashMap<>();        
        map.put("name","your_name"); 
        template.sendBody("direct:MyRoute", map);     
        assertTrue(notify.matchesWaitTime());
   }
}
5uzkadbs

5uzkadbs1#

使用mocks

您可以使用AdviceWithweaveAddLastmock:result添加到路由的末尾。然后使用注入的MockEndpoint为交换定义Assert。MockEndpoint提供了许多方便的方法,您可以使用它们来检查交换的有效性。
当使用AdviceWith修改测试路由时,请记住使用@UseAdviceWith注解,并在每次测试之前手动启动CamelContext。这是为了避免不必要的建议路由重新启动。

package com.example;

import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.AdviceWith;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
import org.apache.camel.test.spring.junit5.UseAdviceWith;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@CamelSpringBootTest
@UseAdviceWith
public class MySpringBootApplicationTest  {

    @Autowired
    private CamelContext camelContext;

    @Autowired
    private ProducerTemplate producerTemplate;

    @EndpointInject(uri = "mock:result")
    private MockEndpoint resulMockEndpoint;

    @Test
    public void test() throws Exception {
        
        resulMockEndpoint.expectedMessageCount(1);
        resulMockEndpoint.message(0)
            .body().isEqualTo("Hello world");

        addMockEndpointAsLast("hello");

        camelContext.start();
        producerTemplate.sendBody("direct:hello", "world");

        resulMockEndpoint.assertIsSatisfied();
    }

    MockEndpoint addMockEndpointAsLast(String routeId) throws Exception {

        AdviceWith.adviceWith(camelContext, routeId, builder -> {
            builder.weaveAddLast()
                .to("mock:result")
            ;
        });
        return resulMockEndpoint;
    }
}

如果你想进一步检查交换,你可以从MockEndpoint的交换列表中获取它。下面是如何以String的形式获取第一个交换的主体的示例。

String result = resulMockEndpoint.getExchanges().get(0)
    .getMessage().getBody(String.class);

无mocks

您可以使用ProducerTemplate.send方法返回结果交换。然而,该方法希望您提供可以使用DefaultExchange类创建的交换对象,而不是任何类型的主体。

package com.example;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.support.DefaultExchange;
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@CamelSpringBootTest
public class MySpringBootApplicationTest  {

    @Autowired
    private CamelContext camelContext;

    @Autowired
    private ProducerTemplate producerTemplate;

    @Test
    void testWithoutMock(){

        Exchange exchange = new DefaultExchange(camelContext);
        exchange.getMessage().setBody("world");

        Exchange resultExchange = producerTemplate.send("direct:hello", exchange);
        assertEquals("Hello world", resultExchange.getMessage().getBody());
    }
}

RouteBuilder

package com.example;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class MySpringBootRouter extends RouteBuilder {

    @Override
    public void configure() {

        from("direct:hello")
            .routeId("hello")
            .setBody().simple("Hello ${body}")
            .log("${body}")
        ;
    }
}

示例使用Apache Camel版本3.20.2

相关问题