0

Following is the error which I am getting -

java.lang.AssertionError: mock://send Body of message: 0. Expected: <notification.scheduler.model.email.EmailNotificationRequest@943700a4> but was: <notification.scheduler.model.email.EmailNotificationRequest@c730128e>
Expected :<notification.scheduler.model.email.EmailNotificationRequest@943700a4> 
Actual   :<notification.scheduler.model.email.EmailNotificationRequest@c730128e>

The configuration of my class is as follows -

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = CustomerNotificationSchedulerApplication.class)
public class PrepareEmailReminderBodyRouteTest {


    @Autowired
    private CamelContext camelContext;

    @Produce(uri = "direct:prepareEmailReminder")
    private ProducerTemplate producer;

    @EndpointInject(uri = "mock:send")
    private MockEndpoint mockSendEndpoint;

    @Autowired
    private EmailNotificationRequestConfiguration emailNotificationRequestConfiguration;

    @Before
    public void before() throws Exception {
        camelContext.getRouteDefinition("prepareEmailReminderRoute").adviceWith(camelContext, new AdviceWithRouteBuilder() {
            @Override
            public void configure() throws Exception {
                interceptSendToEndpoint("direct:sendEmail")
                        .skipSendToOriginalEndpoint()
                        .to(mockSendEndpoint);
            }
        });
    }

And this is the failing test case that is failing at mockSendEndpoint.expectedBodiesReceived(request); -

@Test
@DirtiesContext
public void whenOneRevisionPropertyListIsRetrievedThenSendOneEmailWithEmailTemplate() throws Exception {
    Journal journal = new Journal();
    journal.setEmail("supportX@gmail.com");

    Author author = new Author();
    author.setEmail("mail@mail.to");

    SubmissionDAO sub = new SubmissionDAO();
    sub.setSubmissionId("111111111");
    sub.setRevision(1);
    sub.setJournal(journal);
    sub.setAuthors(Collections.singletonList(author));

    String email = sub.getJournal().getEmail();
    String[] fetchEmail = email.split(",");
    String fromEmail = fetchEmail[0];
    String bccEmail = emailNotificationRequestConfiguration.getBcc().get("default");
    List<String> bccEmails = StringUtils.isNotBlank(bccEmail) ? Arrays.asList(bccEmail.split(",")) : Collections.emptyList();
    
    EmailNotificationRequest request = new EmailNotificationRequest();
    request.setTo(Collections.singletonList("mail@mail.to"));
    request.setSubject("Incomplete submission to Biology, ID: 111111111");
    request.setFrom(fromEmail);
    request.setBcc(bccEmails);
    request.setBody(escapeHtml4(IOUtils.toString(getSystemResourceAsStream("revision-reminder.html"), UTF_8)));

    mockSendEndpoint.setExpectedMessageCount(1);
    mockSendEndpoint.expectedBodiesReceived(request);

    producer.send(ExchangeBuilder.anExchange(camelContext)
            .withProperty(ExchangeProperties.FILTERED_SUBMISSIONS_LIST_PROPERTY, Collections.singletonList(sub))
            .build());

    mockSendEndpoint.assertIsSatisfied();
}

How do I assert those object instances? Is there any way? Or am I doing anything wrong here?

Also FYI, I am setting the exchange body in the Processor with a new EmailNotificationRequest.

Amey Lokhande
  • 402
  • 5
  • 11

2 Answers2

1

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.

If you want to be able to directly compare them, you have to implement equals and hashcode methods of the type EmailNotificationRequest.

The other way is to compare single object attribute values instead of the whole instance (for example subject).

To do this, you can get the received exchanges from the Camel Mock like this

List<Exchange> messages = mockSendEndpoint.getReceivedExchanges()

You can then take a single message from the list or iterate through all messages to compare subject, to, from etc. with the expected values.

burki
  • 6,741
  • 1
  • 15
  • 31
  • Thanks for the answer burki. But I already have implemented equals & hashcode method in the ```EmailNotificationRequest``` class. The issue was something else. – Amey Lokhande Aug 28 '20 at 07:06
0

I feel that the error is kind of a bit misleading.

The test case was actually failing due to the request body which I was setting & asserting. The request body in my case is an HTML template. And I was trying to assert it with a wrong HTML template.

When I used the correct HTML template, the test case passed.

Amey Lokhande
  • 402
  • 5
  • 11