tag:blogger.com,1999:blog-106449775438946661.post4733364984001427128..comments2024-03-19T05:14:38.879-07:00Comments on Overcoming Fear: Testing With Spring, Hibernate and JUnit4Tim Myerhttp://www.blogger.com/profile/01514263555690798238noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-106449775438946661.post-13048006055053847252008-11-05T20:14:00.000-08:002008-11-05T20:14:00.000-08:00Hi Ariel,Excellent! Thank you for the link.I agre...Hi Ariel,<BR/>Excellent! Thank you for the link.<BR/>I agree that in the general case, the Spring 2.5+ annotations appear to be a good way to go. However I can think of at least one situation where these annotations might be problematic and using a composite would be preferable: when you need more than one JUnit runner. In fact, there appears (as far as I can tell) to be a similar limitation with @RunWith and with test base class extension -- namely, that we can use only one test runner and we can use only one base class.<BR/>For example, one test pattern I have noticed over my last 3 projects is the early use of mock Objects (in particular, JMock), but as components are fully implemented, these mock Objects eventually make way for their functional components.<BR/>JMock uses its own JUnit runner similarly to Spring, as in this example:<BR/>....<BR/>@RunWith(JMock.class)<BR/>public class AuthorDAOTest {<BR/><BR/> private final Mockery mockery = new Mockery() {<BR/> {<BR/> setImposteriser(ClassImposteriser.INSTANCE);<BR/> }<BR/> };<BR/> <BR/>....<BR/> @Test<BR/> public void testWithMockery() throws Exception {<BR/> final AuthorDAO dao = delegate.getAuthorDAO();<BR/> final HibernateTemplate mock = mockery.mock(HibernateTemplate.class);<BR/> mockery.checking(new Expectations() {<BR/> {<BR/> one(mock).find("from Author author where author.lastName = ?", "Thoreau");<BR/> one(mock).bulkUpdate("something here");<BR/> }<BR/> });<BR/> dao.setHibernateTemplate(mock);<BR/> dao.findByLastNameUsingHQL("Thoreau");<BR/> }<BR/>....<BR/><BR/>The test case succeeds without the runner but fails with it because the runner performs validations on teardown (and the DAO method never calls bulkUpdate).<BR/><BR/>I could certainly imagine a scenario where I would want to use Mock Objects in a test class alongside Spring transactional components.<BR/><BR/>I would be curious to hear about other possible solutions.<BR/><BR/>Thanks for getting me to look further into this and for introducing me to Spring test annotations!<BR/>-----Tim-----Tim Myerhttps://www.blogger.com/profile/01514263555690798238noreply@blogger.comtag:blogger.com,1999:blog-106449775438946661.post-10546361538434896462008-11-05T12:21:00.000-08:002008-11-05T12:21:00.000-08:00Example taken from: http://static.springframework....Example taken from: <BR/>http://static.springframework.org/spring/docs/2.5.x/reference/testing.html<BR/><BR/>@RunWith(SpringJUnit4ClassRunner.class)<BR/>@ContextConfiguration<BR/>@TransactionConfiguration(transactionManager="txMgr", defaultRollback=false)<BR/>@Transactional<BR/>public class FictitiousTransactionalTest {<BR/><BR/> @BeforeTransaction<BR/> public void verifyInitialDatabaseState() {<BR/> // logic to verify the initial state before a transaction is started<BR/> }<BR/><BR/> @Before<BR/> public void setUpTestDataWithinTransaction() {<BR/> // set up test data within the transaction<BR/> }<BR/><BR/> @Test<BR/> // overrides the class-level defaultRollback setting<BR/> @Rollback(true)<BR/> public void modifyDatabaseWithinTransaction() {<BR/> // logic which uses the test data and modifies database state<BR/> }<BR/><BR/> @After<BR/> public void tearDownWithinTransaction() {<BR/> // execute "tear down" logic within the transaction<BR/> }<BR/><BR/> @AfterTransaction<BR/> public void verifyFinalDatabaseState() {<BR/> // logic to verify the final state after transaction has rolled back<BR/> }<BR/><BR/> @Test<BR/> @NotTransactional<BR/> public void performNonDatabaseRelatedAction() {<BR/> // logic which does not modify database state<BR/> }<BR/>}Ariel Valentinhttps://www.blogger.com/profile/15968494425917141273noreply@blogger.comtag:blogger.com,1999:blog-106449775438946661.post-70976347616201676322008-11-04T17:25:00.000-08:002008-11-04T17:25:00.000-08:00Good to hear from you. Which annotations are you ...Good to hear from you. Which annotations are you referring to?<BR/>Thanks.<BR/>------Tim------Tim Myerhttps://www.blogger.com/profile/01514263555690798238noreply@blogger.comtag:blogger.com,1999:blog-106449775438946661.post-11387892009467721022008-11-04T15:01:00.000-08:002008-11-04T15:01:00.000-08:00Why not use the new Spring 2.5+ annotations?Why not use the new Spring 2.5+ annotations?Ariel Valentinhttps://www.blogger.com/profile/15968494425917141273noreply@blogger.com