son*_*rin 15 unit-testing spring-mvc spring-mvc-test
我正在尝试使用Spring 3.2.1创建spring-mvc测试.在一些教程之后,我认为这将是直截了当的.
这是我的测试:
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( loader = AnnotationConfigContextLoader.class, classes = { JpaTestConfig.class } )
@WebAppConfiguration
public class HomeControllerTest {
@Resource
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Test
public void testRoot() throws Exception {
mockMvc.perform(get("/").accept(MediaType.TEXT_PLAIN)).andDo(print())
// print the request/response in the console
.andExpect(status().isOk()).andExpect(content().contentType(MediaType.TEXT_PLAIN))
.andExpect(content().string("Hello World!"));
}
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的相关pom.xml:
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>hamcrest-core</artifactId>
<groupId>org.hamcrest</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.1.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我有以下测试配置类:
@Configuration
@EnableTransactionManagement
@ComponentScan( basePackages = { "com.myproject.service", "com.myproject.utility",
"com.myproject.controller" } )
@ImportResource( "classpath:applicationContext.xml" )
public class JpaTestConfig {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
...
}
// various other services/datasource but not controllers
}
Run Code Online (Sandbox Code Playgroud)
据我所知,添加@WebAppConfiguration
会强制Spring注入它.但是当我从Eclipse中运行这个测试时,我得到:
org.springframework.beans.factory.NoSuchBeanDefinitionException:通过引起[org.springframework.web.context.WebApplicationContext]找到依赖性否类型的排位豆:预期至少1种豆,其有资格作为自动装配候选这种依赖性.依赖注解:{@ org.springframework.beans.factory.annotation.Autowired(所需=真)}在org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:967)在org.springframework.beans. or.s.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:749)org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement的factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:837).注(AutowiredAnnotationBeanPostProcessor.java:486)
更新 - 我不得不更改我的Test Java Configuration类
@Configuration
@EnableWebMvc
@ComponentScan( basePackages = { "...." } )
@EnableTransactionManagement
@ImportResource( "classpath:applicationContext.xml" )
public class JpaTestConfig extends WebMvcConfigurationSupport {
Run Code Online (Sandbox Code Playgroud)
但是,现在的问题是我可以调用我的REST服务,但它正在调用其他一些服务,包括数据库调用.仅测试呼叫和模拟响应的首选方法是什么.我想测试有效和无效的条件.
fra*_*acz 23
在我的情况下,问题已通过替换:
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = { ... })
Run Code Online (Sandbox Code Playgroud)
同
@ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, classes = { ... })
Run Code Online (Sandbox Code Playgroud)
注意加载器类名中的Web.使用前一个加载器,GenericApplicationContext
尽管@WebAppConfiguration
注释已经注入.
以下设置仅使用Java配置类,对我来说工作正常.
@WebAppConfiguration
@ContextConfiguration(classes = TestApplicationContext.class)
public class MyClassTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Before
public void setUp() {
mockMvc = webAppContextSetup(wac).build();
}
....
}
@Configuration
public class TestApplicationContext {
@Bean
public MyBean myBeanId(){
return Mockito.mock(MyBean.class);
}
....
}
Run Code Online (Sandbox Code Playgroud)
仅在测试类上存在@WebAppConfiguration可确保使用到Web应用程序根目录的默认路径为测试加载WebApplicationContext.因此,您可以自动装配WebApplicationContext并使用它来设置mockMvc.
请注意,@ WebAppConfiguration必须与测试类中的@ContextConfiguration结合使用.
为什么不添加这个注释并看看它是否有效。将 XXXX-text.xml 替换为您的 bean 映射 xml。
@ContextConfiguration(locations={"classpath:/XXXX-test.xml"})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
20457 次 |
最近记录: |