0

Spring MVC 3.1を使用していて、LoginControllerというコントローラークラスをテストしようとしています。メソッドがLoginControllerクラスでAutowiredされている参照authenticationServiceを使用して呼び出しを行おうとすると、authenticateメソッドでnullポインター例外が発生します。私はこのリンクとこれを参照しまし。明確にするために以下のコードを参照してください

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);
    }
   }
  }


サーブレット-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"/>

4

1 に答える 1

1

web.xmlファイルを介して宣言されたサービスBeanのBean定義(AuthenticationServiceなど)を含む別のファイルがある可能性があります。

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

その場合は、テストにもこの構成を含める必要があります。

また、Spring mvcテストの場合は、代わりにspring-test-mvcを使用することを強くお勧めします。

于 2012-10-15T19:24:47.143 に答える