如何使用 Spring 注入 ServletContext 进行 JUnit 测试?

发布于 2024-08-29 16:59:52 字数 322 浏览 3 评论 0原文

我想对用 Apache CXF 编写的 RESTful 接口进行单元测试。

我使用 ServletContext 来加载一些资源,所以我有:

@Context
private ServletContext servletContext;

如果我将其部署在 Glassfish 上,ServletContext 会被注入,并且它会按预期工作。但我不知道如何在我的服务类中注入 ServletContext,以便我可以使用 JUnit 测试来测试它。

我使用 Spring 3.0、JUnit 4、CXF 2.2.3 和 Maven。

I want to unit test a RESTful interface written with Apache CXF.

I use a ServletContext to load some resources, so I have:

@Context
private ServletContext servletContext;

If I deploy this on Glassfish, the ServletContext is injected and it works like expected. But I don't know how to inject the ServletContext in my service class, so that I can test it with a JUnit test.

I use Spring 3.0, JUnit 4, CXF 2.2.3 and Maven.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

客…行舟 2024-09-05 16:59:52

在单元测试中,您可能想要创建 MockServletContext

然后,您可以通过 setter 方法将此实例传递给您的服务对象。

In your unit test, you are probably going to want to create an instance of a MockServletContext.

You can then pass this instance to your service object through a setter method.

另类 2024-09-05 16:59:52

从 Spring 4 开始,单元测试类上的 @WebAppConfiguration 注释应该足够了,请参阅 Spring 参考文档

@ContextConfiguration
@WebAppConfiguration
public class WebAppTest {
    @Test
    public void testMe() {}
}

As of Spring 4, @WebAppConfiguration annotation on unit test class should be sufficient, see Spring reference documentation

@ContextConfiguration
@WebAppConfiguration
public class WebAppTest {
    @Test
    public void testMe() {}
}
笑忘罢 2024-09-05 16:59:52

也许你想用 servletContext.getResourceAsStream 或类似的东西读取资源,为此我使用了 Mockito,如下所示:

 @BeforeClass
void setupContext() {

    ctx = mock(ServletContext.class);
    when(ctx.getResourceAsStream(anyString())).thenAnswer(new Answer<InputStream>() {
        String path = MyTestClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()
                + "../../src/main/webapp";

        @Override
        public InputStream answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            String relativePath = (String) args[0];
            InputStream is = new FileInputStream(path + relativePath);
            return is;
        }
    });

}

Probably you want to read resources with servletContext.getResourceAsStream or something like that, for this I've used Mockito like this:

 @BeforeClass
void setupContext() {

    ctx = mock(ServletContext.class);
    when(ctx.getResourceAsStream(anyString())).thenAnswer(new Answer<InputStream>() {
        String path = MyTestClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()
                + "../../src/main/webapp";

        @Override
        public InputStream answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            String relativePath = (String) args[0];
            InputStream is = new FileInputStream(path + relativePath);
            return is;
        }
    });

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文