Spring boot:单元测试和配置文件

时间:2016-10-25 16:07:19

标签: java spring unit-testing configuration

我正在为一个休息控制器进行单元测试,这只是一个更大的应用程序的一小部分 我的测试上下文未被我的应用程序识别,并且我有以下异常: java.lang.IllegalStateException:无法加载ApplicationContext

这是我的测试类:

测试RestController

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(locations = "classpath:/META-INF/spring/context-test.xml")
@WebIntegrationTest
public class MyRestControllerTest extends AbstractTransactionnalTest {

  @Autowired
  private IManager manager;

  @Test
  // my unit tests
}

问题是,如果我使用locations = "classpath:/META-INF/spring/context-test.xml"代替classes = Production.class以下应用程序类,它可以正常工作:

@Configuration
@EnableAutoConfiguration
@EnableTransactionManagement
@EnableScheduling
@ImportResource({ "classpath:/META-INF/spring/context-production.xml" })
public class Production {
  // class content
}

我已经阅读了所有类似问题的帖子,我知道它与@Configuration@EnableAutoConfiguration注释相关联但是当我尝试使用这些注释并导入了自定义配置类时来自context.xml的设置不起作用。
理想情况下,我希望不添加任何配置类,并且只想在我的test-context.xml中添加一个bean 是否可以使用context.xml中的bean或TestRestController上的注释解决此问题?

这是我的堆栈跟踪:

java.lang.IllegalStateException: Failed to load ApplicationContext

    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
   ... 26 more
Caused by: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    ... 35 more
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:185)
    ... 39 more

以下是我在test-context.xml中模拟管理器的bean:

<bean id="IManager"
        class="org.mockito.Mockito" factory-method="mock">
    <constructor-arg value="com.service.impl.Manager"/>

更新:

我尝试使用自定义管理器模拟,其中数据库被替换为列表 如果我删除注释@WebIntegrationTest,应用程序上下文会正确加载,但是我得到另一个例外,因为服务器没有@WebIntegrationTest注释而没有启动。

网络地址的GET请求上的I / O错误:连接被拒绝

我在春季1.3.7上运行。

1 个答案:

答案 0 :(得分:3)

@ContextConfiguration定义类级元数据,用于确定如何为集成测试加载和配置ApplicationContext。具体来说,@ ContextConfiguration声明应用程序上下文资源位置或将用于加载上下文的带注释的类。

@ContextConfiguration("/test-config.xml") 
public class XmlApplicationContextTests { 
    // class body... 
}

Spring Boot提供了一个 @SpringBootTest 注释可用作标准弹簧测试的替代方案 @ContextConfiguration 需要Spring Boot功能时的注释。注释的工作原理是通过SpringApplication创建测试中使用的ApplicationContext。

您可以使用@SpringBootTest的webEnvironment属性来进一步优化测试的运行方式。

Spring Boot的@ * Test annotations将在您未明确定义主要配置时自动搜索。

搜索算法从包含测试的包开始工作,直到找到@SpringBootApplication或@SpringBootConfiguration注释类。只要您以合理的方式构建代码,通常就可以找到主要配置。

如果要自定义主要配置,可以使用嵌套的@TestConfiguration类。与嵌套的@Configuration类不同,它将用于代替应用程序的主要配置,除了应用程序的主要配置之外,还将使用嵌套的@TestConfiguration类。

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html 40.2

相关问题