首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Camel单元测试总是失败,我不能解释为什么expected是0,或者如何正确地断言expected

Camel单元测试总是失败,我不能解释为什么expected是0,或者如何正确地断言expected
EN

Stack Overflow用户
提问于 2021-07-12 18:38:03
回答 1查看 119关注 0票数 0

我让这个带有Apache Camel的rest端点查找来自不同来源的一些值,首先我验证请求,然后根据路径转到不同的路由:

代码语言:javascript
运行
复制
restConfiguration()
    .component("servlet")
    .bindingMode(RestBindingMode.json)
...

rest("/lookup/{path}")
    .consumes(MediaType.APPLICATION_JSON_VALUE)
    .produces(MediaType.APPLICATION_JSON_VALUE)
    .post().type(LookupRequest.class).outType(LookupResponse.class)
    .to("direct:validate-request")

from("direct:validate-request")
    .routeId(Constants.ROUTE_VALIDATE_REQUEST)
    .to("bean-validator://x")
    .process(exchange ->
        exchange.getIn().setHeader("lookupType", env.getProperty("lookup.path." + exchange.getIn().getHeader("path", String.class)))
    )
    .to("bean:lookupValidationService?method=validate(${body}, ${header.lookupType})")
    .toD("direct:db-lookup-${header.lookupType}");

from("direct:db-lookup-customer")
    .routeId(Constants.ROUTE_CUSTOMER_LOOKUP)
    .log(LoggingLevel.DEBUG, CamelRouter.CAMEL_BODY)
        ...
    .to("sql:{{lookup.queries.customer}}?placeholder=~&usePlaceholder=true")
    .process(new SqlQueryResultProcessor())
        ...
    .endRest();

这是我测试路由的尝试之一:

代码语言:javascript
运行
复制
@ActiveProfiles("test")
@CamelSpringBootTest
@SpringBootTest(classes = LookupServiceApplication.class)
@MockEndpointsAndSkip("log:.*")
@UseAdviceWith
class CameltestingApplicationTests {

    @Autowired CamelContext camelContext;
    @Autowired ProducerTemplate producerTemplate;

    @EndpointInject("mock:direct:db-lookup-customer")
    MockEndpoint customerMock;

    static List<Map<String, Object>> results = new ArrayList<Map<String, Object>>() {{
        add(new HashMap<String, Object>() {{
            put("customerName", "Test customer");
        }});
    }};

    @BeforeEach
    public void before() throws Exception {
        camelContext.setTracing(true);
        AdviceWith.adviceWith(camelContext, Constants.ROUTE_CUSTOMER_LOOKUP, routeBuilder -> {
            routeBuilder.interceptSendToEndpoint("sql:*").skipSendToOriginalEndpoint().process(e -> e.getIn().setBody(results));
        });
    }
    @Test
    public void testReceive() throws Exception {
        camelContext.start();
        // Expect:
        customerMock.expectedMessageCount(1);
        // And: the process to set the header
        customerMock.expectedHeaderReceived("lookupType", "customer.info");

        Map<String, Object> headers = new HashMap<>();
        headers.put("path", "customer.info");
        Map<String, Object> keywords = new HashMap<>();
        keywords.put("customerNumber", 1234678);
        LookupRequest request = new LookupRequest();
        request.setKeywords(keywords);

        // When we send the message
        producerTemplate.sendBodyAndHeaders("direct:validate-request", request, headers);
        // Everything is satisfied.
        Assert.assertNotNull(camelContext.hasEndpoint("direct:db-lookup-customer"));
        customerMock.assertIsSatisfied();
    }
}

这是日志:

