Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring引导:单元测试和配置文件_Java_Spring_Unit Testing_Configuration - Fatal编程技术网

Java Spring引导:单元测试和配置文件

Java Spring引导:单元测试和配置文件,java,spring,unit-testing,configuration,Java,Spring,Unit Testing,Configuration,我正在为rest控制器进行单元测试,它只是更大应用程序的一小部分。 我的应用程序无法识别我的测试上下文,我有以下异常:java.lang.IllegalStateException:未能加载ApplicationContext 这是我的测试课: 测试控制器 @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(locations = "classpath:/META-INF/spring/context-t

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

这是我的测试课:

测试控制器

@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导入设置的自定义配置类时,它不起作用。
理想情况下,我不希望添加任何配置类,只希望将bean添加到我的
testcontext.xml

是否可以使用my 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
注释服务器无法启动

获取网络地址请求时出现I/O错误:连接被拒绝


我在spring 1.3.7上运行。

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

弹簧靴提供了一个 @春靴测试 可作为标准弹簧试验的替代品的注释 @上下文配置 需要Spring启动功能时的注释。注释的工作原理是通过SpringApplication创建测试中使用的ApplicationContext

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

Spring Boot的@*测试注释将在您没有明确定义主配置时自动搜索主配置

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

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


40.2

我根据你的建议尝试了许多解决方案,但迄今为止没有一个有效,你能提供一个例子吗?
@ContextConfiguration("/test-config.xml") 
public class XmlApplicationContextTests { 
    // class body... 
}