Unit Testing
  • How to load configuration files via Spring Unit Teting Framework
  • How to define Transaction RollBack rules declaratively
  • How to define a specific Transaction Manager when unit-testing

Architecture Overview

To gain an understanding of How Spring can help you do unit-testing on different layers of you applicaiton please See Spring Docs

Best Practices & How To's

  • With the help of Spring Testing framework you can test every layer of your app, wether it be the DAO layer or the Controller, we will show you some examples below on how to achieve this for JPA
    • Spring Test can load your Spring Config files and immitate user interaction with your application as though a real user is using the web-app
    • Note: when running unit-test you can NOT use a JNDI data-source
      • we have provided a unit-test flag that needs to be set
    • include and set to true the following flag (System property) when running unit-tests
      • *
        global.var.unit_test.flag=true
        #//in addition to the following flags
        global.var.server.env=dev
        global.var.ds.keys.path="/opt2/webapp_jndi_keys/"
        global.var.ds.pwds.path="/opt2/webapp_jndi_pwds/"
        global.var.ds.prop.path="/opt2/filerepository/datasource-resources"
        
    • Here is an example showing how to implement/perform a unit-test via Spring

      • import org.junit.Test;
        import org.junit.runner.RunWith;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.test.annotation.Rollback;
        import org.springframework.test.context.ContextConfiguration;
        import org.springframework.test.context.TestExecutionListeners;
        import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
        import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
        import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
        import org.springframework.test.context.transaction.TransactionConfiguration;
        import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
        import org.springframework.transaction.annotation.Transactional;
        
        /**
         * Class used to test the basic Data Store Functionality
         *
         */
        
        @RunWith(SpringJUnit4ClassRunner.class)
        @TestExecutionListeners( {
                DependencyInjectionTestExecutionListener.class,
                DirtiesContextTestExecutionListener.class,
                TransactionalTestExecutionListener.class })
        @Transactional
        @ContextConfiguration(locations = {
                "file:./resources/your-webapp-security-context.xml",
                "file:./resources/your-webapp-service-context.xml",
                "file:./resources/your-webapp-dao-context.xml",
                "file:./resources/your-webapp-web-context.xml",
        @TransactionConfiguration(transactionManager="your_TransactionManager")
        public class FundDAOTest {
        
            /**
             * The DAO being tested, injected by Spring
             *
             */
             @Autowired
            private FundDAO fundDAO;
        
            /**
             * Instantiates a new FundDAOTest.
             *
             */
        
            public FundDAOTest() {
            }
        
            /**
             * Method to test Fund domain object.
             *
             */
            @Rollback(true)
            @Test
            public void Fund() {
                String fund = "12345A";
                Set<Fund> funds = fundDAO.findFundByFundFieldContaining(fund);
                assertEquals(0, funds.size());
            }
        }
        
    • define rollback rules on your unit-tests 
      •  @Rollback(true)
        
      • by default Spring assumes a Transaction Manager by the name of "transactionManager"
        • you will most likely name your transaction manager differently in case of multiple data-sources
        • use the following annotation on your unit-test class to specify the transaction manager
        • @TransactionConfiguration(transactionManager="your_TransactionManager")
          

References