We have implemented JUnit4 in our application which uses Spring core & JPA with DB2.
We need to test a full functionality which retrieves data from one database and merges into another database.
Test case for retrieving the data from 1st database is written and it is running without any error perfectly but the records are not store into the 2nd database.
Implementation
The TestCase class we have included the following annotations to make the test case run under a transaction if necessary,
#RunWith(SpringJUnit4ClassRunner.class)
#TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
TransactionalTestExecutionListener.class})
#ContextConfiguration(locations={""})
#TransactionConfiguration(transactionManager="defaultTransactionManager", defaultRollback=false)
#Transactional
In the application we have a manager class to perform this operation with doSynch() method. From that method crudHelper class's txStore() method will be called to initialize and call doStore() method (in same class) to merge the entity to database.
Following are the transactional declarative through out this test case logic
TestCase testSynch() - #Transactional(propagation=Propagation.SUPPORTS)
Manager doSynch() - #Transactional(propagation=Propagation.NEVER)
CRUDHelper txStore() - #Transactional(propagation=Propagation.REQUIRED)
doStore() - No Transactional annotation
doSynch() is marked as NEVER as at that point it doesn't need any transaction and in further levels such as in CRUDHelper the transaction can be marked as REQUIRED to ensure a transaction to be available.
Problem
Here when we run the test case which calls the Manager's doSynch() method to test the functionality, complete flow is working perfect except that records are not merged and no errors are thrown.
The Manager method when called from a JSP works great.
Also we tested by calling txStore() directly from test case and it also works fine.
Please let us know whether the transaction management is not proper or a work around to this issue will be of greater help. Also pls update me if the issue or environment is not clear. Thanks in advance.!!
Do you mark your methods with the #Rollback annotation?
From the JavaDoc:
Test annotation to indicate whether or
not the transaction for the annotated
test method should be rolled back
after the test method has completed.
If true, the transaction will be
rolled back; otherwise, the
transaction will be committed.
Related
We have some methods in production code annotated with #Transaction(propagation = REQUIRES_NEW). Thus, annotating the test class with #Transactional/#Rollback still leads to polluted database. I would like to have method that could delete records from the relevant tables when ALL of the tests of this class have completed. However, #AfterClass method is bound to be static, so how could I access my Spring-provided EntityManager and/or Spring Data repositories. The solution we use at the moment is to annotate test class with #DirtiesContext(mode=AFTER_CLASS) however this leads to really painful experiences if you forget to also override datasource URL for this test class.
So, can this be achieved in more idiomatic Spring/JUnit manner?
I have an regression suite which runs against QA environments. Since it exercises the application, by its very nature, it modifies the database. For these tests, I have specific test users and each test creates any records it is dependent on. During this setup and during execution of the tests, any created database records are tracked in local collection. At the end of the test, i.e. during the cleanUp method, the records are removed from the database. I also use a name and test run identifier that makes it easy to distinguish these records from real data in case of any cleanup issues. Not sure this would meet your idea of idiomatic Spring/JUnit, but this works for us and keeps our QA environment data reasonably clean.
I am using Hibernate4 but not Spring. In the application I am developing I want to log a record of every Add, Update, Delete to a separate log table. As it stands at the moment my code does two transactions in sequence, and it works, but I really want to wrap them up into one transaction.
I know Hibernate does not support nested transactions, only in conjunction with Spring framework. I´ve read about savepoints, but they´re not quite the same thing.
Nothing in the standards regarding JPA and JTA specification has support for nested transactions.
What you most likely mean with support by spring is #Transactional annotations on multiple methods in a call hierarchie. What spring does in that situation is to check is there an ongoing transaction if not start a new one.
You might think that the following situation is a nested transaction.
#Transactional
public void method1(){
method2(); // method in another class
}
#Transactional(propagation=REQUIRES_NEW)
public void method2(){
// do something
}
What happens in realitiy is simplified the following. The type of transactionManager1 and transactionManager2 is javax.transaction.TransactionManager
// call of method1 intercepted by spring
transactionManager1.begin();
// invocation of method1
// call of method 2 intercepted by spring (requires new detected)
transactionManager1.suspend();
transactionManager2.begin();
// invocation of method2
// method2 finished
transactionManager2.commit();
transactionManager1.resume();
// method1 finished
transactionManager1.commit();
In words the one transaction is basically on pause. It is important to understand this. Since the transaction of transactionManager2 might not see changes of transactionManager1 depending on the transaction isolation level.
Maybe a little background why I know this. I've written a prototype of distributed transaction management system, allowing to transparently executed methods in a cloud environment (one method gets executed on instance, the next method might be executed somewhere else).
I have a little bit trouble with Integration Test and the transactions.
I have a Rest Service System. Behind all I have a JPA-Repository, with a Postgres database. Now to test them I build JunitTest where I made the calls on the System. The test loads the web-context and an other xy-context where I have the configuration of security and database connections. On the test method I have the #Transactional annotation.
The test makes 2 requests (This is only one example I have more of similar scenarios on other Object):
insert a new user
on this user create a Group and after bind this to the user
The test makes the first call, and returns me a id where I use to perform the second call.
The second call take the id and make the post and there I have several problems.
Details of the second call:
Test make a post on a controller
Controller takes the request and forward it to the Service
Service method (with #Transactional) take the request and do:
a research to find the inserted user
insert a group object
update the user with the groupId (generated on point 2)
Now one of the problems I had, it was a AccessDeniedException on point 3.1, because I have also ACL, and I have to check if there are enough permissions.
One of the things that I tried to do is to set:
#Transactional(propagation=Propagation.REQUIRES_NEW)
on the Service Method.
What I get after is the result that the AccessDeniedException was disappeared but the research at 3.1 gives me empty result (the research is ok, because on other scenario I have correct results), but is strange because the first post was ok, and how I understand Spring handles the #Transactions and "commits" to database so that a commit is performed when a transaction is closed. This brings me to an other idea to try: remove the #Transaction annotation on the test, but when i made this, then the database has all the data of this scenario until the end of the tests session (If you have a lot of test this is not desirable), and this is not a very good thing.
Now I wrote a little bit where are my doubts, and problems without posting a lot of code and of privacy problems, but on request I can post little pieces of codes.
It's also probable that the approach is incorrect.
The questions are:
-how can I make this service work?
-It's the correct way to set (propagation=Propagation.REQUIRES_NEW)?
-It's the correct way to set #Transactional on the test? (eventually with a Rollback?)
Txs a lot.
To make test I use mockMvc to make the request and some annotation on the class:
#RunWith(SpringJUnit4ClassRunner.class)
#WebAppConfiguration
#ContextConfiguration(locations = { ..... })
#Transactional
public class tests {
#Test
public void aTest(){
mockMvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.addFilter(new DelegatingFilterProxy("springSecurityFilterChain", webApplicationContext), "/*")
.build();
mockMvc.perform(post(.....))
}
}
To answer your question:
It's the correct way to set #Transactional on the test? (eventually with a Rollback?)
No really, but you can. Because you are doing two requests, the second depends on the first, and http request will not remember your transaction, if you insist to do it, you need flush your session between requests.
It's the correct way to set (propagation=Propagation.REQUIRES_NEW)?
It depends. REQUIRES_NEW means it will start new transaction, the influence is that everything in the existing transaction will be invisible in the new transaction, because the old one is not commited yet! if this server is the entry point of the transaction, it makes no difference, but be aware of the visibility problem.
how can I make this service work?
OK, forget what my answers of the previous questions. If I have to write the test, I will do it this way:
The test is not transactional. If you are doing integration test, you don't need to rollback single tests. If you wanna rollback the commit, then you are having wrong task case, you should have two test cases insert user and update group.
3 parts of the test
Send request to insert user and get the ID (single transaction)
Send request to update group(another transaction)
Send request to fetch the user and do the checks.
Hope this can help you.
I'm writing a JUnit test for a Hibernate Search (Lucene) repo. In a #Before method, I persist an entity, and then in a #Test method, I want to search for it. Problem is, the index is apparently not updated by the time the test method is run.
My test class is annotated with #Transactional. I tried annotating just the #Before method with #Transactional, but I must be doing something wrong because Hibernate Search complains about a lack of session while indexing (trying to access lazy loaded properties).
I even tried sleep()ing at the beginning of the test method, to no avail.
I tried calling Search.getFullTextEntityManager(entityManager).createIndexer().startAndWait() in the #Before method, but that appears to run forever.
It's not a good idea to annotate the test as Transactional as you might need to control the transaction.
Hibernate Search is going to flush the changes to the index at transaction commit: you need to commit the transaction before running the query.
As an alternative FullTextSession has a method flushToIndexes() which you could use to force an immediate flush, but then you're not testing the full end to end transactional behavior. Also if you flush the changes to the index, a transaction rollback won't undo them so if you're attempting to have transactional tests to keep them independent that's not going to be good enough.
I would like to understand that if I annotate my junit class with #Transactional, Spring will create only one transaction which will be shared among my #Test methods and rolled back at the end. Whereas if instead I mark each single #Test with #Transactional, a new transaction will be created and rolled back on a #Test basis. I didn't quite find the expected behaviour in the official documentation (link).
Putting #Transactional on a class level is equivalent to putting it on each of the test methods.
I don't think there is an easy way of achieving your first scenario, ie, a single transaction for all the tests. I am not sure it would make much sense anyway since tests will be executed in a random order so you cannot rely on seeing each others modifications. Of course you can always explicitly call your methods from a single uber-test with a single transaction.
#Transactional at JUnit test case class level will start new transaction before each test method and roll it back afterwards.
You cannot easily start new transaction at the beginning of test class and keep it open for the whole class, at least Spring is not supporting this. Your first workaround would be to use #BeforeClass/#AfterClass pair, but they must be static thus you don't have access to transactional manager.
But first ask yourself a question, why do you want to do this? Sounds like one test depends on the output or database side effects of the other. This is a big anti-pattern in testing, not to mention JUnit does not guarantee the order in which test methods are executed. Maybe you just need a database setup before each test case? #Before and #After are executed within the context of Spring transaction, so you can use them there.