用于涉及范围=“请求”的单元测试的EasyMock对象豆
我正在尝试向我们的一些公司代码添加一些单元测试。是的,我知道它应该已经存在,但似乎并不是每个人都与我对单元测试有相同的看法。
然而,我遇到了一些阻碍。不可否认,我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在 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.
Spring 3.2 对此提供了支持。请参阅“Spring MVC 测试框架"
Spring 3.2 comes with support for this. See "Spring MVC Test Framework"
您需要一个
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.