You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Glauber Ferreira <gl...@embedded.ufcg.edu.br> on 2008/09/29 15:39:23 UTC
Rollback transactions in unit testing
Hi all.
I need to rollback transactions in order to revert all data modified
(deleted, updated, created) by my tests. How can I do that in the test code
listed in this link:
http://openejb.apache.org/3.0/unit-testing-transactions.html
Thanks in advance.
--
Glauber Vinícius Ventura de Melo Ferreira
PGP: 0x5AA19EF5
RE: Rollback transactions in unit testing
Posted by Marcin Kwapisz <mk...@zsk.p.lodz.pl>.
> Marcin,
> the strategy you suggest should also work and is especially good for
> prototyping or smaller projects.
>
> In our case where we have a huge application with (>1.5 Mio LoC, > 1000
> EJBs, > 100 database tables with complex relationships) we cannot drop
> (or
> empty) and create tables in junit tests. This would heavily impact the
> development and testing.
> this is the reason why in such cases a rollback after a unit test
> instead of
> makes sense.
[Marcin Kwapisz]
I know. That's why we run as many unit tests as it is possible between server startup and shutdown.
But how to properly test transaction without committing it? (The link in Glauber Ferreira's post concerns testing transactions).
Can I rollback transaction that has been committed? We do not use BMT in our project.
We tried to create unit test that in one transaction (CMT not BMT):
1. Creates initial set of data
2. Calls business method to modify/remove/(etc.) entities
3. Checks results.
And that was not a good idea. The results of unit tests depended on JPA provider (or JPA settings), sometimes passed or sometimes not (the same test - that was really crazy). Manual flushing did not helped. We had to split that one transaction into three to make unit tests work properly.
Regards
--
Marcin Kwapisz
Samodzielny Zakład Sieci Komputerowych
Politechnika Łódzka
RE: Rollback transactions in unit testing
Posted by Andreas Karalus <an...@googlemail.com>.
Marcin,
the strategy you suggest should also work and is especially good for
prototyping or smaller projects.
In our case where we have a huge application with (>1.5 Mio LoC, > 1000
EJBs, > 100 database tables with complex relationships) we cannot drop (or
empty) and create tables in junit tests. This would heavily impact the
development and testing.
this is the reason why in such cases a rollback after a unit test instead of
makes sense.
regards,
andreas
Marcin Kwapisz-2 wrote:
>
> [Marcin Kwapisz]
> I do not understand why you want to rollback committed transaction,
> especially in unit test. Even, I do not know how to do it in JEE
> application.
> Maybe our way will suit you:
>
> 1. set drop and create strategy in persistence.xml (property name depends
> on JPA provider, we have separate file for unit testing)
> 2. Start OpenEJB (for example: perform ejb lookup) -> database should be
> empty now
> 3. Create initial set of data in tables
> 4. Run test
> 5. Shutdown OpenEJB
>
> Now, you can repeat steps 2-5 for another tests
>
> Regards
> --
> Marcin Kwapisz
> Division of Computer Networks
> Technical Univeristy of Lodz, Poland
>
>
>
>
>
>
--
View this message in context: http://www.nabble.com/Rollback-transactions-in-unit-testing-tp19724095p19749405.html
Sent from the OpenEJB User mailing list archive at Nabble.com.
RE: Rollback transactions in unit testing
Posted by Marcin Kwapisz <mk...@zsk.p.lodz.pl>.
[Marcin Kwapisz]
I do not understand why you want to rollback committed transaction, especially in unit test. Even, I do not know how to do it in JEE application.
Maybe our way will suit you:
1. set drop and create strategy in persistence.xml (property name depends on JPA provider, we have separate file for unit testing)
2. Start OpenEJB (for example: perform ejb lookup) -> database should be empty now
3. Create initial set of data in tables
4. Run test
5. Shutdown OpenEJB
Now, you can repeat steps 2-5 for another tests
Regards
--
Marcin Kwapisz
Division of Computer Networks
Technical Univeristy of Lodz, Poland
Re: Rollback transactions in unit testing
Posted by Andreas Karalus <an...@googlemail.com>.
We are also using openejb for junit testing. In our tests we are using
UserTransaction to control the test data. Maybe this approach might also
work for you.
Below is the basic code, the startup and shutdown of the container, as
well as starting/stopping transaction could also be moved to a base junit
class.
This works fine with openejb 3.0, in 3.1-SNAPSHOT the UserTransaction can
be retrieved with a jndi lookup (I think is java:openejb/UserTransaction)
public MyTest {
static MyService service;
UserTransaction tx;
@BeforeClass
public static void onBeforeClass() throws Exception{
// start openejb embeded container here
InitialContext ctx = new IntialContext(props);
// lookup service here
service = (MyService) ctx.lookup("java:openejb/MyServiceLocal");
}
@AfterClass
public static void onAfterClass() throws Exception{
// shutdown openejb
OpenEJB.destroy();
}
@Before
public void onBefore() throws Exception{
// start a UserTransaction for each test method
utx = new org.openejb.core.CoreUserTransaction()
utx.begin();
}
@After
public void onAfter() throws Exception{
utx.commit(); // you can do also a utx.rollback() here
}
//sample test method
@Test
public void testServiceMethod() throws Exception{
service.doSomething();
}
}
Regards,
Andreas
Glauber Ferreira-2 wrote:
>
> Hi all.
>
> I need to rollback transactions in order to revert all data modified
> (deleted, updated, created) by my tests. How can I do that in the test
> code
> listed in this link:
> http://openejb.apache.org/3.0/unit-testing-transactions.html
>
>
> Thanks in advance.
>
> --
> Glauber Vinícius Ventura de Melo Ferreira
> PGP: 0x5AA19EF5
>
>
--
View this message in context: http://www.nabble.com/Rollback-transactions-in-unit-testing-tp19724095p19732849.html
Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: Rollback transactions in unit testing
Posted by David Blevins <da...@visi.com>.
On Sep 29, 2008, at 8:39 AM, Glauber Ferreira wrote:
> Hi all.
>
> I need to rollback transactions in order to revert all data modified
> (deleted, updated, created) by my tests. How can I do that in the
> test code
> listed in this link:
> http://openejb.apache.org/3.0/unit-testing-transactions.html
Suggestions in this thread are pretty good. A couple additional
possibilities to throw on the stack.. If you throw a runtime
exception from the TransactionBean call method the container will
rollback the transaction. Another option is to call the
setRollbackOnly() method on the javax.ejb.SessionContext object. A
session ejb can have it injected with a field declared like "@Resource
SessionContext sessionContext;"
-David