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 8723185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:34:17+00:00 2026-06-13T07:34:17+00:00

I am trying to create a test for my controller. Here’s what I have

  • 0

I am trying to create a test for my controller. Here’s what I have similar. Just name changes. I am using Mockito and Spring MVC. The test configuration files have the autowired beans in them mocked via mock factory. I get a null pointer…

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
        ...
 })
public class MyReportControllerTest {

private MockHttpServletRequest request;
private MockHttpServletResponse response;
private MockHttpSession session;
private HandlerAdapter handlerAdapter;

@Autowired
private ApplicationContext applicationContext;

@Autowired
private MyService myService;

@Autowired
private RequestMappingHandlerMapping rmhm;

@Before
public void setUp() throws Exception {
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
    session = new MockHttpSession();
    handlerAdapter = applicationContext
            .getBean(RequestMappingHandlerAdapter.class);

    request.setSession(session);
    request.addHeader("authToken", "aa");

    Mockito.when(
            myService.getMyInfo(YEAR))
            .thenReturn(getMyInfoList());
}
@Test
public void testGetMyInfo(){
    request.setRequestURI("/getMyInfo/" + 2011);
    request.setMethod("GET");

    try {
        if( handlerAdapter == null){
            System.out.println("Handler Adapter is null!");
        }
        if( request == null){
            System.out.println("Request is null!");
        }
        if( response == null){
            System.out.println("Response is null!");
        }
        if( rmhm.getHandler(request) == null){
            System.out.println("rmhm.getHandler(request) is null!");
        }
        //the above returns null
        System.out.println("RMHM: " + rmhm.toString());
        System.out.println("RMHM Default Handler: " + rmhm.getDefaultHandler());
        handlerAdapter.handle(request, response, 
                rmhm.getHandler(request)
                .getHandler());//null pointer exception here <---

        ...


    } catch (Exception e) {
        e.printStackTrace();
        fail("getMyReport failed. Exception");
    }

}

public List<MyInfo> getMyInfoList(){...}

I have done a thorough debugging and found that the Handler remains null for my mock request. What am I missing that it doesn’t get turned into a handler, or even goes to the default handler?

  • 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-06-13T07:34:18+00:00Added an answer on June 13, 2026 at 7:34 am

    You’ve got a number of problems here.

    Firstly, are you attempting to unit test your Controller, or integration test it?
    It certainly looks more like an integration test; you’ve got Spring @Autowired annotations and a @ContextConfiguration.

    But if that’s the case, why are you attempting to define mock behaviour on myService? That’s never going to work – Spring will inject a “real” instance and Mockito has no hope of working its magic on that.

    Related; you are missing any kind of mock initialization call which would be required if you want that to work.

    Finally, why are you doing all of this wiring up (HandlerMappings, HandlerAdapters etc) when judging by the name of your test, all you really want to do is test your MyReportController? Can you not simply call the necessary “endpoint” method with a mock request, response etc as required?

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

Sidebar

Related Questions

I'm trying to create a very simple REST server. I just have a test
I am trying to test my controller's create action. I am using Devise for
I have the below configuration where I'm trying to create a test C function
I am trying to create a test harness for our QA staff to test
I'm trying to create an Android test project from within my main project. Exactly
I am trying to create a unit test for a method that takes a
I'm trying to create an UIImage test pattern for an iOS 5.1 device. The
Specifically, I'm trying to create a unit test for a method which requires uses
I am trying to learn how to create and test for the expiration of
I'm trying to create a mock HttpContextBase for unit test. var fakePrinciple = new

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.