|
16 | 16 |
|
17 | 17 | package org.springframework.boot;
|
18 | 18 |
|
| 19 | +import java.lang.Thread.UncaughtExceptionHandler; |
19 | 20 | import java.lang.reflect.InvocationTargetException;
|
20 | 21 |
|
21 |
| -import org.junit.Rule; |
22 | 22 | import org.junit.Test;
|
23 |
| -import org.mockito.InjectMocks; |
24 |
| -import org.mockito.Mock; |
25 |
| -import org.mockito.junit.MockitoJUnit; |
26 |
| -import org.mockito.junit.MockitoRule; |
27 | 23 |
|
28 |
| - |
29 |
| -import static org.mockito.ArgumentMatchers.same; |
| 24 | +import static org.mockito.Matchers.same; |
| 25 | +import static org.mockito.Mockito.mock; |
30 | 26 | import static org.mockito.Mockito.verify;
|
31 | 27 | import static org.mockito.Mockito.verifyZeroInteractions;
|
32 | 28 |
|
33 | 29 | /**
|
34 | 30 | * Tests for {@link SpringBootExceptionHandler}.
|
35 | 31 | *
|
36 | 32 | * @author Henri Tremblay
|
| 33 | + * @author Andy Wilkinson |
37 | 34 | */
|
38 |
| -public class SpringBootExceptionHandlerTest { |
39 |
| - |
40 |
| - @Rule |
41 |
| - public MockitoRule rule = MockitoJUnit.rule(); |
| 35 | +public class SpringBootExceptionHandlerTests { |
42 | 36 |
|
43 |
| - @Mock |
44 |
| - private Thread.UncaughtExceptionHandler parent; |
| 37 | + private final UncaughtExceptionHandler parent = mock(UncaughtExceptionHandler.class); |
45 | 38 |
|
46 |
| - @InjectMocks |
47 |
| - private SpringBootExceptionHandler handler; |
| 39 | + private final SpringBootExceptionHandler handler = new SpringBootExceptionHandler( |
| 40 | + this.parent); |
48 | 41 |
|
49 | 42 | @Test
|
50 |
| - public void uncaughtException_shouldNotForwardLoggedErrorToParent() { |
| 43 | + public void uncaughtExceptionDoesNotForwardLoggedErrorToParent() { |
51 | 44 | Thread thread = Thread.currentThread();
|
52 | 45 | Exception ex = new Exception();
|
53 | 46 | this.handler.registerLoggedException(ex);
|
54 |
| - |
55 | 47 | this.handler.uncaughtException(thread, ex);
|
56 |
| - |
57 | 48 | verifyZeroInteractions(this.parent);
|
58 | 49 | }
|
59 | 50 |
|
60 | 51 | @Test
|
61 |
| - public void uncaughtException_shouldForwardLogConfigurationErrorToParent() { |
| 52 | + public void uncaughtExceptionForwardsLogConfigurationErrorToParent() { |
62 | 53 | Thread thread = Thread.currentThread();
|
63 |
| - Exception ex = new Exception("[stuff] Logback configuration error detected [stuff]"); |
| 54 | + Exception ex = new Exception( |
| 55 | + "[stuff] Logback configuration error detected [stuff]"); |
64 | 56 | this.handler.registerLoggedException(ex);
|
65 |
| - |
66 | 57 | this.handler.uncaughtException(thread, ex);
|
67 |
| - |
68 | 58 | verify(this.parent).uncaughtException(same(thread), same(ex));
|
69 | 59 | }
|
70 | 60 |
|
71 | 61 | @Test
|
72 |
| - public void uncaughtException_shouldForwardLogConfigurationErrorToParentEvenWhenWrapped() { |
| 62 | + public void uncaughtExceptionForwardsWrappedLogConfigurationErrorToParent() { |
73 | 63 | Thread thread = Thread.currentThread();
|
74 |
| - Exception ex = new InvocationTargetException(new Exception("[stuff] Logback configuration error detected [stuff]", new Exception())); |
| 64 | + Exception ex = new InvocationTargetException(new Exception( |
| 65 | + "[stuff] Logback configuration error detected [stuff]", new Exception())); |
75 | 66 | this.handler.registerLoggedException(ex);
|
76 |
| - |
77 | 67 | this.handler.uncaughtException(thread, ex);
|
78 |
| - |
79 | 68 | verify(this.parent).uncaughtException(same(thread), same(ex));
|
80 | 69 | }
|
| 70 | + |
81 | 71 | }
|
0 commit comments