代码语言:javascript
运行
复制
2021-07-12 12:10:13.604  INFO 660 --- [           main] o.a.c.i.e.AbstractCamelContext           : Apache Camel 3.11.0 (Lookup service) started in 1s246ms (build:160ms init:1s7ms start:79ms)
2021-07-12 12:10:13.623  INFO 660 --- [           main] o.a.c.Tracing                            : *--> [validate    ] [from[direct:validate-request]    ] Exchange[Id: 32F8D8FB9A7CF84-0000000000000000, BodyType: com.odfl.freight.lookupservice.model.LookupRequest, Body: LookupRequest(keywords={customerNumber=1234678})]
2021-07-12 12:10:13.624  INFO 660 --- [           main] o.a.c.Tracing                            :      [validate    ] [bean-validator://x               ] Exchange[Id: 32F8D8FB9A7CF84-0000000000000000, BodyType: com.odfl.freight.lookupservice.model.LookupRequest, Body: LookupRequest(keywords={customerNumber=1234678})]
2021-07-12 12:10:13.727  INFO 660 --- [           main] o.a.c.Tracing                            :      [validate    ] [Processor@0x166ddfb7             ] Exchange[Id: 32F8D8FB9A7CF84-0000000000000000, BodyType: com.odfl.freight.lookupservice.model.LookupRequest, Body: LookupRequest(keywords={customerNumber=1234678})]
2021-07-12 12:10:13.728  INFO 660 --- [           main] o.a.c.Tracing                            :      [validate    ] [bean:lookupValidationService?meth] Exchange[Id: 32F8D8FB9A7CF84-0000000000000000, BodyType: com.odfl.freight.lookupservice.model.LookupRequest, Body: LookupRequest(keywords={customerNumber=1234678})]
2021-07-12 12:10:13.733  INFO 660 --- [           main] o.a.c.Tracing                            :      [validate    ] [direct:db-lookup-${header.lookupT] Exchange[Id: 32F8D8FB9A7CF84-0000000000000000, BodyType: com.odfl.freight.lookupservice.model.LookupRequest, Body: LookupRequest(keywords={customerNumber=1234678})]
2021-07-12 12:10:13.739  INFO 660 --- [           main] o.a.c.Tracing                            : ---> [customer    ] [from[direct:db-lookup-customer]  ] Exchange[Id: 32F8D8FB9A7CF84-0000000000000000, BodyType: com.odfl.freight.lookupservice.model.LookupRequest, Body: LookupRequest(keywords={customerNumber=1234678})]
2021-07-12 12:10:13.740  INFO 660 --- [           main] o.a.c.Tracing                            :      [customer    ] [log                              ] Exchange[Id: 32F8D8FB9A7CF84-0000000000000000, BodyType: com.odfl.freight.lookupservice.model.LookupRequest, Body: LookupRequest(keywords={customerNumber=1234678})]
2021-07-12 12:10:13.740  INFO 660 --- [           main] o.a.c.Tracing                            :      [customer    ] [setHeader[customerNumber]        ] Exchange[Id: 32F8D8FB9A7CF84-0000000000000000, BodyType: com.odfl.freight.lookupservice.model.LookupRequest, Body: LookupRequest(keywords={customerNumber=1234678})]
2021-07-12 12:10:13.891  INFO 660 --- [           main] o.a.c.Tracing                            :      [customer    ] [sql:{{lookup.queries.customer}}?p] Exchange[Id: 32F8D8FB9A7CF84-0000000000000000, BodyType: com.odfl.freight.lookupservice.model.LookupRequest, Body: LookupRequest(keywords={customerNumber=1234678})]
2021-07-12 12:10:13.892  INFO 660 --- [           main] o.a.c.Tracing                            :      [customer    ] [Processor@0x1feb586d             ] Exchange[Id: 32F8D8FB9A7CF84-0000000000000000, BodyType: com.odfl.freight.lookupservice.model.LookupRequest, Body: LookupRequest(keywords={customerNumber=1234678})]
2021-07-12 12:10:13.895  INFO 660 --- [           main] o.a.c.Tracing                            :      [customer    ] [Processor@0x34695b23             ] Exchange[Id: 32F8D8FB9A7CF84-0000000000000000, BodyType: null, Body: [{customerName=Test customer}]]
2021-07-12 12:10:13.896  INFO 660 --- [           main] o.a.c.Tracing                            :      [customer    ] [log                              ] Exchange[Id: 32F8D8FB9A7CF84-0000000000000000, BodyType: com.odfl.freight.lookupservice.model.LookupResponse, Body: LookupResponse(ok=true, errorMessage=null, results=[{customerName=Test customer}])]
2021-07-12 12:10:13.896  INFO 660 --- [           main] o.a.c.Tracing                            : <--- [customer    ] [from[direct://db-lookup-customer]] Exchange[Id: 32F8D8FB9A7CF84-0000000000000000, BodyType: com.odfl.freight.lookupservice.model.LookupResponse, Body: LookupResponse(ok=true, errorMessage=null, results=[{customerName=Test customer}])]
2021-07-12 12:10:13.897  INFO 660 --- [           main] o.a.c.Tracing                            : *<-- [validate    ] [from[direct://validate-request]  ] Exchange[Id: 32F8D8FB9A7CF84-0000000000000000, BodyType: com.odfl.freight.lookupservice.model.LookupResponse, Body: LookupResponse(ok=true, errorMessage=null, results=[{customerName=Test customer}])]
2021-07-12 12:10:13.900  INFO 660 --- [           main] o.a.c.c.m.MockEndpoint                   : Asserting: mock://direct:db-lookup-customer is satisfied

...

java.lang.AssertionError: mock://direct:db-lookup-customer Received message count 0, expected at least 1

    at org.apache.camel.component.mock.MockEndpoint.fail(MockEndpoint.java:1790)
    at org.apache.camel.component.mock.MockEndpoint.assertTrue(MockEndpoint.java:1773)

我是测试Apache Camel routes的新手,有人能帮助我理解问题出在哪里,或者如何验证输出,因为您可以看到交换对象主体在那里并传递了。我已经尝试了一些方法来模拟这个端点,但似乎都不起作用,我也没有想法了。

EN

回答 1

Stack Overflow用户

发布于 2021-07-13 23:14:02

您的@MockEndpointsAndSkip("log:.*")只模拟了log端点,并将customerMock注入为mock:direct:db-lookup-customer,但是如果它不在路由中,它如何接收消息呢?

我认为您所需要做的就是以某种方式模拟您的direct:db-lookup-customer,例如在adviceWith中或使用注释

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68345896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档