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

  • Home
  • SEARCH
  • 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 6589681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:12:21+00:00 2026-05-25T17:12:21+00:00

Its my understanding that an efficient way to unit test a spring controller is

  • 0

Its my understanding that an efficient way to unit test a spring controller is too simply instantiate a new instance (of the required controller) and test the controller methods directly. Not bothering with Mock requests etc, as spring itself does need testing.

However, many of my controller methods simply return a view name as a single String, and I want to test that the model itself has the correct parameters and data.

Do I need to change the methods so that they return a ModelAndView, so that I can then access the model within the unit test ? Should my methods be doing that (returning a model and view) anyway ?

When I create a new ModelAndView within a controller method does the existing Model get overwritten ?

  • 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-25T17:12:22+00:00Added an answer on May 25, 2026 at 5:12 pm

    I’ll try to answer some of your questions.

    About unit testing the controllers, sometimes I found that it’s needed to add Spring’s Mock request and response, as some spring features expect the request and response in the request context.

    class MyTest {
        private MockHttpServletRequest request;
        private MockHttpServletResponse response;
    
        @BeforeMethod(alwaysRun = true)
        public void setup() {
            request = new MockHttpServletRequest();
            response = new MockHttpServletResponse();
            controller = new Controller( /*inject dependencies here*/ );
        }
    }
    

    This is easy to figure out, because if they are not there, you’ll get a very specific error saying that the request or response are not in the context.

    Related to the model, I found easier to return a ModelAndView and add some tests that check the output parameters and view name.

    public void shows_xxx_index_view {
        assertThat(modelAndView.getViewName(), equalTo("controller/index"));
    }
    
    public void model_contains_search_results {
        assertThat(modelAndView.getModelMap().get("searchResults"), equalTo(expected_results));
    }
    

    And I don’t know if the model gets overwritten.

    This is my experience from a small project, so I’m by no means an expert, but I found this approach useful.

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

Sidebar

Related Questions

It's my understanding that in Spring, all objects are treated by default as singletons.
Its my understanding that date() and time() functions native to php will give me
Its my understanding that the questions in StackOverflow has the following format http://stackoverflow.com/questions/{question-id}/{slug-made-from-question-title} So
Its my understanding that the recommended approach to localization in WPF is to use
Its my understanding that by adding the ScaffoldColumn(false) annotation to an property in a
It's my understanding that nulls are not indexable in DB2, so assuming we have
It's my understanding that common wisdom says to only use exceptions for truly exceptional
It's my understanding that StackOverflow (SO) was built using ASP.NET. What surprised me is
It's my understanding that all three of these lines below should return an ARRAY
It's my understanding that all .NET object instances begin with an 8 byte 'object

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.