From 230c8f93e84a8a7ce242047a9430d63cf6fd92ba Mon Sep 17 00:00:00 2001 From: Igor Suhorukov Date: Thu, 12 Apr 2018 11:37:13 +0300 Subject: [PATCH] Throw exception from user code in SpringFailOnTimeout even if a timeout occurs Issue: SPR-16717 --- .../statements/SpringFailOnTimeout.java | 14 +++--- .../SpringFailOnTimeoutExceptionTest.java | 45 +++++++++++++++++++ 2 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 spring-test/src/test/java/org/springframework/test/context/junit4/spr16716/SpringFailOnTimeoutExceptionTest.java diff --git a/spring-test/src/main/java/org/springframework/test/context/junit4/statements/SpringFailOnTimeout.java b/spring-test/src/main/java/org/springframework/test/context/junit4/statements/SpringFailOnTimeout.java index 130b9a65a1..e9870703e6 100644 --- a/spring-test/src/main/java/org/springframework/test/context/junit4/statements/SpringFailOnTimeout.java +++ b/spring-test/src/main/java/org/springframework/test/context/junit4/statements/SpringFailOnTimeout.java @@ -88,15 +88,11 @@ public class SpringFailOnTimeout extends Statement { } else { long startTime = System.currentTimeMillis(); - try { - this.next.evaluate(); - } - finally { - long elapsed = System.currentTimeMillis() - startTime; - if (elapsed > this.timeout) { - throw new TimeoutException( - String.format("Test took %s ms; limit was %s ms.", elapsed, this.timeout)); - } + this.next.evaluate(); + long elapsed = System.currentTimeMillis() - startTime; + if (elapsed > this.timeout) { + throw new TimeoutException( + String.format("Test took %s ms; limit was %s ms.", elapsed, this.timeout)); } } } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/spr16716/SpringFailOnTimeoutExceptionTest.java b/spring-test/src/test/java/org/springframework/test/context/junit4/spr16716/SpringFailOnTimeoutExceptionTest.java new file mode 100644 index 0000000000..1ff0447276 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/spr16716/SpringFailOnTimeoutExceptionTest.java @@ -0,0 +1,45 @@ +package org.springframework.test.context.junit4.spr16716; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.model.Statement; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.stubbing.Answer; +import org.springframework.test.context.junit4.statements.SpringFailOnTimeout; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doThrow; + +/** + * Validate SpringFailOnTimeout contract + * SPR-16716. + * + * @author Igor Suhorukov + * @since 5.0.6 + */ +@RunWith(MockitoJUnitRunner.class) +public class SpringFailOnTimeoutExceptionTest { + + @Mock + private Statement statement; + + @Test(expected = IllegalArgumentException.class) + public void validateOriginalExceptionFromEvaluateMethod() throws Throwable { + IllegalArgumentException expectedException = new IllegalArgumentException(); + doThrow(expectedException).when(statement).evaluate(); + new SpringFailOnTimeout(statement, TimeUnit.SECONDS.toMillis(1)).evaluate(); + } + + @Test(expected = TimeoutException.class) + public void validateTimeoutException() throws Throwable { + doAnswer((Answer) invocation -> { + TimeUnit.MILLISECONDS.sleep(50); + return null; + }).when(statement).evaluate(); + new SpringFailOnTimeout(statement, TimeUnit.MILLISECONDS.toMillis(1)).evaluate(); + } +}