tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Piotr D <piotr...@gmail.com>
Subject EJB repository testing with OpenEJB - how to rollback changes
Date Tue, 05 Apr 2011 20:16:14 GMT
Hi,

I try to test my EJB-based repositories using OpenEJB. Every time new
unit test is runned I'd like to have my DB in an "initial" state.
After the test, all changes should be rolled back (no matter if test
succeeded or not). How to accomplish it in a simple way? I tried using
UserTransaction - beginning it when test is starting and rolling back
changes when finishing (as you can see below). I don't know why, but
with this code all changes in DB (which were done during unit test)
are left after line rolling changes back has been executed. As I
wrote, I'd like to accomplish it in the simplest way, without any
external DB schema and so on.

---------

 public class MyRepositoryTest {

    private Context initialContext;

    private UserTransaction tx;

    private MyRepository repository; //class under the test

    @Before
    public void setUp() throws Exception {
        this.initialContext = OpenEjbContextFactory.getInitialContext();
        this.repository = (MyRepository) initialContext.lookup(
                "MyRepositoryLocal");
        TransactionManager tm = (TransactionManager) initialContext.lookup(
                "java:comp/TransactionManager");
        tx = new CoreUserTransaction(tm);
        tx.begin();
    }

    @After
    public void tearDown() throws Exception {
        tx.rollback();
        this.initialContext = null;
    }

    @Test
    public void test() throws Exception {
        // do some test stuff
    }
}

---------

I've tried also in this way:
---------
@Before
    public void setUp() throws Exception {
       (...)
        tx = (UserTransaction)
initialContext.lookup("java:comp/UserTransaction");
    }
---------

But result is the same (changes are persisted into data storage and no
rolled back). I use the latest (3.1.4) OpenEJB version.

Thanks in advance for any hints!

Piotr

Mime
View raw message