|
| 1 | +/* |
| 2 | + * Copyright 2002-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.context.junit.jupiter.parallel; |
| 18 | + |
| 19 | +import java.lang.reflect.Parameter; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.AfterEach; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.RepeatedTest; |
| 24 | +import org.junit.platform.launcher.Launcher; |
| 25 | +import org.junit.platform.launcher.LauncherDiscoveryRequest; |
| 26 | +import org.junit.platform.launcher.core.LauncherFactory; |
| 27 | +import org.junit.platform.launcher.listeners.SummaryGeneratingListener; |
| 28 | + |
| 29 | +import org.springframework.beans.factory.annotation.Autowired; |
| 30 | +import org.springframework.context.ApplicationContext; |
| 31 | +import org.springframework.context.annotation.Configuration; |
| 32 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 33 | + |
| 34 | +import static org.junit.jupiter.api.Assertions.*; |
| 35 | +import static org.junit.jupiter.engine.Constants.*; |
| 36 | +import static org.junit.platform.engine.discovery.DiscoverySelectors.*; |
| 37 | +import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.*; |
| 38 | + |
| 39 | +/** |
| 40 | + * Integration tests which verify that {@code @BeforeEach} and {@code @AfterEach} methods |
| 41 | + * that accept {@code @Autowired} arguments can be executed in parallel without issues |
| 42 | + * regarding concurrent access to the {@linkplain Parameter parameters} of such methods. |
| 43 | + * |
| 44 | + * @author Sam Brannen |
| 45 | + * @since 5.1.3 |
| 46 | + */ |
| 47 | +class ParallelExecutionSpringExtensionTests { |
| 48 | + |
| 49 | + private static final int NUM_TESTS = 1000; |
| 50 | + |
| 51 | + @RepeatedTest(10) |
| 52 | + void runTestsInParallel() { |
| 53 | + Launcher launcher = LauncherFactory.create(); |
| 54 | + SummaryGeneratingListener listener = new SummaryGeneratingListener(); |
| 55 | + launcher.registerTestExecutionListeners(listener); |
| 56 | + |
| 57 | + LauncherDiscoveryRequest request = request()// |
| 58 | + .configurationParameter(PARALLEL_EXECUTION_ENABLED_PROPERTY_NAME, "true")// |
| 59 | + .configurationParameter(PARALLEL_CONFIG_DYNAMIC_FACTOR_PROPERTY_NAME, "10")// |
| 60 | + .selectors(selectClass(TestCase.class))// |
| 61 | + .build(); |
| 62 | + |
| 63 | + launcher.execute(request); |
| 64 | + |
| 65 | + assertEquals(NUM_TESTS, listener.getSummary().getTestsSucceededCount(), |
| 66 | + "number of tests executed successfully"); |
| 67 | + } |
| 68 | + |
| 69 | + @SpringJUnitConfig |
| 70 | + static class TestCase { |
| 71 | + |
| 72 | + @BeforeEach |
| 73 | + void beforeEach(@Autowired ApplicationContext context) { |
| 74 | + } |
| 75 | + |
| 76 | + @RepeatedTest(NUM_TESTS) |
| 77 | + void repeatedTest(@Autowired ApplicationContext context) { |
| 78 | + } |
| 79 | + |
| 80 | + @AfterEach |
| 81 | + void afterEach(@Autowired ApplicationContext context) { |
| 82 | + } |
| 83 | + |
| 84 | + @Configuration |
| 85 | + static class Config { |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments