I have a junit 4 test class testing DAO.
unit test:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:/WEB-INF/applicationContext-db.xml", "classpath:/WEB-INF/applicationContext-hibernate.xml", "classpath:/WEB-INF/applicationContext.xml" }) @TestExecutionListeners({DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class}) @DataSetLocation("test/java/com/yada/yada/dao/dbunit-general.xml") @TransactionConfiguration(transactionManager="transactionManager", defaultRollback = true) @Transactional public class RealmDAOJU4Test { @Autowired private DbUnitInitializer dbUnitInitializer; @Autowired private RealmDAO realmDAO; @BeforeTransaction public void setupDatabase() {
The @BeforeTransacation method is executed before EVERY test method. I would like to do this: use my DbUnitInitializer to load data into my database - ONCE when the class is created. Then each test in the class does what it needs to do with the database, then rollback (not commit), which it changes. It seems like for killing to re-insert all the same data from my test files before each testing. Is there any way to do this?
or
Is the right way to write these tests to fully load the database before EVERY test? If so, which function has a default value of Rollback = true in this situation?
Thank you for helping me in my thoughts ...
source share