|
| 1 | +/* |
| 2 | + * Copyright 2012-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.boot; |
| 18 | + |
| 19 | +import java.lang.Thread.UncaughtExceptionHandler; |
| 20 | +import java.lang.reflect.InvocationTargetException; |
| 21 | + |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import static org.mockito.Matchers.same; |
| 25 | +import static org.mockito.Mockito.mock; |
| 26 | +import static org.mockito.Mockito.verify; |
| 27 | +import static org.mockito.Mockito.verifyZeroInteractions; |
| 28 | + |
| 29 | +/** |
| 30 | + * Tests for {@link SpringBootExceptionHandler}. |
| 31 | + * |
| 32 | + * @author Henri Tremblay |
| 33 | + * @author Andy Wilkinson |
| 34 | + */ |
| 35 | +public class SpringBootExceptionHandlerTests { |
| 36 | + |
| 37 | + private final UncaughtExceptionHandler parent = mock(UncaughtExceptionHandler.class); |
| 38 | + |
| 39 | + private final SpringBootExceptionHandler handler = new SpringBootExceptionHandler( |
| 40 | + this.parent); |
| 41 | + |
| 42 | + @Test |
| 43 | + public void uncaughtExceptionDoesNotForwardLoggedErrorToParent() { |
| 44 | + Thread thread = Thread.currentThread(); |
| 45 | + Exception ex = new Exception(); |
| 46 | + this.handler.registerLoggedException(ex); |
| 47 | + this.handler.uncaughtException(thread, ex); |
| 48 | + verifyZeroInteractions(this.parent); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void uncaughtExceptionForwardsLogConfigurationErrorToParent() { |
| 53 | + Thread thread = Thread.currentThread(); |
| 54 | + Exception ex = new Exception( |
| 55 | + "[stuff] Logback configuration error detected [stuff]"); |
| 56 | + this.handler.registerLoggedException(ex); |
| 57 | + this.handler.uncaughtException(thread, ex); |
| 58 | + verify(this.parent).uncaughtException(same(thread), same(ex)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void uncaughtExceptionForwardsWrappedLogConfigurationErrorToParent() { |
| 63 | + Thread thread = Thread.currentThread(); |
| 64 | + Exception ex = new InvocationTargetException(new Exception( |
| 65 | + "[stuff] Logback configuration error detected [stuff]", new Exception())); |
| 66 | + this.handler.registerLoggedException(ex); |
| 67 | + this.handler.uncaughtException(thread, ex); |
| 68 | + verify(this.parent).uncaughtException(same(thread), same(ex)); |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments