我对@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);
}
}
答案 0 :(得分:0)
尝试在事务 DbUnit TestExecutionListener上更改TransactionalTestExecutionListener。
来源:https://github.com/springtestdbunit/spring-test-dbunit#transactions
答案 1 :(得分:0)
事实上,问题出在dataSource bean中,用于连接。属性 - “defaultAutoCommit”等于false,当我将其设置为true时,问题就解决了。