GH-3679: Better caching for SpringIntegrationTest (#3792)

* GH-3679: Better caching for SpringIntegrationTest

Fixes https://github.com/spring-projects/spring-integration/issues/3679

The `@SpringIntegrationTest` makes a test cache key based on its attributes values
when the same application context can be used in different test classes
with different endpoints to have stopped originally.

* Rework an `IntegrationEndpointsInitializer` to the `SpringIntegrationTestExecutionListener`
which consult a `MockIntegrationContext` for endpoints to be started or not before
the test execution and definitely stopped after the test execution to have a flexibility
with the cached context
* Improve a `MockIntegrationContext` to gather `AbstractEndpoint` beans and have them
marked for stopping in the beginning of the application context.
The `SpringIntegrationTestExecutionListener` takes care about startup in its `beforeTestClass()`
* Verify different state for the `SpringIntegrationTest` with the `CachedSpringIntegrationTestAnnotationTests`

* * Improve `SpringIntegrationTestExecutionListener` performance
when no `@SpringIntegrationTest.noAutoStartup()` is configured
* Fix `CachedSpringIntegrationTestAnnotationTests` check `isRunning()`
for the endpoint under the testing instead of `isAutoStartup()`
which is changed in one test class, but not other, and the order
of their execution would matter
* Migrate `MockMessageSourceTests` to JUnit 5 as a roadmap of the whole project

* Fixes https://github.com/spring-projects/spring-integration/issues/3787
This commit is contained in:
Artem Bilan
2022-05-09 13:38:10 -04:00
committed by GitHub
parent 0ad89147b2
commit 18e410afbe
9 changed files with 155 additions and 141 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018-2019 the original author or authors.
* Copyright 2018-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,9 +20,11 @@ import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.test.context.SpringIntegrationTest;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -37,22 +39,28 @@ public abstract class CachedSpringIntegrationTestAnnotationTests {
private static int beanFactoryPostProcessorCallCounter;
@Autowired
protected AbstractEndpoint someEndpoint;
public static class SpringIntegrationTestAnnotationCaching1Tests
extends CachedSpringIntegrationTestAnnotationTests {
@Test
public void testSingleApplicationContext() {
assertThat(beanFactoryPostProcessorCallCounter).isEqualTo(1);
assertThat(this.someEndpoint.isRunning()).isTrue();
}
}
@SpringIntegrationTest(noAutoStartup = "someEndpoint")
public static class SpringIntegrationTestAnnotationCaching2Tests
extends CachedSpringIntegrationTestAnnotationTests {
@Test
public void testSingleApplicationContext() {
assertThat(beanFactoryPostProcessorCallCounter).isEqualTo(1);
assertThat(this.someEndpoint.isRunning()).isFalse();
}
}
@@ -65,6 +73,23 @@ public abstract class CachedSpringIntegrationTestAnnotationTests {
return beanFactory -> beanFactoryPostProcessorCallCounter++;
}
@Bean
public AbstractEndpoint someEndpoint() {
return new AbstractEndpoint() {
@Override
protected void doStart() {
}
@Override
protected void doStop() {
}
};
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2019 the original author or authors.
* Copyright 2017-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,9 +19,8 @@ package org.springframework.integration.test.mock;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.beans.factory.annotation.Autowired;
@@ -47,16 +46,14 @@ import org.springframework.integration.test.context.SpringIntegrationTest;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
/**
* @author Artem Bilan
*
* @since 5.0
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = MockMessageSourceTests.Config.class)
@SpringJUnitConfig(classes = MockMessageSourceTests.Config.class)
@SpringIntegrationTest(noAutoStartup = { "inboundChannelAdapter", "*Source*" })
@DirtiesContext
public class MockMessageSourceTests {
@@ -73,7 +70,7 @@ public class MockMessageSourceTests {
@Autowired
private IntegrationFlowContext integrationFlowContext;
@After
@AfterEach
public void tearDown() {
this.mockIntegrationContext.resetBeans("mySourceEndpoint", "inboundChannelAdapter");
this.results.purge(null);