【问题标题】:Integration Test with Spring Data SOLR and Transaction Management使用 Spring Data SOLR 和事务管理进行集成测试
【发布时间】:2015-09-13 14:16:50
【问题描述】:

我正在尝试编写一个测试用例来检查文档是否被推送到 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());

}

【问题讨论】:

    标签: spring-test spring-data-solr


    【解决方案1】:

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

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

    例子:

    CustomerDocument consumerDocument = solrTemplate.getById(email, CustomerDocument.class);
    Assert.assertNotNull(consumerDocument);
    

    【讨论】:

    • 好的,谢谢弗朗西斯科,我今天晚些时候会试试这个,听起来它会起作用。我只需要移动一些东西,因为电子邮件实际上不是 docId。谢谢,我会回来的。
    • @Francisco 我无法更新我的 solr 依赖项(当前使用 1.2.0),有没有办法提交事务,就像我们为 JPA 事务执行 entityManager.flush() 一样?
    【解决方案2】:

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

    solrTemplate.commit();
    

    示例代码如下所示:

    @Test
    public void testSomething(){
      createMyBeanAndSaveIt();
    
      solrTemplate.commit();
    
      solrTemplate.query...
    }
    

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

    【讨论】:

      猜你喜欢
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-01
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 2017-08-11
      相关资源
      最近更新 更多