Trudy Cool wrote:
I have to figure out how to get this stuff from the applicationContext.xml into 
my tests, correct ....


(end of quote)




I think the "right" way to test these things ("right" meaning the way the 
designer's of Spring intended, vs. "right" as in "what works") is to use Spring 
services to instantiate all objects, rather than an explicit "new".  Either 
that, or call an object's Factory's "newInstance()" method.  Doing that keeps 
your code independent of things (such as how AndroMDA actually names 
implementation objects).



I've been slugging through this one also, and I think I've got the answer.  May 
I point you to the class 
org.springframework.test.AbstractTransactionalDataSourceSpringContextTests 
found in the "spring-mock.jar" file.  Its used for testing of Spring objects.  
It will also allow you to use the object context files created by AndroMDA.  
Having to create Hibernate sessions directly, as well as transaction managers, 
etc. is actually "redundant work", as AndroMDA does most of the work for you.  
As you rightly stated, you need to find out how to use the 
"applicationContext.xml" file directly.  Here is some test code I'm using:





package com.lighthousepmg.kDemo.test;



import com.lighthousepmg.kDemo.AddressBook;

import com.lighthousepmg.kDemo.AddressBookDao;

import com.lighthousepmg.kDemo.Person;

import com.lighthousepmg.kDemo.PersonalEntry;



import 
org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;



public class KDemoTest extends 
AbstractTransactionalDataSourceSpringContextTests {



    public void testAddBook() {

        

         AddressBookDao dao = 
(AddressBookDao)applicationContext.getBean("addressBookDao");

         

         AddressBook book = 
AddressBook.Factory.newInstance();

         book.setBookName("My Address Book 3");

         book.setIsPrivate(new 
Boolean(false));

         

         PersonalEntry entry = 
PersonalEntry.Factory.newInstance();

        
 entry.setPerson(Person.newInstance("Joel", "Kozikowski"));

         entry.setState("IN");

         entry.setStreet("13598 Grapevine 
Ln.");

         entry.setCity("Fishers");

         entry.setZip("46038");

         

         // We must managed BOTH sides of the 
association manually (Hibernate requirement)...

         book.getContents().add(entry);

         entry.getBooks().add(book);

         

         book = dao.create(book);

         

    }



    

    protected String[] getConfigLocations() {

        return new String[] { 
"applicationContext.xml", "applicationContext-localDataSource.xml" };

    }



    

    public static void main(String[] args) {

        
junit.textui.TestRunner.run(KDemoTest.class);

    }





}







Two things of interest:



1) The method getConfigLocations() is declared "abstract" in 
AbstractTransactionalDataSourceSpringContextTests, and thus must be 
overwritten.  You simply specify the name of the Spring config files you want 
to use.  Both "applicationContext.xml" and 
"applicationContext-localDataSource.xml" are created by the AndroMDA Spring 
cartridge.



2) AbstractTransactionalDataSourceSpringContextTests has as one of its member 
fields an "applicationContext" member, which is basically a BeanFactory (its 
other things also, but BeanFactory is one of its interfaces).  You should get 
your beans by calling the getBean() method using the name found in your 
applicationContext.xml.  Spring will handle wiring up all the required classes 
(including the data source and Hibernate session).



3) I think transaction management is implicit in all calls to your Dao.  You 
shouldn't have to programatically make any calls to a transaction manager.  
AbstractTransactionalDataSourceSpringContextTests DOES have a 
"transactionManager" member field, which you can explicitily call "commit()" or 
"rollback()", but I found if I called them AFTER my call to dao.create(), I'd 
get an error that the transaction was already committed.



4) As a helpful hint - you can create your OWN test database (separate from 
working code).  Copy the file applicationContext-localDataSource.xml to a file 
like applicationContext-test-dataSource.xml, then change all the parameters to 
use your test table.  Then, simply change the reference in the 
getConfigLocations() method, and you'll use your test data source instead.



I'm still playing around with testing, but once I stumbled on this Spring test 
class, things got a lot easier.
_________________________________________________________
Reply to the post : http://galaxy.andromda.org/forum/viewtopic.php?p=3853#3853
Posting to http://forum.andromda.org/ is preferred over posting to the mailing 
list!


-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Andromda-user mailing list
Andromda-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/andromda-user

Reply via email to