我想寫一個測試用例來檢查文檔是否被推入SOLR索引。問題是,斷言失敗,因爲事務沒有被提交。數據庫斷言很好,它們在回滾之前用正確的行數進行響應。但是我在SOLR的文檔中得到0,因爲當我查詢索引時,以前的文檔不會被提交到索引中。與春季數據SOLR和交易管理的集成測試
所有條件通過此測試,除了最後一個返回0計數。
任何人都可以建議我如何實施此測試,以考慮與SOLR和回滾要求的交易邊界管理?
@Test
@Sql(scripts = {"classpath:test/sql/customers/delete_customer.sql"})
@Rollback
public void testSubmitSubscriberRegistration() throws Exception{
logger.entry();
final String email="[email protected]";
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());
}
好,謝謝弗朗西斯科,我會在今天晚些時候嘗試這個,聽起來好像會起作用。我只需要移動一些東西,因爲電子郵件實際上不是docId。謝謝我會回到這個。 –
謝謝我測試了這個,它運行良好。 –
@Francisco我無法更新我的solr依賴項(當前使用1.2.0),有沒有辦法提交事務,就像我們爲JPA事務執行'entityManager.flush()'一樣? –