【问题标题】:spring-test-dbunit @DatabaseSetupspring-test-dbunit @DatabaseSetup
【发布时间】:2023-03-02 23:54:02
【问题描述】:

@DatabaseSetup 注释有问题。当我使用它时,我在事务中有数据(在执行@DatabaseSetup 之后),这是我不需要的并且注释@Transactional(Transactional.TxType.NEVER) 由于某种原因无法正常工作。知道如何解决吗?请帮帮我!

这是我的测试课:

@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);
  }
}

【问题讨论】:

    标签: spring junit4 spring-annotations dbunit transactional


    【解决方案1】:

    尝试在 TransactionDbUnitTestExecutionListener 上更改 TransactionalTestExecutionListener。

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-29
        • 1970-01-01
        • 2015-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多