I’m struggling with legacy code. I’m creating unit tests so I’ve decided to use groovy to fill database with required legacy data. Normally in my code I using ibatis for persistence. I’d like to rollback test in the end. Problem is that when I create row via groovy then I use it’s id to create row via ibatis I get constraint violation exception – parent key not found.
When I use groovy to persist parent and than create child based on parents id it works perfectly fine.
Also I can’t use @Transactional because of problems with XML parser (legacy code 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);
}
Configuration looks like this:
<?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>
Unfortunately it was a database schema issue.