Announcement Announcement Module
Collapse
No announcement yet.
Switching in stub services in web app Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Switching in stub services in web app

    Hello all

    We are building a new web app using Spring 3.1, using Spring mvc.

    We want to be able to switch out service beans used by the app with stub service beans for the purpose of testing.

    We've started off by using ContextLoaderListener along with xml configuration to load up our beans, but we can't seem to find a way to switch or replace the service beans that we want to stub at initialization.

    Hoping to make use of the @Configuration annotation, but again, because initialization is done by the ContextLoaderListener we don't have control of this.

    Hope that makes sense.

    Anyone done this sort of thing before?

    Anyone got any ideas on how to do this?

  • #2
    Switching on stub services....

    To add a bit more context and explanation....

    I am using AnnotationConfigWebApplicationContext.

    so web.xml includes

    Code:
             <context-param>
    		<param-name>contextClass</param-name>
    		<param-value>
    			org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    		</param-value>
    	</context-param>
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>uk.co.my.test.wiring.ApplicationWiring</param-value>
    	</context-param>
    
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

    and the ApplicationWiring class looks like..

    Code:
    @Configuration
    public class ApplicationWiring {
    
    	@Bean
    	public VehicleDetailsService vehicleDetailsService(){
    		if (ApplicationConfiguration.stubMode) {
    			return new StubVehicleDetailsService(liveStubDataHolder());
    		} else {
    			return new LiveVehicleDetailsService(applicationConfiguration(), vehicleDetailsFactory());
    		}
    		
    	}
    
    	@Bean
    	public VehicleDetailsFactory vehicleDetailsFactory() {
    		return new VehicleDetailsFactory(applicationConfiguration());
    	}
    	
    	@Bean
    	public  ApplicationConfiguration applicationConfiguration() {
    		return new ApplicationConfiguration(new Properties());
    	}
    
    	@Bean
    	public  LiveStubDataHolder liveStubDataHolder() {
    		return new LiveStubDataHolder();
    	}
    
    }
    I am running the app from embedded jetty - so I've got a starter class for 'real' services and one for 'stub'

    This is the stub mode...
    Code:
    public class EmbeddedDefaultApplicationStarterWithStubs {
    
        public static void main(String... args) throws Exception {
        	ApplicationConfiguration.stubMode = true;
        	new EmbeddedUnpackedWarContainer("src/main/webapp").start();
        }
    
    }
    It kind of works....

    But what I'd really like to do is switch in a different ApplicationWiring depending on whether stubsMode is true or not, without having to edit web.xml

    Is there a better way of doing this? Any suggestions most welcome...

    Comment


    • #3
      Have a look at https://github.com/SpringSource/spring-test-mvc. This discussion might also be relevant: https://github.com/SpringSource/spri...-mvc/issues/21

      Comment

      Working...
      X