WebAppConfiguration未注入

WebAppConfiguration未注入

本文介绍了@WebAppConfiguration未注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Spring 3.2.1创建spring-mvc测试.在学习了一些教程之后,我认为这将是直截了当的.

I am trying to create spring-mvc tests using Spring 3.2.1. Following some tutorials, I thought this would be straight-forward.

这是我的考试:

@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();
}
}

这是我相关的pom.xml:

Here is my relevant 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>

我有以下测试配置类:

@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
}

据我了解,添加@WebAppConfiguration将迫使Spring注入它.但是,当我从Eclipse中运行此测试时,我得到:

It is my understanding that adding @WebAppConfiguration will force Spring to inject it. But when I run this test from within Eclipse I get:

更新-我必须更改测试Java配置类

Update - I had to change my Test Java Configuration class

@Configuration
@EnableWebMvc
@ComponentScan( basePackages = { "...." } )
@EnableTransactionManagement
@ImportResource( "classpath:applicationContext.xml" )
public class JpaTestConfig extends WebMvcConfigurationSupport {

但是,现在的问题是我可以调用我的REST服务,但是它正在调用其他一些服务,包括数据库调用.仅测试呼叫和模拟响应的首选方法是什么.我想测试有效条件和无效条件.

However, the problem is now that I can call my REST service, but it is invoking some other services, including database calls. What is the preferred way to just test the call and a mocked response. I would like to test valid and invalid conditions.

推荐答案

就我而言,问题已通过替换来解决:

In my case problem has been solved by replacing:

@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = { ... })

使用

@ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, classes = { ... })

请注意加载程序类名称中的 Web .在以前的加载程序中,尽管已注释了@WebAppConfiguration,但仍注入了GenericApplicationContext.

Notice the Web in the loader class name. With the previous loader, the GenericApplicationContext has been injected despite @WebAppConfiguration annotation.

这篇关于@WebAppConfiguration未注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:46