简体   繁体   English

具有自定义应用程序上下文的 Spring 集成测试

[英]Spring integration test with custom application context

For my application I created my own type of ApplicationContext that allows me to interact in specific manners that are needed for may application.对于我的应用程序,我创建了我自己的ApplicationContext类型,它允许我以可能应用程序所需的特定方式进行交互。 As the application is a desktop application, I create the context like this:由于该应用程序是桌面应用程序,因此我创建了这样的上下文:

@SpringBootApplication
@Import(StandaloneConfiguration.class)
@PropertySource(value = {"application.properties", "server.properties"})
public class OpenPatricianApplication extends Application {
    private ApplicationContext context;
    @Override
    public void init() {
        SpringApplicationBuilder builder = new SpringApplicationBuilder(OpenPatricianApplication.class);
        context = builder.contextClass(DependentAnnotationConfigApplicationContext.class).run(getParameters().getRaw().toArray(new String[0]));
        // more initialisation

        }
    }
}

Now I want to create a Spring Boot integration test that actually relies on the functionality of my own ApplicationConext implementation.现在我想创建一个 Spring Boot 集成测试,它实际上依赖于我自己的ApplicationConext实现的功能。

@SpringBootTest(classes = {ServerTestConfiguration.class})
public class ServerIntegrationTest {
    private DependentAnnotationConfigApplicationContext context;
}

How do I go about initializing my context in the test?如何在测试中初始化我的context The context must be created in order to start the spring application for this to work, but with the SpringBootTest annotation this already happened, when the constructor is entered.必须创建context才能启动 spring 应用程序,但是当输入构造函数时,使用SpringBootTest注释这已经发生了。 Are there any additional annotations or parameter for existing ones that can be applied?是否有任何可以应用的现有注释或参数? Should tests of these nature not be annotated with SpringBootTest at all and the application created manually?这些性质的测试是否应该完全不使用SpringBootTest注释并手动创建应用程序?

The approach that I found to solve this issue is to forgo the SpringBootTest annotation altogether and construct the context as part of the constructor.我发现解决此问题的方法是完全放弃SpringBootTest注释,并将上下文构造为构造函数的一部分。 Alternatively you could also do it in the BeforeAll or BeforeEach method, but as my test class extends a base class that needs some beans injected, the constructor seemed the right choice.或者,您也可以在BeforeAllBeforeEach方法中执行此操作,但由于我的测试类扩展了一个需要注入一些 bean 的基类,因此构造函数似乎是正确的选择。

However what does not work is injecting the beans in the super class by way of constructor injection, as the call to the super constructor has to be the first call in the constructor and that would necessitate to have a static initializer block for the context and I want to avoid static stuff as much as possible, especially if the context is not properly cleaned up at the end of the test, it would live on as part of the loaded class in memory and potentially consume lot of memory.然而,通过构造函数注入的方式在超类中注入 bean 是行不通的,因为对超级构造函数的调用必须是构造函数中的第一个调用,这将需要为上下文提供一个静态初始化块,我想要尽可能避免静态的东西,特别是如果在测试结束时没有正确清理上下文,它将作为内存中加载类的一部分存在并可能消耗大量内存。

So here is the code:所以这是代码:

public class ServerIntegrationTest extends SaveLoadBase<CityWall> {

    public CityWallSerializationTest() {
        SpringApplicationBuilder builder = new SpringApplicationBuilder(ServerTestConfiguration.class);
        DependentAnnotationConfigApplicationContext context = (DependentAnnotationConfigApplicationContext) builder.contextClass(DependentAnnotationConfigApplicationContext.class).run();
        setContext(context);
        setClientServerEventBus((AsyncEventBus) context.getBean("clientServerEventBus"));
        setLoadAndSaveService(context.getBean(TestableLoadAndSaveService.class));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用上下文层次结构子上下文bean作为应用程序侦听器的Spring集成测试 - Spring integration test with context hierarchy child context bean as application listener 使用@ContextConfiguration运行Spring集成测试时,应用程序上下文为null - Application context is coming as null while running spring integration test with @ContextConfiguration Spring自定义注释集成测试 - Spring Custom Annotation Integration Test 使用工具加载Spring集成测试上下文 - Loading Spring integration test context with instrumentation 每次集成测试后 Spring 上下文变脏 - Spring context dirty after each integration test Spring测试/生产应用程序上下文 - Spring test/production application context 由于属性文件位置类型,应用程序或集成测试Spring上下文创建失败 - Either application or integration test Spring context creation fails due to properties file location type 用于应用程序上下文的Bean引用的Spring集成表达式 - Spring Integration Expression For Bean References on Application Context 如何对 Spring Boot 应用程序进行集成测试? - How to do an integration test for a Spring Boot application? 使用Flowable在Spring应用程序中进行集成测试 - Integration test in Spring application using Flowable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM