我试图为我的控制器运行一些测试但不知道@Autowire注释不起作用。
以下是我要做的事情:
@WebAppConfiguration
@ContextConfiguration("/WEB-INF/spring/app-config.xml")
public class ClientsTest {
private Client client = new Cliente();
@Test
public void test() {
BindingResult result = mock(BindingResult.class);
ClientController clientController = new ClientController();
ModelAndView model = clientController.regClient(client, result);
Assert.assertEquals("success", model.getViewName());
}
}
@Controller
public class ClientController {
@Autowired private ClientService clientService;
@RequestMapping(value="/regClient.html", method = RequestMethod.POST)
public ModelAndView regClient(@ModelAttribute("client") @Valid Client client, BindingResult result){
ModelAndView model = new ModelAndView();
if(result.hasErrors())
{
model.setViewName("error");
}
else
{
model = clientService.regClient(client);
model.setViewName("success");
}
return model;
}
}
<?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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="com.app.app_v2.web" />
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>client</value>
</list>
</property>
</bean>
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<set>
<ref bean="clientFormatter"/>
</set>
</property>
</bean>
<bean id="clientFormatter" class="com.app.app_v2.spring.ClientFormatter"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="clientService" class="com.app.app_v2.services.ClientService"/>
</beans>
据我所知,clientService为null,这就是我获得异常的原因。 因为我对运行测试了解不多,所以我会请求你的帮助。
答案 0 :(得分:1)
@Autowire 无法正常工作的直接问题与您明确实例化clientController而不是通过Spring框架这一事实有关。如果你想让这个类成为一个Spring bean,你需要让Spring框架管理它的生命周期,而不是依赖注入将启动,并且所有注入了 @Autowire 的spring bean都将被注入。此外,测试应该使用弹簧滑轮SpringJUnit4ClassRunner
进行请注意,这不是必需的,因为您可以通过两种方式进行测试。更多关于单元测试,通过模拟您的客户端服务,例如通过EasyMock或Mockito。您当前发布的测试看起来更适合这种方式。要了解如何完成测试,请查看此blog post
另一方面,您可以进行集成测试。从版本3.2开始,Spring MVC提供了测试模块,它应该是针对Spring MVC编写集成测试的方式。您可以关注支持源代码的优秀blog series来学习如何操作。