2012-03-08 48 views
0

我正在使用Spring 3.1.0.RELEASE。我无法从Spring4JUnitRunner类自动装配私有变量。我的JUnit类是...运行Spring4JUnitRunner测试时,如何自动装入字段?

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration({ "file:src/main/webapp/WEB-INF/dispatcher-servlet.xml" }) 
public class RegistrationControllerTest { 

@Autowired 
private ApplicationContext applicationContext; 

private MockHttpServletRequest request; 
private MockHttpServletResponse response; 
private HandlerAdapter handlerAdapter; 
private RegistrationController controller; 

@Before 
public void setUp() { 
    request = new MockHttpServletRequest(); 
    response = new MockHttpServletResponse(); 
    handlerAdapter = applicationContext.getBean(HandlerAdapter.class); 
    // I could get the controller from the context here 
    controller = new RegistrationController(); 
    final RegistrationValidation registrationValidation = new RegistrationValidation(); 
    controller.setRegistrationValidation(registrationValidation); 
} 

不知道它的相关性,但这里是我的dispathcer-servlet.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" 
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"> 

<!-- Enable annotation driven controllers, validation etc... --> 
<mvc:annotation-driven /> 

<context:component-scan base-package="com.myco.eventmaven" /> 

<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix"> 
     <value>/WEB-INF/views/</value> 
    </property> 
    <property name="suffix"> 
     <value>.jsp</value> 
    </property> 
</bean> 

<bean id="messageSource" 
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <property name="basename" value="/messages" /> 
</bean> 

</beans> 

这里是我的控制器。该“usersDao”字段是我的测试过程中无效,会导致NullPointerExceptions(正常工作,当我运行在JBoss的一个正常的Web应用程序).​​..

@Controller 
@RequestMapping("/registrationform.jsp") 
public class RegistrationController { 

private static Logger LOG = Logger.getLogger(RegistrationController.class); 

@Autowired 
private RegistrationValidation registrationValidation; 

@Autowired 
private UsersDao usersDao; 

public void setRegistrationValidation(
     RegistrationValidation registrationValidation) { 
    this.registrationValidation = registrationValidation; 
} 

// Display the form on the get request 
@RequestMapping(method = RequestMethod.GET) 
public String showRegistration(Map model) { 
    LOG.debug("called GET method."); 
    final Registration registration = new Registration(); 
    model.put("registration", registration); 
    return "user/registrationform"; 
} 

// Process the form. 
@RequestMapping(method = RequestMethod.POST) 
public String processRegistration(Registration registration, BindingResult result) throws NoSuchAlgorithmException, UnsupportedEncodingException { 
    String nextPage = "user/registrationform"; 

    // set custom Validation by user 
    registrationValidation.validate(registration, result); 
    if (result.hasErrors()) { 
     return nextPage; 
    } else { 
     // Save the user to the database. 
     if (usersDao.saveUser(registration)) { 
      nextPage = "user/registrationsuccess"; 
     } // if 
    } // if 
    return nextPage; 
} 

类成员场,usersDao的,被标注与@组件注释...

@Component("usersDao") 
public class UsersDaoImpl implements UsersDao { 

我需要添加哪些额外的配置才能在我的JUnit类中正确自动装载dao对象?谢谢, -

回答

4

你会得到null,因为你自己实例化了RegistrationController,而不是从Spring获取bean。你几乎已经明白了这一点:

// I could get the controller from the context here 
controller = new RegistrationController(); 

你可以,你应该。删除这两行,并在字段声明中使用以下内容:

@Autowired 
private RegistrationController controller;