@WebAppConfiguration nicht injiziert

Ich versuche, Spring-MVC-Tests mit Spring 3.2.1 zu erstellen. Nach einigen Tutorials dachte ich, dass dies einfach sein würde.

Hier ist mein Test:

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

Hier ist meine relevante 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>

Ich habe folgende Testkonfigurationsklasse:

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

Ich verstehe das Hinzufügen@WebAppConfiguration wird Spring zwingen, es zu injizieren. Aber wenn ich diesen Test in Eclipse durchführe, erhalte ich:

Auslöser: org.springframework.beans.factory.NoSuchBeanDefinitionException: Es wurde keine qualifizierende Bean des Typs [org.springframework.web.context.WebApplicationContext] für die Abhängigkeit gefunden: Es wird mindestens 1 Bean erwartet, die sich als Autowire-Kandidat für diese Abhängigkeit qualifiziert. Abhängigkeitsanmerkungen: {@ org.springframework.beans.factory.annotation.Autowired (erforderlich = true)} bei org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException (DefaultListableBeanFactory.java:967) bei org.spreans.spring factory.support.DefaultListableBeanFactory.doResolveDependency (DefaultListableBeanFactory.java:837) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency (DefaultListableBeanFactory.java:749) at org.spring inject (AutowiredAnnotationBeanPostProcessor.java:486)

Aktualisieren - Ich musste meine Test Java Configuration Klasse ändern

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

Das Problem ist jedoch, dass ich jetzt meinen REST-Service aufrufen kann, aber er einige andere Services aufruft, einschließlich Datenbankaufrufe. Was ist der bevorzugte Weg, um nur den Anruf und eine verspottete Antwort zu testen. Ich möchte gültige und ungültige Bedingungen testen.

Antworten auf die Frage(3)

Ihre Antwort auf die Frage