我试图编写一个测试用例来检查文档是否被压入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());
}
答案 0 :(得分:1)
Solr有一个功能名称RealTimeGet,可用于检索已发送到solr但仍未提交的文档。
Spring-Data-Solr提供RealTime Get部分中记录的此功能。使用此功能,您甚至可以在提交事务之前根据其ID检索文档。
示例:
CustomerDocument consumerDocument = solrTemplate.getById(email, CustomerDocument.class);
Assert.assertNotNull(consumerDocument);
答案 1 :(得分:0)
下面应该提交solr-transaction然后你可以查询Solr
solrTemplate.commit();
示例代码如下:
@Test
public void testSomething(){
createMyBeanAndSaveIt();
solrTemplate.commit();
solrTemplate.query...
}
对于其他绊倒这个问题的人,上面是我们所做的。