The Artima Developer Community
Sponsored Link

Java Buzz Forum
Twiddling on whether my Tests use mocks or The Real Thing (tm)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
dion

Posts: 5028
Nickname: dion
Registered: Feb, 2003

Dion Almaer is the Editor-in-Chief for TheServerSide.com, and is an enterprise Java evangelist
Twiddling on whether my Tests use mocks or The Real Thing (tm) Posted: Dec 7, 2004 4:50 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by dion.
Original Post: Twiddling on whether my Tests use mocks or The Real Thing (tm)
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
Latest Java Buzz Posts
Latest Java Buzz Posts by dion
Latest Posts From techno.blog(Dion)

Advertisement
Rather than always having my tests go to a database, I have my DAOs all mocked up. For most of my tests, I want to use this mock layer (much faster). For some cases I want to use the full layer (e.g. to test the real DAOs, or for integration tests). I wired up my BaseTestCase to handle both cases in a simple class, which ties into the Spring Dependency post from yesterday: package foo.test; import org.springframework.test.AbstractDependencyInjectionSpringContextTests; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public abstract class BaseTestCase extends AbstractDependencyInjectionSpringContextTests { private static String[] realPaths = { "classpath:applicationContext*.xml" }; private static String[] mockPaths = { "classpath:test-applicationContext.xml", "classpath:applicationContext-webwork.xml" }; protected Log log = LogFactory.getLog(this.getClass().getName()); /** * Override method to choose which layer to use * * Default to using the mock layer */ protected boolean useMockLayer() { return true; } protected String[] getConfigLocations() { return useMockLayer() ? mockPaths : realPaths; } } Now, I just place my mocks in the test-applicationContext.xml: <bean id="userDAO" class="foo.dao.MockUserDAO"/> If I want a test to use the full layer I simply override: protected boolean useMockLayer() { return false; }

Read: Twiddling on whether my Tests use mocks or The Real Thing (tm)

Topic: Why it can be so frustrating getting things to work... Previous Topic   Next Topic Topic: New blog for non-technology entries

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use