Hello
I have a Dao extending JpaDaoSupport and wrote a JUnit test for it:
This test is working. The testFindById method could load an entity from the database.Code:@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/spring-context.xml" }) @Transactional public class ContractDaoTest { private static String contractId; @Resource private ContractDao contractDao; @Test @Rollback(false) public void testInsert() { Contract aContract = new Contract(); ContractDaoTest.contractId = aContract.getId(); contractDao.insert(aContract); } @Test public void testFindById() { Assert.assertNotNull(ContractDaoTest.contractId); Contract aContract = contractDao.findById(ContractDaoTest.contractId); Assert.assertNotNull(aContract); } }
For an easier logging I wrote a class extending DependencyInjectionTestExecutionListener:
If I use this in my tests with an additional configurationCode:public class LogTestMethodListener extends DependencyInjectionTestExecutionListener implements TestExecutionListener { long startTm = 0; public void beforeTestMethod(TestContext tc) throws Exception { super.beforeTestMethod(tc); LoggerFactory.getLogger(tc.getTestClass()).debug("Start Method {}", tc.getTestMethod().getName()); this.startTm = System.currentTimeMillis(); } public void afterTestMethod(TestContext tc) throws Exception { super.afterTestMethod(tc); LoggerFactory.getLogger(tc.getTestClass()).debug("End Method {} duration= {} ms", tc.getTestMethod().getName(), (System.currentTimeMillis() - this.startTm)); } }
Now the the second method no longer finds the entity. In the log is no rollback or something.Code:@TestExecutionListeners( { LogTestMethodListener.class })
Where is my mistake?
regards
Mark


Reply With Quote
