Browse Source

Throw exception from user code in SpringFailOnTimeout even if a timeout occurs

Issue: SPR-16717
pull/1798/head
Igor Suhorukov 7 years ago committed by Sam Brannen
parent
commit
230c8f93e8
  1. 14
      spring-test/src/main/java/org/springframework/test/context/junit4/statements/SpringFailOnTimeout.java
  2. 45
      spring-test/src/test/java/org/springframework/test/context/junit4/spr16716/SpringFailOnTimeoutExceptionTest.java

14
spring-test/src/main/java/org/springframework/test/context/junit4/statements/SpringFailOnTimeout.java

@ -88,15 +88,11 @@ public class SpringFailOnTimeout extends Statement { @@ -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));
}
}
}

45
spring-test/src/test/java/org/springframework/test/context/junit4/spr16716/SpringFailOnTimeoutExceptionTest.java

@ -0,0 +1,45 @@ @@ -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
* <a href="https://jira.spring.io/browse/SPR-16716" target="_blank">SPR-16716</a>.
*
* @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<Void>) invocation -> {
TimeUnit.MILLISECONDS.sleep(50);
return null;
}).when(statement).evaluate();
new SpringFailOnTimeout(statement, TimeUnit.MILLISECONDS.toMillis(1)).evaluate();
}
}
Loading…
Cancel
Save