Browse Source
This commit modifies the way the `@RecordApplicationEvents` annotation works in tests, allowing for capture of events from threads other than the main test thread (async events) and for the assertion of captured event from a separate thread (e.g. when using `Awaitility`). This is done by switching the `ApplicationEventsHolder` to use an `InheritedThreadLocal`. There is a mutual exclusion between support of asynchronous events vs support of JUnit5 parallel tests with the `@TestInstance(PER_CLASS)` mode. As a result, we favor the former and now `SpringExtension` will invalidate a test class that is annotated (or meta-annotated, or enclosed-annotated) with `@RecordApplicationEvents` AND `@TestInstance(PER_CLASS)` AND `@Execution(CONCURRENT)`. See gh-29827 Closes gh-30020pull/30456/head
8 changed files with 339 additions and 14 deletions
@ -0,0 +1,84 @@
@@ -0,0 +1,84 @@
|
||||
/* |
||||
* Copyright 2002-2023 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test.context.junit4.event; |
||||
|
||||
import org.assertj.core.api.InstanceOfAssertFactories; |
||||
import org.awaitility.Awaitility; |
||||
import org.awaitility.Durations; |
||||
import org.junit.Rule; |
||||
import org.junit.Test; |
||||
import org.junit.rules.TestName; |
||||
import org.junit.runner.RunWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.test.context.event.ApplicationEvents; |
||||
import org.springframework.test.context.event.RecordApplicationEvents; |
||||
import org.springframework.test.context.junit4.SpringRunner; |
||||
import org.springframework.test.context.junit4.event.JUnit4ApplicationEventsIntegrationTests.CustomEvent; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Integration tests for {@link ApplicationEvents} that record async events |
||||
* or assert the events from a separate thread, in conjunction with JUnit 4. |
||||
* |
||||
* @author Simon Baslé |
||||
* @since 6.1.0 |
||||
*/ |
||||
@RunWith(SpringRunner.class) |
||||
@RecordApplicationEvents |
||||
public class JUnit4ApplicationEventsAsyncIntegrationTests { |
||||
|
||||
@Rule |
||||
public final TestName testName = new TestName(); |
||||
|
||||
@Autowired |
||||
ApplicationContext context; |
||||
|
||||
@Autowired |
||||
ApplicationEvents applicationEvents; |
||||
|
||||
@Test |
||||
public void asyncPublication() throws InterruptedException { |
||||
Thread t = new Thread(() -> context.publishEvent(new CustomEvent("async"))); |
||||
t.start(); |
||||
t.join(); |
||||
|
||||
assertThat(this.applicationEvents.stream(CustomEvent.class)) |
||||
.singleElement() |
||||
.extracting(CustomEvent::getMessage, InstanceOfAssertFactories.STRING) |
||||
.isEqualTo("async"); |
||||
} |
||||
|
||||
@Test |
||||
public void asyncConsumption() { |
||||
context.publishEvent(new CustomEvent("sync")); |
||||
|
||||
Awaitility.await().atMost(Durations.ONE_SECOND) |
||||
.untilAsserted(() -> assertThat(assertThat(this.applicationEvents.stream(CustomEvent.class)) |
||||
.singleElement() |
||||
.extracting(CustomEvent::getMessage, InstanceOfAssertFactories.STRING) |
||||
.isEqualTo("sync"))); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config { |
||||
} |
||||
} |
||||
@ -0,0 +1,77 @@
@@ -0,0 +1,77 @@
|
||||
/* |
||||
* Copyright 2002-2023 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test.context.testng.event; |
||||
|
||||
import org.assertj.core.api.InstanceOfAssertFactories; |
||||
import org.awaitility.Awaitility; |
||||
import org.awaitility.Durations; |
||||
import org.testng.annotations.Test; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.test.context.event.ApplicationEvents; |
||||
import org.springframework.test.context.event.RecordApplicationEvents; |
||||
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; |
||||
import org.springframework.test.context.testng.event.TestNGApplicationEventsIntegrationTests.CustomEvent; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Integration tests for {@link ApplicationEvents} that record async events |
||||
* or assert the events from a separate thread, in conjunction with TestNG. |
||||
* |
||||
* @author Simon Baslé |
||||
* @since 6.1.0 |
||||
*/ |
||||
@RecordApplicationEvents |
||||
class TestNGApplicationEventsAsyncIntegrationTests extends AbstractTestNGSpringContextTests { |
||||
|
||||
@Autowired |
||||
ApplicationContext context; |
||||
|
||||
@Autowired |
||||
ApplicationEvents applicationEvents; |
||||
|
||||
|
||||
@Test |
||||
public void asyncPublication() throws InterruptedException { |
||||
Thread t = new Thread(() -> context.publishEvent(new CustomEvent("asyncPublication"))); |
||||
t.start(); |
||||
t.join(); |
||||
|
||||
assertThat(this.applicationEvents.stream(CustomEvent.class)) |
||||
.singleElement() |
||||
.extracting(CustomEvent::getMessage, InstanceOfAssertFactories.STRING) |
||||
.isEqualTo("asyncPublication"); |
||||
} |
||||
|
||||
@Test |
||||
public void asyncConsumption() { |
||||
context.publishEvent(new CustomEvent("asyncConsumption")); |
||||
|
||||
Awaitility.await().atMost(Durations.ONE_SECOND) |
||||
.untilAsserted(() -> assertThat(assertThat(this.applicationEvents.stream(CustomEvent.class)) |
||||
.singleElement() |
||||
.extracting(CustomEvent::getMessage, InstanceOfAssertFactories.STRING) |
||||
.isEqualTo("asyncConsumption"))); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config { } |
||||
} |
||||
Loading…
Reference in new issue