Software Development, Agile Practices, Lean Thinking, Music
[ start | index | login or register ]

Changes of Unit Testing with StrutsTestCase and EasyMock from #8 to #9

Inserted lines at line 15
15: 1.1 The Abstract Factory Design Pattern
16: In order to enable substitution of the service class I have applied the {link:Abstract Factory pattern|url=http://en.wikipedia.org/wiki/Abstract_factory_pattern}. The TestObjectFactory class is a concrete implementation of an abstract ObjectFactory class. At the time of unit testing I can easily insert a stub or mock service, while at runtime the SpringObjectFactory concrete implementation will provide the actual services from the Spring application context of the web application.
17: {code}
18: ...
19: public abstract class ObjectFactory
20: {
21: private static ObjectFactory instance;
22: public abstract Object getBean(String beanName, ServletContext servletContext);
23: public static synchronized ObjectFactory getInstance()
24: {
25: if (instance == null)
26: {
27: instance = new SpringObjectFactory();
28: }
29: return instance;
30: }
31: public static synchronized void setInstance(ObjectFactory instance)
32: {
33: ObjectFactory.instance = instance;
34: }
35: }
36: ...
37: public class TestObjectFactory extends ObjectFactory
38: {
39: private static HashMap beans = new HashMap();
40: public Object getBean(String beanName, ServletContext servletContext)
41: {
42: return beans.get(beanName);
43: }
44: public void addBean(String beanName, Object bean)
45: {
46: beans.put(beanName, bean);
47: }
48: }
49: ...
50: public class SpringObjectFactory extends ObjectFactory
51: {
52: public Object getBean(String beanName, ServletContext servletContext)
53: {
54: WebApplicationContext webContext = WebApplicationContextUtils
55: .getWebApplicationContext(servletContext);
56: return webContext.getBean(beanName);
57: }
58: }
59: {code}
Describe here what your SnipSnap is about!

Configure this box!

  1. Login in
  2. Click here: snipsnap-portlet-2
  3. Edit this box
www.brandonburk.com | Copyright 2008 Brandon N. Burk