|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 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 | + * https://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.transaction; |
| 18 | + |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | +import java.util.concurrent.TimeoutException; |
| 21 | +import javax.sql.DataSource; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.api.Timeout; |
| 25 | +import org.junit.platform.testkit.engine.EngineTestKit; |
| 26 | +import org.junit.platform.testkit.engine.Events; |
| 27 | + |
| 28 | +import org.springframework.context.annotation.Bean; |
| 29 | +import org.springframework.context.annotation.Configuration; |
| 30 | +import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
| 31 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 32 | +import org.springframework.test.context.junit.jupiter.FailingTestCase; |
| 33 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 34 | +import org.springframework.transaction.PlatformTransactionManager; |
| 35 | +import org.springframework.transaction.annotation.Propagation; |
| 36 | +import org.springframework.transaction.annotation.Transactional; |
| 37 | + |
| 38 | +import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; |
| 39 | +import static org.junit.platform.testkit.engine.EventConditions.event; |
| 40 | +import static org.junit.platform.testkit.engine.EventConditions.finishedWithFailure; |
| 41 | +import static org.junit.platform.testkit.engine.EventConditions.test; |
| 42 | +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf; |
| 43 | +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message; |
| 44 | +import static org.springframework.test.transaction.TransactionAssert.assertThatTransaction; |
| 45 | + |
| 46 | +/** |
| 47 | + * JUnit Jupiter based integration tests which verify support for Spring's |
| 48 | + * {@link Transactional @Transactional} annotation in conjunction with JUnit |
| 49 | + * Jupiter's {@link Timeout @Timeout}. |
| 50 | + * |
| 51 | + * @author Sam Brannen |
| 52 | + * @since 5.2 |
| 53 | + * @see org.springframework.test.context.junit4.TimedTransactionalSpringRunnerTests |
| 54 | + */ |
| 55 | +class TimedTransactionalSpringExtensionTests { |
| 56 | + |
| 57 | + @Test |
| 58 | + void springTransactionsWorkWithJUnitJupiterTimeouts() { |
| 59 | + Events events = EngineTestKit.engine("junit-jupiter") |
| 60 | + .selectors(selectClass(TestCase.class)) |
| 61 | + .execute() |
| 62 | + .tests() |
| 63 | + .assertStatistics(stats -> stats.started(4).succeeded(2).failed(2)); |
| 64 | + |
| 65 | + events.failed().assertThatEvents().haveAtMost(2, |
| 66 | + event(test("WithExceededJUnitJupiterTimeout"), |
| 67 | + finishedWithFailure( |
| 68 | + instanceOf(TimeoutException.class), |
| 69 | + message(msg -> msg.endsWith("timed out after 50 milliseconds"))))); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + @SpringJUnitConfig |
| 74 | + @Transactional |
| 75 | + @FailingTestCase |
| 76 | + static class TestCase { |
| 77 | + |
| 78 | + @Test |
| 79 | + @Timeout(1) |
| 80 | + void transactionalWithJUnitJupiterTimeout() { |
| 81 | + assertThatTransaction().isActive(); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + @Timeout(value = 50, unit = TimeUnit.MILLISECONDS) |
| 86 | + void transactionalWithExceededJUnitJupiterTimeout() throws Exception { |
| 87 | + assertThatTransaction().isActive(); |
| 88 | + Thread.sleep(100); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + @Timeout(1) |
| 93 | + @Transactional(propagation = Propagation.NOT_SUPPORTED) |
| 94 | + void notTransactionalWithJUnitJupiterTimeout() { |
| 95 | + assertThatTransaction().isNotActive(); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + @Timeout(value = 50, unit = TimeUnit.MILLISECONDS) |
| 100 | + @Transactional(propagation = Propagation.NOT_SUPPORTED) |
| 101 | + void notTransactionalWithExceededJUnitJupiterTimeout() throws Exception { |
| 102 | + assertThatTransaction().isNotActive(); |
| 103 | + Thread.sleep(100); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + @Configuration |
| 108 | + static class Config { |
| 109 | + |
| 110 | + @Bean |
| 111 | + PlatformTransactionManager transactionManager(DataSource dataSource) { |
| 112 | + return new DataSourceTransactionManager(dataSource); |
| 113 | + } |
| 114 | + |
| 115 | + @Bean |
| 116 | + DataSource dataSource() { |
| 117 | + return new EmbeddedDatabaseBuilder().generateUniqueName(true).build(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments