gpt4 book ai didi

unit-testing - @WebAppConfiguration 未注入(inject)

转载 作者:行者123 更新时间:2023-12-03 14:41:35 25 4
gpt4 key购买 nike

我正在尝试使用 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();
}
}
这是我相关的 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 注入(inject)它。但是当我在 Eclipse 中运行这个测试时,我得到:

Caused by:org.springframework.beans.factory.NoSuchBeanDefinitionException: Noqualifying bean of type[org.springframework.web.context.WebApplicationContext] found fordependency: expected at least 1 bean which qualifies as autowirecandidate for this dependency. Dependency annotations:{@org.springframework.beans.factory.annotation.Autowired(required=true)}atorg.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:967)atorg.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:837)atorg.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:749)atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)


更新 - 我必须更改我的测试 Java 配置类
@Configuration
@EnableWebMvc
@ComponentScan( basePackages = { "...." } )
@EnableTransactionManagement
@ImportResource( "classpath:applicationContext.xml" )
public class JpaTestConfig extends WebMvcConfigurationSupport {
但是,现在的问题是我可以调用我的 REST 服务,但它正在调用其他一些服务,包括数据库调用。仅测试调用和模拟响应的首选方法是什么。我想测试有效和无效条件。

最佳答案

在我的情况下,问题已通过替换解决:

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


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

注意 网站 在加载器类名中。使用以前的加载程序, GenericApplicationContext尽管 @WebAppConfiguration 已被注入(inject)注解。

关于unit-testing - @WebAppConfiguration 未注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16136376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com