|
| 1 | +package org.springframework.test.context.junit4.spr16716; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | +import org.junit.runner.RunWith; |
| 5 | +import org.junit.runners.model.Statement; |
| 6 | +import org.mockito.Mock; |
| 7 | +import org.mockito.junit.MockitoJUnitRunner; |
| 8 | +import org.mockito.stubbing.Answer; |
| 9 | +import org.springframework.test.context.junit4.statements.SpringFailOnTimeout; |
| 10 | + |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | +import java.util.concurrent.TimeoutException; |
| 13 | + |
| 14 | +import static org.mockito.Mockito.doAnswer; |
| 15 | +import static org.mockito.Mockito.doThrow; |
| 16 | + |
| 17 | +/** |
| 18 | + * Validate SpringFailOnTimeout contract |
| 19 | + * <a href="https://jira.spring.io/browse/SPR-16716" target="_blank">SPR-16716</a>. |
| 20 | + * |
| 21 | + * @author Igor Suhorukov |
| 22 | + * @since 5.0.6 |
| 23 | + */ |
| 24 | +@RunWith(MockitoJUnitRunner.class) |
| 25 | +public class SpringFailOnTimeoutExceptionTest { |
| 26 | + |
| 27 | + @Mock |
| 28 | + private Statement statement; |
| 29 | + |
| 30 | + @Test(expected = IllegalArgumentException.class) |
| 31 | + public void validateOriginalExceptionFromEvaluateMethod() throws Throwable { |
| 32 | + IllegalArgumentException expectedException = new IllegalArgumentException(); |
| 33 | + doThrow(expectedException).when(statement).evaluate(); |
| 34 | + new SpringFailOnTimeout(statement, TimeUnit.SECONDS.toMillis(1)).evaluate(); |
| 35 | + } |
| 36 | + |
| 37 | + @Test(expected = TimeoutException.class) |
| 38 | + public void validateTimeoutException() throws Throwable { |
| 39 | + doAnswer((Answer<Void>) invocation -> { |
| 40 | + TimeUnit.MILLISECONDS.sleep(50); |
| 41 | + return null; |
| 42 | + }).when(statement).evaluate(); |
| 43 | + new SpringFailOnTimeout(statement, TimeUnit.MILLISECONDS.toMillis(1)).evaluate(); |
| 44 | + } |
| 45 | +} |
0 commit comments