Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 为内部依赖ContextLoader.getCurrentWebApplicationContext()的SpringMVC应用程序编写Junit测试_Spring Mvc - Fatal编程技术网

Spring mvc 为内部依赖ContextLoader.getCurrentWebApplicationContext()的SpringMVC应用程序编写Junit测试

Spring mvc 为内部依赖ContextLoader.getCurrentWebApplicationContext()的SpringMVC应用程序编写Junit测试,spring-mvc,Spring Mvc,我正在尝试为SpringMVC应用程序中的控制器编写集成测试。控制器调用一个服务类,该服务类反过来调用一个dao来从存储库读/写数据。DAO需要查找一些配置。配置bean在WEB-INF/applicationContext.xml中定义 我用的是这样的东西: 配置配置=(配置)ContextLoader.getCurrentWebApplicationContext().getBean(“配置”) 私有字符串namespace=config.getProperty(“someproperty”

我正在尝试为SpringMVC应用程序中的控制器编写集成测试。控制器调用一个服务类,该服务类反过来调用一个dao来从存储库读/写数据。DAO需要查找一些配置。配置bean在WEB-INF/applicationContext.xml中定义

我用的是这样的东西:

配置配置=(配置)ContextLoader.getCurrentWebApplicationContext().getBean(“配置”)
私有字符串namespace=config.getProperty(“someproperty”)

属性存储在zookeeper中,因此我不使用spring的属性管理工件

问题是,在运行JUnit测试ContextLoader时,getCurrentWebApplicationContext()始终返回null

到目前为止,我已经研究了以下方法:
1.Ted Young的方法(仅谷歌搜索spring mvc集成测试Ted Young)
2.
3.本网站。。问题/8464919/unit-testing-a-servlet-that-dependens-springs-webapplicationcontextuals-getre
4.使用Selenium/JWebunit
5.

1无法解决此问题。WebApplicationContext保持为空
2声明对WebApplicationContext的支持将在spring 3.2中提供
3.我不明白。从何处获取testApplicationContext和getServletContext() 4.我不想这样做,因为这完全是黑盒测试。
5.我现在在看5个。但这需要启动一个servlet容器。没有其他选择吗

我将感激你能提供的任何帮助

谢谢 皮克斯软件


@Ted Young所以不允许我说完我说的话。使用loader=MockWebApplicationContextLoader,当webapp由servletcontainer初始化时,它不应该像Spring contextloader一样作为默认的contextloader提供吗?我需要做些什么来获得MockWebApplicationContextLoader的句柄吗?注入config对象适用于单例对象。但不可能都是单身。在每个构造函数中传递配置对象听起来太乏味了。现在,我已经创建了一个类,它有一个静态配置对象,通过setter方法自动连接。我将看一看ApplicationContextAware。许多thx将属性文件存储在类路径中

现在在控制器类中访问该属性,如下所示:

/*to access your filename.properties file */
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("filename.properties"));

String  sServerLocation = properties.getProperty("key");
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
        "/applicationContext-test.xml /applicationContext-other.xml");
现在您可以访问您的属性文件


我确信它会工作。

ContextLoader.getCurrentWebApplicationContext返回null的原因是,当您使用我的MockWebApplicationContextLoader时,您既没有使用web应用程序上下文,也没有使用特定的ContextLoader实现

@ContextConfiguration(locations = "classpath:module-test-beans.xml")
@WebAppConfiguration
public class SampleTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private WebApplicationContext wac;

    @BeforeClass
    public void setUp() throws ServletException {
        MockServletContext sc = new MockServletContext("");
        ServletContextListener listener = new ContextLoaderListener(wac);
        ServletContextEvent event = new ServletContextEvent(sc);
        listener.contextInitialized(event);
    }

    @Test
    public void testMe() {
        Assert.assertFalse(ContextLoader.getCurrentWebApplicationContext() == null);
    }
}
既然您的存储库是由Spring管理的,为什么不直接将config对象注入存储库呢?注入配置对象是访问它最合适的方式。然后,可以在用@PostConstruct注释的方法中初始化名称空间属性


或者,DOA可以实现ApplicationContextAware,以便在构建过程中接收应用程序上下文的副本。

在junit测试开始时添加以下代码:

MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
        "/applicationContext-test.xml"); // <== Customize with your paths
ServletContextListener listener = new ContextLoaderListener();
ServletContextEvent event = new ServletContextEvent(sc);
listener.contextInitialized(event);

您必须手动将WebApplication上下文添加到ContextLoderListner。 这会奏效的

@ContextConfiguration(locations = "classpath:module-test-beans.xml")
@WebAppConfiguration
public class SampleTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private WebApplicationContext wac;

    @BeforeClass
    public void setUp() throws ServletException {
        MockServletContext sc = new MockServletContext("");
        ServletContextListener listener = new ContextLoaderListener(wac);
        ServletContextEvent event = new ServletContextEvent(sc);
        listener.contextInitialized(event);
    }

    @Test
    public void testMe() {
        Assert.assertFalse(ContextLoader.getCurrentWebApplicationContext() == null);
    }
}

嗨,谢谢你的回复。我们不能在属性文件中存储属性,因为我们处于分布式环境中,因此需要在zookeeper中存储属性。另外,我不认为使用ContextLoader.getCurrentWebApplicationContext()会阻止应用程序进行单元测试。嗨,如果我使用loader=MockWebApplicationContextLoader,当web应用程序由servlet容器初始化时,它不应该像Spring上下文加载器一样作为默认上下文加载器使用吗?有什么特别的事情我需要做得到的MockWebApplicationContextLoader的处理?OTOH,如果它不是这样,为什么我需要它?不,它不应该是可用的。您必须扩展MWACL来配置ContextLoader,我看不到任何简单的方法。出于这种原因,使用ContextLoader是个坏主意。您真的应该问问自己,为什么您首先要使用ContextLoader来访问ApplicationContext。我们使用ContextLoader是因为不可能在需要的任何地方注入SpringBean。AFAIK ContextLoader.getCWAC()是API的有效用法。非常好的解决方案,只需添加文档以获取更多详细信息