|
| 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.junit4.spr16716; |
| 18 | + |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | +import java.util.concurrent.TimeoutException; |
| 21 | + |
| 22 | +import org.junit.Rule; |
| 23 | +import org.junit.Test; |
| 24 | +import org.junit.rules.ExpectedException; |
| 25 | +import org.junit.runners.model.Statement; |
| 26 | +import org.mockito.stubbing.Answer; |
| 27 | + |
| 28 | +import org.springframework.test.context.junit4.statements.SpringFailOnTimeout; |
| 29 | + |
| 30 | +import static org.mockito.Mockito.*; |
| 31 | + |
| 32 | +/** |
| 33 | + * Unit tests for {@link SpringFailOnTimeout}. |
| 34 | + * |
| 35 | + * @author Igor Suhorukov |
| 36 | + * @author Sam Brannen |
| 37 | + * @since 4.3.17 |
| 38 | + */ |
| 39 | +public class SpringFailOnTimeoutTests { |
| 40 | + |
| 41 | + private Statement statement = mock(Statement.class); |
| 42 | + |
| 43 | + @Rule |
| 44 | + public final ExpectedException exception = ExpectedException.none(); |
| 45 | + |
| 46 | + |
| 47 | + @Test |
| 48 | + public void nullNextStatement() throws Throwable { |
| 49 | + exception.expect(IllegalArgumentException.class); |
| 50 | + new SpringFailOnTimeout(null, 1); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void negativeTimeout() throws Throwable { |
| 55 | + exception.expect(IllegalArgumentException.class); |
| 56 | + new SpringFailOnTimeout(statement, -1); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void userExceptionPropagates() throws Throwable { |
| 61 | + doThrow(new Boom()).when(statement).evaluate(); |
| 62 | + |
| 63 | + exception.expect(Boom.class); |
| 64 | + new SpringFailOnTimeout(statement, 1).evaluate(); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void timeoutExceptionThrownIfNoUserException() throws Throwable { |
| 69 | + doAnswer((Answer<Void>) invocation -> { |
| 70 | + TimeUnit.MILLISECONDS.sleep(50); |
| 71 | + return null; |
| 72 | + }).when(statement).evaluate(); |
| 73 | + |
| 74 | + exception.expect(TimeoutException.class); |
| 75 | + new SpringFailOnTimeout(statement, 1).evaluate(); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void noExceptionThrownIfNoUserExceptionAndTimeoutDoesNotOccur() throws Throwable { |
| 80 | + doAnswer((Answer<Void>) invocation -> { |
| 81 | + return null; |
| 82 | + }).when(statement).evaluate(); |
| 83 | + |
| 84 | + new SpringFailOnTimeout(statement, 100).evaluate(); |
| 85 | + } |
| 86 | + |
| 87 | + private static class Boom extends RuntimeException { |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments