首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring-test-dbunit @DatabaseSetup

spring-test-dbunit @DatabaseSetup
EN

Stack Overflow用户
提问于 2015-12-24 12:45:05
回答 2查看 2.4K关注 0票数 0

我对@DatabaseSetup注解有一个问题。当我使用它时,我在事务中有数据(在执行@DatabaseSetup之后),这是我不需要的,并且注释@Transactional(Transactional.TxType.NEVER)由于某种原因不能工作。你知道怎么修复它吗?请帮帮我!

这是我的测试类:

代码语言:javascript
复制
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ServiceTestContext.class})
@TestExecutionListeners({
        DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class,
        DbUnitTestExecutionListener.class
})
public class RequestHistoryServiceImplTest extends BaseServiceTest {

  @Autowired
  JdbcTemplate jdbcTemplate;

  @Autowired
  private RequestHistoryService requestHistoryService;

  private Set<HistoryJPA> getExpectedHistoryJPAs() {
    Set<HistoryJPA> expectedSet = new HashSet<>();

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    Timestamp date = new Timestamp(calendar.getTime().getTime());

    HistoryJPA firstHistoryJPA = new HistoryJPA();
    firstHistoryJPA.setProcessInstanceId("92604760");
    firstHistoryJPA.setCreated(date);
    firstHistoryJPA.setName("action");
    firstHistoryJPA.setValue("draft");
    expectedSet.add(firstHistoryJPA);

    HistoryJPA secondHistoryJPA = new HistoryJPA();
    secondHistoryJPA.setProcessInstanceId("92604760");
    secondHistoryJPA.setCreated(date);
    secondHistoryJPA.setName("_formName");
    secondHistoryJPA.setValue("New Vacation Request");
    expectedSet.add(secondHistoryJPA);

    HistoryJPA thirdHistoryJPA = new HistoryJPA();
    thirdHistoryJPA.setProcessInstanceId("92604760");
    thirdHistoryJPA.setCreated(date);
    thirdHistoryJPA.setName("employeeId");
    thirdHistoryJPA.setValue("4000600100000178284");
    expectedSet.add(thirdHistoryJPA);
    return expectedSet;
  }

  @Test
  @Transactional(Transactional.TxType.NEVER)
  @DatabaseSetup(value = {
          "/data/init/history/act_hi_varinst.xml"
  })
  @DatabaseTearDown(value = {
          "data/init/history/clearAct_hi_varinst.xml",
          "data/init/history/clearT_history.xml"
  })
  public void testSaveHistory() throws Exception {
    RowCountCallbackHandler actHiVarinstCount = new RowCountCallbackHandler();
    jdbcTemplate.query("select * from act_hi_varinst where proc_inst_id_ = '92604760'", actHiVarinstCount);
    assertEquals(4, actHiVarinstCount.getRowCount());

    Set<HistoryJPA> expectedSet = getExpectedHistoryJPAs();

    RowCountCallbackHandler THistoryCount = new RowCountCallbackHandler();
    jdbcTemplate.query("select * from t_history", THistoryCount);
    assertEquals(0, THistoryCount.getRowCount());

    requestHistoryService.saveHistory("92604760", null);

    RowCountCallbackHandler newTHistoryCount = new RowCountCallbackHandler();
    jdbcTemplate.query("select * from t_history where process_instance_id = '92604760'", newTHistoryCount);
    assertEquals(3, newTHistoryCount.getRowCount());

    Set<HistoryJPA> actualSet = new HashSet<>();
    List<Map<String, Object>> rows = jdbcTemplate.queryForList("select * from t_history where process_instance_id = '92604760'");
    for (Map<String, Object> row : rows) {
      HistoryJPA historyJPA = new HistoryJPA();
      historyJPA.setProcessInstanceId((String) row.get("process_instance_id"));

      Calendar newDate = Calendar.getInstance();
      newDate.setTime((Timestamp) row.get("created"));
      newDate.set(Calendar.SECOND, 0);
      newDate.set(Calendar.MILLISECOND, 0);

      historyJPA.setCreated(new Timestamp(newDate.getTimeInMillis()));
      historyJPA.setName((String) row.get("name"));
      historyJPA.setValue((String) row.get("value"));
      actualSet.add(historyJPA);
    }

    assertEquals(expectedSet, actualSet);
  }
}
EN

回答 2

Stack Overflow用户

发布于 2016-01-11 18:21:46

尝试更改TransactionDbUnitTestExecutionListener.上的TransactionalTestExecutionListener

来源:https://github.com/springtestdbunit/spring-test-dbunit#transactions

票数 0
EN

Stack Overflow用户

发布于 2016-01-12 13:51:45

事实上,问题出在用于连接的dataSource bean中。属性- "defaultAutoCommit“等于false,当我将其设置为true时,问题就解决了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34447359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档