@WebAppConfiguration nie został wstrzyknięty

Próbuję tworzyć testy wiosna-mvc za pomocą Spring 3.2.1. Po kilku samouczkach pomyślałem, że to będzie proste.

Oto mój 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();
}
}

Oto mój odpowiedni plik 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>

Mam następującą klasę konfiguracji testowej:

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

Rozumiem, że dodawanie@WebAppConfiguration zmusi Springa do wstrzyknięcia. Ale kiedy uruchamiam ten test z poziomu Eclipse, otrzymuję:

Przyczyna: org.springframework.beans.factory.NoSuchBeanDefinitionException: nie znaleziono kwalifikującego się komponentu bean typu [org.springframework.web.context.WebApplicationContext] dla zależności: oczekiwano co najmniej 1 komponentu bean, który kwalifikuje się jako kandydat do automatycznego potwierdzenia dla tej zależności. Adnotacje zależności: {@ org.springframework.beans.factory.annotation.Autowired (required = true)} w org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException (DefaultListableBeanFactory.java:967) w org.springframework.beans. factory.support.DefaultListableBeanFactory.doResolveDependency (DefaultListableBeanFactory.java:837) w org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency (DefaultListableBeanFactory.java:749) w org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement. inject (AutowiredAnnotationBeanPostProcessor.java:486)

Aktualizacja - Musiałem zmienić klasę testowej konfiguracji Java

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

Jednak problem polega na tym, że mogę wywołać moją usługę REST, ale wywołuje ona inne usługi, w tym wywołania bazy danych. Jaki jest preferowany sposób sprawdzenia połączenia i wyśmiewanej odpowiedzi. Chciałbym przetestować ważne i nieprawidłowe warunki.

questionAnswers(3)

yourAnswerToTheQuestion