0
我在使用遺留代碼掙扎。我正在創建單元測試,因此我決定使用groovy來填充具有所需遺留數據的數據庫。通常在我的代碼中,我使用ibatis進行持久化。最後我想回滾測試。問題是,當我通過groovy創建行,然後我使用它的id來通過ibatis創建行我得到約束違規異常 - 找不到父鍵。Groovy和同一事務下的彈簧
當我使用groovy來堅持父母,並根據父母身份創建孩子時,它的作品完美無缺。
我也不能使用,因爲與XML語法上的問題@Transactional(遺留代碼FTW:/)
@ContextConfiguration(locations = [ "../dao/impl/ibatis/spring-data-context-config.xml", "classpath:/pl/com/betacom/treq/dao-context.xml"])
@RunWith(SpringJUnit4ClassRunner.class)
public class FinancingForIltCreationTest {
@Autowired
IFinancingForIltDAO financingForIltDAO;
@Autowired
Sql sql;
@Autowired
DataSourceTransactionManager transactionManager;
private TransactionStatus transactionStatus;
@Before
public void setUp() {
transactionStatus = transactionManager.getTransaction(new DefaultTransactionDefinition());
}
@After
public void tearDown() {
transactionManager.rollback(transactionStatus);
transactionStatus = null;
}
@Test
public void shallCreateFinancingForIlt() throws Exception {
//given
IltOffering offering = new IltOffering("GOING_DOWN_TO_UBERGROUND", offeringTempId, java.sql.Date.valueOf("2011-07-21"), java.sql.Date.valueOf("2012-07-21"));
offering.insert(sql); // it's inserted by groovy
//when
FinancingForIltDTO financingForIltDTO = createFinancingForIlt(offering.id).build(financingForIltDAO); // it's my assembler inserting via iBatis
//then
assertNotNull(financingForIltDTO.id);
}
配置是這樣的:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSourceIn"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>####</value>
</property>
<property name="url">
<value>####</value>
</property>
<property name="username">
<value>####</value>
</property>
<property name="password">
<value>####</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<constructor-arg ref="dataSourceIn" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<bean id="sql" class="groovy.sql.Sql">
<constructor-arg ref="dataSource" />
</bean>