unit testing - Manage transactions within a spring transactional test case. -


i have test case insert domain object. within domain object 1 of fields "deploymenttype" if not set postgres has default populate production.

i want test in spring unit test of default set when insert deploymenttype set null , postgres taking care of it.

my test case extends abstracttransactionaltestngspringcontexttests , has been annotated

@transactional(propagation = propagation.nested) @transactionconfiguration(transactionmanager = "transactionmanager", defaultrollback = true) 

the unit test case follows.

@test public void createcustomerwithsite() {     this.customerservice.createcustomersite(testdata.makecustomersite(this.customer, "test-alias"));      final list<customersite> list = this.customerservice.findcustomersites(this.customer.getid());     assertthat(list.size(), is(1));     final customersite cs = list.get(0);     assertthat(cs.getclusterdeploymenttype(), is(clusterdeploymenttype.production)); } 

now since test transactional commit never takes place , hence when domain object see "deploymenttype" null , test fails.

so in such case when want test db behavior in unit test, guess need access transactionmanager in middle of test , commit transaction. start new transaction , domain object db , check if default value has been set db while inserting.

like :

       this.customerservice.createcustomersite(testdata.makecustomersite(this.customer, "test-alias"));        transactionmanager tm = gettransactionmanager();        tm.commit(); // default type insterted in db , visible in next transaction.        tm.begintransaction();           final list<customersite> list = this.customerservice.findcustomersites(this.customer.getid());         assertthat(list.size(), is(1));         final customersite cs = list.get(0);         assertthat(cs.getclusterdeploymenttype(), is(clusterdeploymenttype.production)); 

how access transaction manager in unit test transactional. right way ?

you need access entitymanager/sessionfactory (depending on use) , class flush.

then use means of querying database (a jdbctemplate) retrieve row database , check if correct. don't want test same means put in database testing first (or second level) cache way.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -