Announcement Announcement Module
Collapse
No announcement yet.
Using AbstractTransactionalDataSourceSpringContextTests for Hibernate DAO unit test Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using AbstractTransactionalDataSourceSpringContextTests for Hibernate DAO unit test

    Hi,

    I am using the AbstractTransactionalDataSourceSpringContextTests for my Hibernate DAO class unit test. I am using the autowire by name for my beans. Following an example I found on the internet, I am able to put my testing data into spring configuration file with beans. That really gives me a lot of efficiency when I need to adjust testing data. However, I noticed one thing that does not work as the the example specified.

    Here is the original example:

    import java.util.List;
    import java.sql.Timestamp;

    import org.springframework.test.AbstractTransactionalData SourceSpringContextTests;

    import com.ubs.ef.efra.database.dao.ref.pojo.Roles;
    import com.ubs.ef.efra.database.dao.ref.RolesDAO;


    public class RolesDAOTest extends AbstractTransactionalDataSourceSpringContextTests {

    protected String[] getConfigLocations() {
    setAutowireMode(AUTOWIRE_BY_NAME);
    setDependencyCheck(false);
    return new String[] { "classpath*:/**/applicationContext-hibernate.xml",
    "classpath*:/**/applicationContext-test.xml"

    };
    }



    private RolesDAO dao;

    private Long existingId;

    private Roles testEntityRoles;

    private String testDescription;


    public void setRolesDAO(RolesDAO dao) {
    this.dao = dao;
    }

    public RolesDAO getRolesDAO() {
    return this.dao;
    }

    public void setExistingId(Long id) {
    this.existingId = id;
    }

    public Long getExistingId() {
    return this.existingId;
    }

    public Roles getTestEntityRoles() {
    return testEntityRoles;
    }

    public void setTestEntityRoles(Roles testEntityRoles) {
    this.testEntityRoles = testEntityRoles;
    }

    public void testGetById() {
    Roles test = dao.getById(existingId);
    assertNotNull(test);
    }

    public void testFindAll() {
    List<Roles> all = dao.findAll();
    assertNotNull("testFindAll", all);
    assertFalse("testFindAll", all.isEmpty());
    }

    public void testSaveAndRemove() {
    dao.save(testEntityRoles);
    Roles test = dao.getById(testEntityRoles.getRoleId());
    assertNotNull("testSaveAndRemove", testEntityRoles.getRoleId());
    assertNotNull("testSaveAndRemove", test);
    dao.delete(testEntityRoles);
    test = dao.getById(testEntityRoles.getRoleId());
    assertNull("testSaveAndRemove", test);
    }

    public String getTestDescription() {
    return testDescription;
    }

    public void setTestDescription(String testDescription) {
    this.testDescription = testDescription;
    }

    public void testFindByDescription() {
    List<Roles> list = dao.findByDescription(testDescription);
    assertNotNull("testFindByDescription", list);
    assertFalse("testFindByDescription", list.isEmpty());
    }


    }


    In the original example, the testing data was set as property for the RolesDAOTest class. However, when I run it in the Junit test, the Junit did not get the RolesDAOTest from bean context. Junit still calls the new RolesDAOTest() to get an instance. I am able to get the DAO class and other testingEntity beans load automatically.

    Right now, I use alternative to create another testing data bean to load the testing data. The problem is that I have to use the hibernate mapping Roles class, which does not give me much flexibility. If I want to have a testing case to test search by a list of roleNames, I can not do it using this approach.

    I am new to Spring and so I am wondering that maybe I misunderstand how to use the AbstractTransactionalDataSourceSpringContextTests? Any comments for my question will be helpful.
Working...
X