Spring 3.2.1を使用してspring-mvcテストを作成しようとしています。いくつかのチュートリアルに続いて、私はこれが簡単だと思いました。ここで@WebAppConfigurationが注入されていません
は私のテストである:ここでは
@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です:
<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内からこのテストを実行するときしかし、私は得る:
: org.springframework.beans.factory.NoSuchBeanDefinitionException:タイプ なし 予選豆[org.springframework.web.context.WebApplicationContext] 依存関係が見つかりました:autowireとなる少なくとも1つのbeanが必要です。この依存関係の候補は です。依存注釈: でorg.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:967) で{@ org.springframework.beans.factory.annotation.Autowired(=真必須)} ORG org.springframework.beansで.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:749)で(DefaultListableBeanFactory.java:837) .factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
アップデート - 私は
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "...." })
@EnableTransactionManagement
@ImportResource("classpath:applicationContext.xml")
public class JpaTestConfig extends WebMvcConfigurationSupport {
を私のテストのJava Configurationクラスを変更しなければならなかったしかし、問題は、私は私のRESTサービスを呼び出すことができるということになりましたが、それはデータベース呼び出しを含むいくつかの他のサービスを、起動されます。コールと嘲笑された応答をテストするための好ましい方法は何ですか?有効な条件と無効な条件をテストしたいと思います。
私は豆が@Configurationにマッピングされてい: – sonoerin
それは私が私のテストの設定クラスに以下を追加する必要が判明: @EnableWebMvc @ComponentScan(...) JpaTestConfigはWebMvcConfigurationSupport – sonoerin
良い検索を拡張します。あなたの質問にこのアップデートを加えてください。または、ビット詳細で質問に答えてみてください。 –