springmvc 使用junit4对controller进行单元测试报错

这是我测试类的代码
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations={"classpath:spring-application.xml"})
@TransactionConfiguration(defaultRollback = true)
@Transactional
public class TestTwo {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = webAppContextSetup(this.wac).build();
}
@Test
public void testLogin() throws Exception {
mockMvc.perform((post("/loginTest").param("userName","admin").param("password","123456"))).andExpect(status().isOk()) .andDo(print());
}
}

spring-application配置文件 由于文字限制 去掉了头文件
<!-- 包扫描 -->
<context:component-scan base-package="com.hh.*" ></context:component-scan>
<mvc:annotation-driven />

<!-- 视图返回 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
使用测试类的时候报错:

BeanCreationException: Error creating bean with name 'com.tt.controller.TestTwo': Injection of autowired dependencies failed; nested exception is ...
貌似是WebApplicationContext 不能注入。求大神指教啊!!!

第1个回答  2016-07-28
WebApplicationContext 是配置文件吗?是的话放在@ContextConfiguration(locations={"classpath:spring-application.xml"})里面看看
或者把private WebApplicationContext wac;
改成
private WebApplicationContext wbApplicationContext;
相似回答