Transactional Testing in Spring

View: New views
4 Messages — Rating Filter:   Alert me  

Transactional Testing in Spring

by tch :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm having a problem doing some integration testing in spring with transactions.

I've got my test case that injects a DAO. I want to verify that a
unique constraint occurs on insert, well since eclipselink is smart
enough to not bother sending the SQL until a commit happens my SQL is
never executed.

How are people working around this? Are you somehow loading a
different persistence.xml that changes the caching behavior when
testing?

I would prefer to not have to do a commit and a delete to test an insert...

Ideas?

Thanks,
Tim
_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Re: Transactional Testing in Spring

by tch :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

After a cup of coffee I have figured this obvious problem out. The
flush command (duh).

Example for completeness:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/applicationContext-test.xml")
@Transactional
public class EmployeeEntityTest extends TestCase{

        @Autowired
        protected SimpleDao dao;
       
        @Test
        public void testEmployeeCrud()
        {
                Employee hs = new Employee();
                dao.insert(hs);
                dao.getEntityManager().flush();
                assertNotNull(hs);
               
        }
}



./tch


On Tue, Oct 14, 2008 at 8:22 AM, Tim Hollosy <hollosyt@...> wrote:

> I'm having a problem doing some integration testing in spring with transactions.
>
> I've got my test case that injects a DAO. I want to verify that a
> unique constraint occurs on insert, well since eclipselink is smart
> enough to not bother sending the SQL until a commit happens my SQL is
> never executed.
>
> How are people working around this? Are you somehow loading a
> different persistence.xml that changes the caching behavior when
> testing?
>
> I would prefer to not have to do a commit and a delete to test an insert...
>
> Ideas?
>
> Thanks,
> Tim
>
_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Re: Transactional Testing in Spring

by Gaurav Malhotra :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Spring provide one of most elegant way of handling transaction (declaratively) using org.springframework.test.jpa.AbstractJpaTests;

Here is sample junit code

You can make an abstract class like

import org.springframework.test.jpa.AbstractJpaTests;

public abstract class AbstractJPAForOhiOrm extends AbstractJpaTests {
       
        @Override
        protected String[] getConfigLocations() {
                String[] springConfig = {"classpath:META-INF/spring/Test-OhiORM.xml"};
                return springConfig;
        }
}


public class MyTestCases extends AbstractJPAForOhiOrm {

        @Autowired // inject the service
        CrudService service;

        @Transactional
        public void testOhiErros() {
                CrudService<OhiErrorsB> cs = service;
                OhiErrorsB err =  cs.findFresh(OhiErrorsB.class, new Long(25));
       
                System.err.println("Size " + err.getOhiErrorsTlList().size());
               
                List<OhiErrorsTl> errList = err.getOhiErrorsTlList();
               
                for (OhiErrorsTl ohiErrorsTl : errList) {
                        System.err.println(ohiErrorsTl.getMessage());
                }

           // setComplete(); // if you actually want to commit the data into the database
          // endTransaction();

        }


}

Regards,
Gaurav Malhotra



Tim Hollosy wrote:
I'm having a problem doing some integration testing in spring with transactions.

I've got my test case that injects a DAO. I want to verify that a
unique constraint occurs on insert, well since eclipselink is smart
enough to not bother sending the SQL until a commit happens my SQL is
never executed.

How are people working around this? Are you somehow loading a
different persistence.xml that changes the caching behavior when
testing?

I would prefer to not have to do a commit and a delete to test an insert...

Ideas?

Thanks,
Tim
_______________________________________________
eclipselink-users mailing list
eclipselink-users@eclipse.org
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Re: Transactional Testing in Spring

by Gaurav Malhotra :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The last sample was select sample. Below example actually inserts into the db

        @Transactional
        public void testAddControlRule() {
                CodControlRulesB ctrB = new CodControlRulesB();
                ctrB.setCode("code");
                ctrB.setValidation("value.toUpper()==value");
                Date today = new Date();
                Long todayInLng = today.getTime();
                Timestamp todayTS = new Timestamp(todayInLng);
               
                ctrB.setCreatedBy(todayInLng);
                ctrB.setLastUpdatedBy(todayInLng);
                ctrB.setCreationDate(todayTS);
                ctrB.setLastUpdatedDate(todayTS);
                ctrB.setObjectVersionNumber(new Long(1));
                ctrB.setLastUpdateLogin("test");
               
               
                CodControlRulesTl ctrlTl = new CodControlRulesTl();
                ctrlTl.setCodControlRulesB(ctrB);
                ctrlTl.setDescr("test");
                ctrlTl.setCreatedBy(todayInLng);
                ctrlTl.setLastUpdatedBy(todayInLng);
                ctrlTl.setCreationDate(todayTS);
                ctrlTl.setLastUpdatedDate(todayTS);
                ctrlTl.setObjectVersionNumber(new Long(1));
                ctrlTl.setLastUpdateLogin("test");
               
                List<CodControlRulesTl>  list = new ArrayList<CodControlRulesTl>();
                list.add(ctrlTl);
                ctrB.setCodControlRulesTlList(list);
               
                CrudService<CodControlRulesB> ctrService = service;
                ctrB = ctrService.create(ctrB);
               
                System.err.println("Control rule val " + ctrB.getId());

               // Un- comment the lines if you want to commit the data into db
//
// setComplete();  
// endTransaction();
        }

Spring provide one of most elegant way of handling transaction (declaratively) using org.springframework.test.jpa.AbstractJpaTests;

Here is sample junit code

You can make an abstract class like

import org.springframework.test.jpa.AbstractJpaTests;

public abstract class AbstractJPAForOhiOrm extends AbstractJpaTests {
       
        @Override
        protected String[] getConfigLocations() {
                String[] springConfig = {"classpath:META-INF/spring/Test-OhiORM.xml"};
                return springConfig;
        }
}


public class MyTestCases extends AbstractJPAForOhiOrm {

        @Autowired // inject the service
        CrudService service;

        @Transactional
        public void testOhiErros() {
                CrudService<OhiErrorsB> cs = service;
                OhiErrorsB err =  cs.findFresh(OhiErrorsB.class, new Long(25));
       
                System.err.println("Size " + err.getOhiErrorsTlList().size());
               
                List<OhiErrorsTl> errList = err.getOhiErrorsTlList();
               
                for (OhiErrorsTl ohiErrorsTl : errList) {
                        System.err.println(ohiErrorsTl.getMessage());
                }

           // setComplete(); // if you actually want to commit the data into the database
          // endTransaction();

        }


}

Regards,
Gaurav Malhotra



Tim Hollosy wrote:
I'm having a problem doing some integration testing in spring with transactions.

I've got my test case that injects a DAO. I want to verify that a
unique constraint occurs on insert, well since eclipselink is smart
enough to not bother sending the SQL until a commit happens my SQL is
never executed.

How are people working around this? Are you somehow loading a
different persistence.xml that changes the caching behavior when
testing?

I would prefer to not have to do a commit and a delete to test an insert...

Ideas?

Thanks,
Tim
_______________________________________________
eclipselink-users mailing list
eclipselink-users@eclipse.org
https://dev.eclipse.org/mailman/listinfo/eclipselink-users