简体   繁体   中英

spring-test-dbunit @DatabaseSetup

I have a problem with @DatabaseSetup annotation. When I'm using it, I have data(after executing @DatabaseSetup ) in transaction, which I don't needed And Annotation @Transactional(Transactional.TxType.NEVER) is not working for some reason. Any idea how to fix it? Please help me!

That's my test class:

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

Try to change TransactionalTestExecutionListener on Transaction DbUnit TestExecutionListener.

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

As a matter of fact, the problem was in the dataSource bean, which used for connection. The property - "defaultAutoCommit" was equal false, and when I set it in true, the problem was resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM