All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cedarsolutions.junit.spring.MockWebContextLoader Maven / Gradle / Ivy

There is a newer version: 5.8.4
Show newest version
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *              C E D A R
 *          S O L U T I O N S       "Software done right."
 *           S O F T W A R E
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Copyright (c) 2013 Kenneth J. Pronovici.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Apache License, Version 2.0.
 * See LICENSE for more information about the licensing terms.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Author   : Kenneth J. Pronovici 
 * Language : Java 6
 * Project  : Common Java Functionality
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package com.cedarsolutions.junit.spring;

import static org.springframework.web.context.WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.support.AbstractContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;

import com.cedarsolutions.exception.NotImplementedException;

/**
 * Mock web container, used for loading the Spring context in unit tests.
 *
 * 

* This simulates what happens when the application server boots, at least * to the extent that some screw-ups in the Spring context files become obvious. *

* * @see Integration testing Spring MVC Controllers * @author Kenneth J. Pronovici */ public class MockWebContextLoader extends AbstractContextLoader { private String contextPath; private String[] locations; private ServletContext servletContext; private GenericWebApplicationContext webContext; public MockWebContextLoader(String contextPath) { this.contextPath = contextPath; } @Override protected String getResourceSuffix() { return "-context.xml"; } @Override public ApplicationContext loadContext(final String... locations) throws Exception { this.locations = locations; this.servletContext = new MockServletContext(this.contextPath, new FileSystemResourceLoader()); this.webContext = new GenericWebApplicationContext(); this.servletContext.setAttribute(ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webContext); this.webContext.setServletContext(this.servletContext); new XmlBeanDefinitionReader(this.webContext).loadBeanDefinitions(locations); AnnotationConfigUtils.registerAnnotationConfigProcessors(this.webContext); this.webContext.refresh(); this.webContext.registerShutdownHook(); return this.webContext; } @Override public ApplicationContext loadContext(MergedContextConfiguration context) throws Exception { throw new NotImplementedException("Not implemented"); } public String getContextPath() { return this.contextPath; } public String[] getLocations() { return this.locations; } public WebApplicationContext getWebContext() { return this.webContext; } public ServletContext getServletContext() { return this.servletContext; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy