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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:59:07+00:00 2026-06-12T21:59:07+00:00

I’m using Spring MVC 3.1 and I am attempting to test a controller class

  • 0

I’m using Spring MVC 3.1 and I am attempting to test a controller class called LoginController. I get a null pointer exception in the authenticate method when the method attempts to make a call using reference authenticationService, which is Autowired in the LoginController class. I have referenced this link as well as this one . Please see code below for clarity

LoginControllerTest.java

 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(locations = {"classpath:servlet-context.xml"})
public class LoginControllerTest extends DatabaseSupport {

@Autowired
private ApplicationContext applicationContext;

@Autowired
private LoginController loginController;

 @Autowired
private AuthenticationService authenticationService;

private MockHttpServletRequest request;
private MockHttpServletResponse response;
private RequestMappingHandlerAdapter handlerAdapter;

private LoginForm loginForm;


@Before
public void setUp() {
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();

    handlerAdapter = applicationContext.getBean(RequestMappingHandlerAdapter.class);
    authenticationService = applicationContext.getAutowireCapableBeanFactory()
                           .createBean(AuthenticationService.class);
    loginController = new LoginController();
    loginForm = new LoginForm();

}


@Test
public void loginTest() throws Exception {
    final String expectedMessage = "login";
    final String requestUri = "/";
    final HandlerMethod expectedHandlerMethod;

    final MockHttpServletRequest request;
    final MockHttpServletResponse response;

    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
    request.setMethod("GET");

    request.setRequestURI(requestUri);

    expectedHandlerMethod = new HandlerMethod(loginController, "login");
    ModelAndView mav = handlerAdapter.handle(request, response, expectedHandlerMethod); //this.getHandler(request);


    assertViewName(mav, expectedMessage);
}


@Test
public void logoutTest() throws Exception {
    final String expectedMessage = "login";
    final String requestUri = "/";
    final HandlerMethod expectedHandlerMethod;

    final MockHttpServletRequest request;
    final MockHttpServletResponse response;

    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
    request.setMethod("GET");

    request.setRequestURI(requestUri);

    expectedHandlerMethod = new HandlerMethod(loginController, "logout");
    ModelAndView mav = handlerAdapter.handle(request, response, expectedHandlerMethod); //this.getHandler(request);


    assertViewName(mav, expectedMessage);
}


@Test
public void authenticateTest() throws Exception {
    //final String expectedMessage = "redirect:search/";
    final String requestUri = "/";
    final HandlerMethod expectedHandlerMethod;
    final Map mav;

    final MockHttpServletRequest request;
    final MockHttpServletResponse response;

    loginForm.setUserName("abell");
    loginForm.setPassWord("PassW0rd");

    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
    request.setMethod("GET");
    request.setRequestURI(requestUri);
    mav = new HashMap();

      //authenticate(@ModelAttribute("loginForm") LoginForm loginForm, Map model, HttpServletRequest request)
    //expectedHandlerMethod = new HandlerMethod(loginController, "authenticate");
    //ModelAndView mav = handlerAdapter.handle(request, response, expectedHandlerMethod); //this.getHandler(request);
    String expectedMessage = loginController.authenticate(loginForm, mav, request);

    assertTrue(expectedMessage.equalsIgnoreCase("redirect:search/"));
}


private Object getHandler(MockHttpServletRequest request) throws Exception {
    HandlerExecutionChain chain = null;

    Map<String, HandlerMapping> map = applicationContext.getBeansOfType(HandlerMapping.class);
    Iterator<HandlerMapping> itt = map.values().iterator();


    while (itt.hasNext()) {
        HandlerMapping mapping = itt.next();
        chain = mapping.getHandler(request);
        if (chain != null) {
            break;
        }
    }

    if (chain == null) {
        throw new InvalidParameterException("Unable to find handler for request URI: " + request.getRequestURI());
    }

    return chain.getHandler();
   }
}

LoginController.java

@Controller
@SessionAttributes
@RequestMapping("/")
public class LoginController {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private AuthenticationService authenticationService;

    @Autowired
    private WloanInfoDao wloanInfoDao;

    @Autowired
    private UserProfile userProfile;


    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String login() {

            return "login";
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout() {
            logger.info("Logout");
            return "login";

    }

    @RequestMapping(value="/authenticate", method = RequestMethod.POST)
    public String authenticate(@ModelAttribute("loginForm") LoginForm loginForm, Map    model, HttpServletRequest request) throws AuthenticationException {
            try {
                    boolean authenticated = authenticationService.authenticate(loginForm.getUserName(), loginForm.getPassWord());

                    if (authenticated) {
                            userProfile.setUsername(loginForm.getUserName());
                            userProfile.setAuthenticated(true);
                            return "redirect:search/";
                    }

                    model.put("loginForm", loginForm);
                    request.setAttribute("errorMessage","Invalid username or password.");
            } catch (AuthenticationException e) {
                    logger.error("Error occurring during login: " + e.getMessage(), e);
                    throw new AuthenticationException(e);
            }
            return "login";
            }

             }

AuthenticationService.java

 @Service("authenticationService")
 public class AuthenticationService {

private static final Logger logger =    LoggerFactory.getLogger(AuthenticationService.class);
private static final String APPNAME = "ULDD";

public boolean authenticate(String username, String password) throws AuthenticationException {
    try {
        final ServiceConnectionInfo sci = new ServiceConnectionInfo();

        PropertyManager props = new DbPropertyManager(APPNAME);

        logger.info("Calling service authentication webservice: " + props.getString("auth.url"));
        logger.info("Authentication user " + username);

        sci.setUrl(props.getString("auth.url"));
        sci.setUsername(props.getString("auth.username"));
        sci.setPassword(props.getString("auth.password"));

        final IServiceClient serviceClient = new Axis1ServiceClient(sci);
        final AuthenticationServiceClient asc = new AuthenticationServiceClient(serviceClient, "AuthenticationServiceClientTest");

        final AuthServiceResponse ret;

        ret = asc.authenticate(username, password, AuthServiceRequest.Domain.INTERNAL, Arrays.asList(new String[] { APPNAME }));
        ret.next();

        return ret.isAuthenticated();

    } catch (ServiceClientException e) {
        throw new AuthenticationException("Error calling authentication service", e);
    } catch (Exception e) {
        throw new AuthenticationException("Error authenticating username: " + username, e);
    }
   }
  }

servlet-context.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven/>

    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <import resource="hibernate-context.xml"/>

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <mvc:resources mapping="/media/**" location="/media/"/>

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"/>
            <property name="suffix" value=".jsp"/>
    </bean>
    <!-- Spring is retarded, -->
    <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
            <property name="scopes">
                    <map>
                            <entry key="session">
                                    <bean class="org.springframework.context.support.SimpleThreadScope"/>
                            </entry>
                    </map>
            </property>
    </bean>

    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

            <!-- one of the properties available; the maximum file size in bytes -->
            <property name="maxUploadSize" value="100000"/>
    </bean>

    <context:component-scan base-package="com.everbank.uldd" scoped-proxy="targetClass"/>

  • 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-12T21:59:11+00:00Added an answer on June 12, 2026 at 9:59 pm

    You probably have another file with the bean definitions for your service beans – AuthenticationService etc, declared through the web.xml file:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>service..</param-value>
    </context-param>
    

    if so, you need to include this configuration also for your test.

    Also, for Spring mvc tests, I would highly recommend using spring-test-mvc instead.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
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'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.