简体   繁体   English

java.lang.AssertionError:骆驼模拟端点问题

[英]java.lang.AssertionError: Issue with Camel Mock Endpoint

I'm having issue with camel route mocking.我遇到了骆驼路线 mocking 的问题。 It was not able to count the route messages instead its returning "0" message count.它无法计算路由消息,而是返回“0”消息计数。 Here is my test case and route.这是我的测试用例和路线。 Can someone assist me with this?有人可以帮助我吗?

WatcherRouteTest.java WatcherRouteTest.java

@CamelSpringBootTest
@SpringBootTest
@MockEndpoints
@UseAdviceWith
public class RouteTest {

  @Autowired
  CamelContext camelContext;

  @Autowired
  ProducerTemplate producerTemplate;

  File resultFile;

  @EndpointInject("mock:azure-storage-blob")
  MockEndpoint storageMockEndpoint;


  @Test
  void testFlow() throws Exception {

    AdviceWith.adviceWith(camelContext, "uploadFile", a ->
        a.replaceFromWith("direct:start")
    );

    storageMockEndpoint.expectedMessageCount(1);
    storageMockEndpoint.message(0)
        .header(Exchange.FILE_NAME).isEqualTo("text.csv");

    InputStream body = fetchFileFromResourcesFolderAsStream("test-files/text.csv");
    Map<String, Object> headers = new HashMap<>();
    headers.put(Exchange.FILE_NAME, "text.csv");
    headers.put("CamelAzureStorageBlobContainerName","abc");
    headers.put("header.CamelAzureStorageBlobSourceBlobAccountName","abcd");

    camelContext.start();
    producerTemplate.sendBodyAndHeaders("direct:start", body, headers);

    storageMockEndpoint.assertIsSatisfied();

    resultFile = fileCreationUtility( "text.csv");
    assertTrue(resultFile.exists());

  }
}

Here is my actual implementation:这是我的实际实现:

from("direct:upload")
        .routeId("uploadFile")
        .setHeader("CamelAzureStorageBlobSourceBlobAccountName",constant(storageAccountName))
        .toD(BLOB_URL)
        .log(LoggingLevel.INFO,"${header.CamelFileName} Uploaded to ${header.CamelAzureStorageBlobContainerName} Container Successfully")
        .end();

Exception Occurred:发生异常:

java.lang.AssertionError: mock://azure-storage-blob Received message count. Expected: <1> but was: <0>
Expected :<1>
Actual   :<0> 

On Workaround try with following solution在解决方法上尝试使用以下解决方案

Solution 1): And You should setup the expectations on the mock before you send messages into Camel.解决方案 1):并且您应该在将消息发送到 Camel 之前在mock上设置期望。 The steps is步骤是

1. setup expectations on the mocks 1. 设置对模拟的期望

2. send messages 2.发送消息

3. assert 3.断言

Example:例子:

@Test public void testDozenMsgsOrderByIntegerBody() throws Exception {
    // fail();
    List<Integer> input = Arrays.asList(new Integer[] {12, 11, 10, 9, 8, 7, 6, 5, 1, 2, 3, 4});
    List<Integer> expectedOutput = new ArrayList<Integer>();
    for (int i=1; i<=12; i++) {expectedOutput.add(i);};
    MockEndpoint resultEndpoint = context.getEndpoint("mock:result", MockEndpoint.class);
    resultEndpoint.expectedBodiesReceived(expectedOutput);
    for (Integer i : input) {template.sendBody("direct:start", i);};
    resultEndpoint.assertIsSatisfied();
}

class OrderingTestRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
    from("direct:start")
       .resequence(body(Integer.class))
       .to("mock:result");
    }
}

For more details refer this SO Thread :有关更多详细信息,请参阅此SO 线程

Solution 2) The AssertionError complains that the two object instances are not equal what is obvious because they are not the same instance.解决方案 2) AssertionError抱怨两个 object 实例不相等,这是显而易见的,因为它们不是同一个实例。

One of them is created in your application, the other is created in your test case.其中一个是在您的应用程序中创建的,另一个是在您的测试用例中创建的。

And also Error may be due to the request body in example:HTML template.并且错误可能是由于示例中的请求正文:HTML 模板。 Trying to assert it with a wrong HTML template it throws the AssertionError error尝试使用错误的 HTML 模板断言它会引发AssertionError错误

For more details refer this SO Thread :有关更多详细信息,请参阅此SO 线程

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

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