【问题标题】:How do I autowire a field when running Spring4JUnitRunner test?运行 Spring4JUnitRunner 测试时如何自动装配字段?
【发布时间】:2012-03-08 21:46:56
【问题描述】:

我正在使用 Spring 3.1.0.RELEASE。我在从 Spring4JUnitRunner 类自动装配私有变量时遇到问题。我的 JUnit 类是 ...

@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注解...

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

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

【问题讨论】:

    标签: spring junit autowired


    【解决方案1】:

    您得到 null 是因为您自己实例化了 RegistrationController,而不是从 Spring 中获取 bean。你几乎自己想通了:

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

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

    @Autowired 
    private RegistrationController controller;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 2021-04-29
      相关资源
      最近更新 更多