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
      • *
        #//in addition to the following flags
    • 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
        @TestExecutionListeners( {
                TransactionalTestExecutionListener.class })
        @ContextConfiguration(locations = {
        public class FundDAOTest {
             * The DAO being tested, injected by Spring
            private FundDAO fundDAO;
             * Instantiates a new FundDAOTest.
            public FundDAOTest() {
             * Method to test Fund domain object.
            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")