Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7587007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:34:02+00:00 2026-05-30T19:34:02+00:00

I’m currently writing a Spring MVC-based webapp. Rather than writing one test for every

  • 0

I’m currently writing a Spring MVC-based webapp.

Rather than writing one test for every annotated method, I would like to benefit from Parameterized JUnit runner.

Finally, I got it almost working, although I had to change all primitive arguments to their wrapper counterpart in my controller methods (and then manually do the sanity checks on null refs).

If it can help, here is the code (this also depends on Guava):

@RunWith(Parameterized.class)
public class MyControllerMappingTest {

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;
    private MyController mockedController;
    private AnnotationMethodHandlerAdapter annotationHandlerAdapter;
    private final String httpMethod;
    private final String uri;
    private final String controllerMethod;
    private final Class<?>[] parameterTypes;
    private final Object[] parameterValues;

    @Before
    public void setup() {
        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
        mockedController = mock(MyController.class);
        annotationHandlerAdapter = new AnnotationMethodHandlerAdapter();
    }

    @Parameters
    public static Collection<Object[]> requestMappings() {
        return asList(new Object[][] {
                {"GET", "/my/uri/0", "index", arguments(new MethodArgument(Integer.class, 0))}
        });
    }

    private static List<MethodArgument> arguments(MethodArgument... arguments) {
        return asList(arguments);
    }

    public MyControllerMappingTest(String httpMethod, String uri, String controllerMethod, List<MethodArgument> additionalParameters) {
        this.httpMethod = httpMethod;
        this.uri = uri;
        this.controllerMethod = controllerMethod;
        this.parameterTypes = new Class<?>[additionalParameters.size()];
        initializeParameterTypes(additionalParameters);
        this.parameterValues = newArrayList(transform(additionalParameters, valueExtractor())).toArray();
}

    private void initializeParameterTypes(List<MethodArgument> additionalParameters) {
        Iterable<Class<?>> classes = transform(additionalParameters, typeExtractor());
        int i = 0;
        for (Class<?> parameterClass : classes) {
            parameterTypes[i++] = parameterClass;
        }
    }

    @Test
    public void when_matching_mapping_constraints_then_controller_method_automatically_called() throws Exception {
        request.setMethod(httpMethod);
        request.setRequestURI(uri);
        annotationHandlerAdapter.handle(request, response, mockedController);

        Method method = MyController.class.getMethod(controllerMethod, parameterTypes);
        method.invoke(verify(mockedController), parameterValues);
    }
}

with the custom class MethodArgument that follows:

public class MethodArgument {
    private final Class<?> type;
    private final Object value;

    public MethodArgument(final Class<?> type, final Object value) {
        this.type = type;
        this.value = value;
    }

    public Object getValue() {
        return value;
    }

    public Class<?> getType() {
        return type;
    }

    public static Function<MethodArgument, Class<?>> typeExtractor() {
        return new Function<MethodArgument, Class<?>>() {
            @Override
            public Class<?> apply(MethodArgument argument) {
                return argument.getType();
            }
        };
    }

    public static Function<MethodArgument, Object> valueExtractor() {
        return new Function<MethodArgument, Object>() {
            @Override
            public Object apply(MethodArgument argument) {
                return argument.getValue();
            }
        };
    }
}

So, I’m almost there, the only test case here works because of Java Integer cache, and the Integer instance is therefore the same throughout the call chain… This however doesn’t work with custom objects, I always end up with an InvocationTargetException (cause: “Argument(s) are different!”)…

The types are correct but the passed instances are not identical to the ones set in the @Parameters method.

Any idea how to work around this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:34:03+00:00Added an answer on May 30, 2026 at 7:34 pm

    Hold your horses!

    SpringSource is baking a spring-test-mvc module :
    https://github.com/SpringSource/spring-test-mvc

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.