views:

33

answers:

2

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" />
A: 

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.

Espen
A: 

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.

iwein