用于涉及范围=“请求”的单元测试的EasyMock对象豆

发布于 2024-09-06 13:50:53 字数 1823 浏览 10 评论 0原文

我正在尝试向我们的一些公司代码添加一些单元测试。是的,我知道它应该已经存在,但似乎并不是每个人都与我对单元测试有相同的看法。

然而,我遇到了一些阻碍。不可否认,我的 Java、Spring 和单元测试知识还不够丰富。我的问题是这样的:

我在代码中添加了一个单元测试,它测试一个类。此类包含一个具有scope =“request”的bean,当它尝试实例化该bean时,它会抛出异常:

java.lang.IllegalStateException:没有为范围'request'注册范围

我相信这一点是因为我没有 HttpServletRequest 对象,但我不知道如何创建其中一个模拟对象,而且我也不知道创建后如何将此模拟对象添加到单元测试中,以便它解析这个问题。

下面是所涉及代码的精简版本,我相信其中包含了该问题的所有细节。

我怎样才能让它发挥作用?

@Test
public void handleRequest() {
    try {
        Message<?> outMessage = (Message<?>) response.handleRequest(map);
    } catch (Exception e) {
        assertNotNull(e);
    }
    outMessage.getPayload().toString());
}

public class upddResponse extends AbstractResponseTransform {

@SuppressWarnings("unchecked")
public Message<?> handleRequest(Map<String, Message<?>> messages) throws Exception {
    super.addEnvironmentDetails(serviceResponseDocument.getServiceResponse());
}

public abstract class AbstractResponseTransform implements ResponseTransform,
            ApplicationContextAware {

    private ApplicationContext applicationContext;
    private MCSResponseAggregator mcsResponseAggregator;

    public ServiceResponseType addEnvironmentDetails(ServiceResponseType serviceResponse) throws Exception {
        try {
            mcsResponseAggregator = (MCSResponseAggregator) applicationContext
                        .getBean("mcsResponseAggregator");
        }
        catch (Exception ex) {

        }
    }
}

public interface ResponseTransform extends Transform {
    public Message<?> handleRequest(Map<String, Message<?>> messages)
            throws Exception;
}

<bean id="mcsResponseAggregator" class="com.company.aggregator.MCSResponseAggregator" scope="request" />

I am trying to add some Unit Testing to some of our companies code. Yes, I know it should already be there, but not everyone seems to have the same view of unit testing that I do.

However, I have come against a bit of a stopper for me. Admittedly, my Java, Spring and Unit Testing knowledge are not all that they should be. My problem is this though:

I have added a unit test to my code, which tests a class. This class includes a bean which has scope="request", and when it tries to instantiate the bean it throws an exception:

java.lang.IllegalStateException: No Scope registered for scope 'request'

I believe this is because I don't have a HttpServletRequest object, but I don't know how to create a mock one of these and also I don't know how, once created, to add this Mock Object to the unit test so that it resolves this problem.

Below is a cut down version of the code involved, which I believe includes all of the details that are part of this problem.

How can I get this to work?

@Test
public void handleRequest() {
    try {
        Message<?> outMessage = (Message<?>) response.handleRequest(map);
    } catch (Exception e) {
        assertNotNull(e);
    }
    outMessage.getPayload().toString());
}

public class upddResponse extends AbstractResponseTransform {

@SuppressWarnings("unchecked")
public Message<?> handleRequest(Map<String, Message<?>> messages) throws Exception {
    super.addEnvironmentDetails(serviceResponseDocument.getServiceResponse());
}

public abstract class AbstractResponseTransform implements ResponseTransform,
            ApplicationContextAware {

    private ApplicationContext applicationContext;
    private MCSResponseAggregator mcsResponseAggregator;

    public ServiceResponseType addEnvironmentDetails(ServiceResponseType serviceResponse) throws Exception {
        try {
            mcsResponseAggregator = (MCSResponseAggregator) applicationContext
                        .getBean("mcsResponseAggregator");
        }
        catch (Exception ex) {

        }
    }
}

public interface ResponseTransform extends Transform {
    public Message<?> handleRequest(Map<String, Message<?>> messages)
            throws Exception;
}

<bean id="mcsResponseAggregator" class="com.company.aggregator.MCSResponseAggregator" scope="request" />

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

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

发布评论

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

评论(3

亽野灬性zι浪 2024-09-13 13:50:54

您可以在 Spring 上下文中使用模拟:

但这并不能解决你的问题,因为它不会让Spring理解scope =“request”。您可以创建您的 自己的实现请求范围,但我感觉你最好不要经历所有这些麻烦。

最简单的方法是在一个小的测试上下文中覆盖您的请求作用域 bean。从技术上讲,您并没有测试原始上下文,但您会完成得更快。

You can use mocks within the Spring Context:

but that will not solve your problem as it will not make Spring understand scope="request". You can create your own implementation of the request scope, but I'm getting the feeling that you're better off not going through all this trouble.

The easy way out would be to override your request scoped bean in a little test context. You're technically not testing the original context then, but you will be done a lot quicker.

可遇━不可求 2024-09-13 13:50:54

Spring 3.2 对此提供了支持。请参阅“Spring MVC 测试框架"

Spring 3.2 comes with support for this. See "Spring MVC Test Framework"

放我走吧 2024-09-13 13:50:53

您需要一个 WebApplicationContext 来处理带有以下内容的 bean: scope="request"

我建议在 Spring 集成测试中使用存根对象,并在测试隔离的类时使用不带 Spring 的 EasyMock。

You need a WebApplicationContext to handle beans with: scope="request"

I recommend to use stub objects with Spring integration tests and use EasyMock without Spring when you test a class isolated.

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