我在使用Spring Framework 3.2实现Spring MVC测试时遇到了一些问题(跟在http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/testing.html#spring-mvc-test-framework之后)。MVC测试控制器ClassNotFoundException
总结:
我使用Maven 3.0.4。
我有多个使用Spring Framework的项目。相关项目层次是:
- 共同
- 道
- web服务(使用普通和DAO)
在web服务,我有一个控制器
package com.lalala;
...
@Controller
@RequestMapping("/")
public class WebServicesController {
@Autowired private WebService webService;
//some methods...
}
WebService类是声明为接口并具有实现。
现在,测试:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("/testApplicationContext.xml")
public class WebServicesTest {
private MockMvc mockMvc;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.standaloneSetup(new WebServicesController()).build();
}
@Test
public void inexistentService() throws Exception {
mockMvc.perform(get("webservices/pong").accept(MediaType.APPLICATION_JSON)).andExpect(status().is(500)).andDo(print());
}
}
和配置文件testApplicationContext.xml:
<beans ... >
<import resource="classpath:spring-hibernate-test.xml"/>
<tx:annotation-driven/>
<context:annotation-config />
<context:component-scan base-package="com.lalala"/>
</beans>
当我尝试启动Eclipse中的测试中,我有一个java .lang.NoClassDefFoundError: 引起:java.lang.ClassNotFoundException:com.lalala.WebServicesController。有人能告诉我我的配置和测试有什么问题吗?
(在那旁边,项目工作正常,一旦部署在Tomcat和只知道,如果我写的不对的测试,尝试启动其在詹金斯完美运行已有的测试。他们没有在本地工作。)
您是否也在使用Maven? –
@AyubMalik是的我使用Maven 3.0.4 – user2508244
尝试'项目>清洁' – jchips12