PDA

View Full Version : AbstractSpringContextTests (JAVA) equivalent?



rvdlv
08-07-2006, 07:33 PM
First let me start of to compliment on the great work that is done on the Spring.NET. Keep up the good work!

Coming from a Java background working with Spring, I really like to be able to leverage the "AbstractTransactionalDataSourceSpringContextTests" class provided by the Spring framework. This lets us write transactional database tests like below (merely an example):



/**
* Testclass for testing class {@link HibernatePetDao}.
*
* @author Rob
*/
public class HibernatePetDaoTest extends AbstractTransactionalDataSourceSpringContextTests {

private HibernatePetDao petDao;
private SessionFactory sessionFactory;
private DatabaseHelper databaseHelper;

protected String[] getConfigLocations() {
return new String[]{"testDatasource.xml"};
}

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

protected void onSetUpInTransaction() throws Exception {
petDao = new HibernatePetDao();
petDao.setSessionFactory(sessionFactory);
databaseHelper = new DatabaseHelper(jdbcTemplate);

// Insert testdata
jdbcTemplate.execute("INSERT INTO `pet` (`id`, `version`, `name`) VALUES (100, 0, 'Snoopy')");
}

public void testGetPetById() throws Exception {
Pet pet = petDao.getPetById(100L);

assertNotNull(pet);
assertEquals("Snoopy", pet.getName());
}
}


Is there any chance you guys are considering a "AbstractTransactionalSpringContextTests" in the near future?

Rgds,

Rob

Mark Pollack
08-08-2006, 12:01 PM
Hi Rob,

Yes, we are certainly planning that. In this particular case there are some advantages to using NUnit since we can use NUnit's extension mechanism which would let us use attributes instead of inheritance to indicate the same behavior, that is



[TransactionalTests]
public class HibernatePetDaoTest {

// as usual...
}


The attribute could also be on the method level. Same thing applies for dependency injection of test code. I *really* wanna to this.... the inheritance model is limiting imho, in Java it would be nice to see this funtionality as an aspect and it would be particularly elegant with the new AOP features in Spring.Java 2.0.

Cheers,
Mark

rvdlv
08-08-2006, 09:48 PM
Hi Mark,

Thanks for your response. Leveraging NUnit extensions does make for an elegant solution indeed. I'm eagerly following further developments in this area. I'll keep my CVS checkout up to date ;-)