简体   繁体   中英

java.lang.AssertionError: Issue with Camel Mock Endpoint

I'm having issue with camel route mocking. It was not able to count the route messages instead its returning "0" message count. Here is my test case and route. Can someone assist me with this?

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. The steps is

1. setup expectations on the mocks

2. send messages

3. assert

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 :

Solution 2) The AssertionError complains that the two object instances are not equal what is obvious because they are not the same instance.

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. Trying to assert it with a wrong HTML template it throws the AssertionError error

For more details refer this SO Thread :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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