我有一个 apache camel 测试用例,由于创建了不同的对象实例而失败。对象实例具有不同的 ID

I have an apache camel test case which is failing because of different object instances getting created. The object instances are having different IDs

以下是我遇到的错误 -

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>

我的class配置如下-

@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);
            }
        });
    }

这是在 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();
}

如何声明这些对象实例?有什么办法吗?还是我做错了什么?

另外,仅供参考, 我正在使用新的 EmailNotificationRequest 在处理器中设置交换主体。

AssertionError 抱怨两个对象实例不相等 很明显,因为它们不是同一个实例 .

其中一个是在您的应用程序中创建的,另一个是在您的测试用例中创建的。

如果您希望能够直接比较它们,您必须 implement equals and hashcode methods 类型 EmailNotificationRequest

另一种方法是比较单个对象属性值而不是整个实例(例如subject)。

为此,您可以像这样Camel Mock获取收到的交易

List<Exchange> messages = mockSendEndpoint.getReceivedExchanges()

然后您可以从列表中取出一条消息或遍历所有消息,将主题、收件人、发件人等与预期值进行比较。

我觉得这个错误有点误导。

由于我正在设置和断言的请求主体,测试用例实际上失败了。在我的例子中,请求正文是一个 HTML 模板。我试图用错误的 HTML 模板断言它。

当我使用正确的 HTML 模板时,测试用例通过了。