Results 1 to 3 of 3

Thread: test with extended DependencyInjectionTestExecutionListener

  1. #1
    Join Date
    Feb 2008
    Posts
    18

    Question test with extended DependencyInjectionTestExecutionListener

    Hello

    I have a Dao extending JpaDaoSupport and wrote a JUnit test for it:
    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);    
      }
    }
    This test is working. The testFindById method could load an entity from the database.

    For an easier logging I wrote a class extending DependencyInjectionTestExecutionListener:

    Code:
    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));
      }
    }
    If I use this in my tests with an additional configuration
    Code:
    @TestExecutionListeners( { LogTestMethodListener.class })
    Now the the second method no longer finds the entity. In the log is no rollback or something.

    Where is my mistake?

    regards
    Mark

  2. #2
    Join Date
    Feb 2006
    Posts
    20

    Thumbs up

    It's because by specifying custom listener you're overwriting three default listeners defined in TestContextManager:

    Code:
    public class TestContextManager {
    
      private static final String[] DEFAULT_TEST_EXECUTION_LISTENER_CLASS_NAMES = new String[] {
      "org.springframework.test.context.support.DependencyInjectionTestExecutionListener",
      "org.springframework.test.context.support.DirtiesContextTestExecutionListener",
      "org.springframework.test.context.transaction.TransactionalTestExecutionListener" };
    [...]
    }
    First one is there (your listener extends this class, so dependencies are injected). However, listener that is responsible for transactions is not invoked.

    P.S. You cannot be sure that your tests will be executed in these order everytime. If you need to prepare some data - user @Before or @BeforeTransaction annotations.

  3. #3
    Join Date
    Feb 2008
    Posts
    18

    Default

    Hello rience,

    you are right. Adding the missing other Listeners solves the problem.

    Thank you!

    regards,
    Mark

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •