springmvc 使用junit4对controller进行单元测试报错
这是我测试类的代码@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration@ContextConfigura...
这是我测试类的代码
@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 不能注入。求大神指教啊!!! 展开
@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 不能注入。求大神指教啊!!! 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励30(财富值+成长值)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询