Results 1 to 3 of 3

Thread: Switching in stub services in web app

  1. #1
    Join Date
    Jan 2012
    Posts
    4

    Default 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. #2
    Join Date
    Jan 2012
    Posts
    4

    Question 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...

  3. #3
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •