3

我正在尝试编写一个测试用例来检查文档是否被推送到 SOLR 索引中。问题是,断言失败是因为事务尚未提交。数据库断言很好,它们在回滚之前以正确的行数响应。但是我在 SOLR 中的文档计数为 0,我想是因为当我查询索引时,以前的文档还没有提交到索引中。

所有条件都通过了这个测试,除了最后一个,它返回 0 个计数。

谁能建议我如何实施此测试以考虑具有 SOLR 和回滚要求的事务边界管理?

@Test
@Sql(scripts = {"classpath:test/sql/customers/delete_customer.sql"})
@Rollback
public void testSubmitSubscriberRegistration() throws Exception{
    logger.entry();
    final String email="Billy.Evans@mailinator.com";
    int orig = JdbcTestUtils.countRowsInTable(jdbcTemplate, "customers");
    MvcResult result = this.mockMvc.perform(post("/ajax/customers/register")
                    .param("firstName", "Bill")
                    .param("lastName", "Evans")
                    .param("preferredName", "Billy")
                    .param("email", email)
                    .param("subscriber", "true")
    )
            .andExpect(status().isOk())
            .andExpect(MockMvcResultMatchers.jsonPath("$.object.id", Matchers.notNullValue()))
                    .andReturn();
    logger.debug(result.getResponse().getContentAsString());

    Assert.assertEquals(JdbcTestUtils.countRowsInTable(jdbcTemplate, "customers"), orig+1);
    Assert.assertEquals(JdbcTestUtils.countRowsInTable(jdbcTemplate, "customer_roles"), orig+1);
    Mockito.verify(mockCustomerNotifier, Mockito.times(1)).sendRegistrationConfirmation(Mockito.any(Customer.class), Mockito.anyString());

    // now we do a quick confirmation of the state of the user.
    Customer customer = customerAccountService.findByEmail(email);
    Assert.assertNotNull(customer);
    Assert.assertTrue(customer.isSubscriber());
    Assert.assertEquals(customer.getFirstName(), "Bill");
    Assert.assertEquals(customer.getLastName(), "Evans");
    Assert.assertEquals(customer.getEmail(), email);
    boolean found = false;
    for (Role role: customer.getRoles())
    {
        if (role.getCode().equals("REGISTERED_CUSTOMER")){
            found = true;
        }
    }

    Assert.assertTrue(found);

    // now we have to verify the search indexes were updated with the customer details.
    Assert.assertNotNull(customerDocumentRepository);
    List<CustomerDocument> customerDocuments = customerDocumentRepository.findByEmail(email);
    Assert.assertEquals(1, customerDocuments.size());

}
4

2 回答 2

1

Solr 有一个名为RealTimeGet的功能,这可用于检索已发送到 solr 但仍未提交的文档。

Spring-Data-Solr提供此功能,如RealTime Get部分中所述。使用此功能,您甚至可以在提交事务之前根据其 id 检索文档。

例子:

CustomerDocument consumerDocument = solrTemplate.getById(email, CustomerDocument.class);
Assert.assertNotNull(consumerDocument);
于 2015-09-15T18:50:08.150 回答
0

下面应该提交 solr-transaction 然后你可以查询 Solr

solrTemplate.commit();

示例代码如下所示:

@Test
public void testSomething(){
  createMyBeanAndSaveIt();

  solrTemplate.commit();

  solrTemplate.query...
}

对于其他偶然发现这个问题的人,以上是我们所做的。

于 2016-01-18T09:09:52.397 回答