|
32 | 32 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
33 | 33 | import static org.mockito.BDDMockito.given;
|
34 | 34 | import static org.mockito.Mockito.mock;
|
| 35 | +import static org.mockito.Mockito.times; |
| 36 | +import static org.mockito.Mockito.verify; |
35 | 37 |
|
36 | 38 | /**
|
37 | 39 | * Unit tests for {@link ControllerAdviceBean}.
|
@@ -89,22 +91,39 @@ public String toString() {
|
89 | 91 | assertEqualsHashCodeAndToString(bean1, bean2, toString);
|
90 | 92 | }
|
91 | 93 |
|
| 94 | + @Test |
| 95 | + public void orderedWithLowestPrecedenceByDefaultForBeanName() { |
| 96 | + assertOrder(SimpleControllerAdvice.class, Ordered.LOWEST_PRECEDENCE); |
| 97 | + } |
| 98 | + |
92 | 99 | @Test
|
93 | 100 | public void orderedWithLowestPrecedenceByDefaultForBeanInstance() {
|
94 | 101 | assertOrder(new SimpleControllerAdvice(), Ordered.LOWEST_PRECEDENCE);
|
95 | 102 | }
|
96 | 103 |
|
97 | 104 | @Test
|
98 |
| - public void orderedViaAnnotationForBeanInstance() { |
99 |
| - assertOrder(new OrderAnnotationControllerAdvice(), 42); |
100 |
| - assertOrder(new PriorityAnnotationControllerAdvice(), 42); |
| 105 | + public void orderedViaOrderedInterfaceForBeanName() { |
| 106 | + // TODO Change expectedOrder once Ordered is properly supported |
| 107 | + assertOrder(OrderedControllerAdvice.class, Ordered.LOWEST_PRECEDENCE); |
101 | 108 | }
|
102 | 109 |
|
103 | 110 | @Test
|
104 | 111 | public void orderedViaOrderedInterfaceForBeanInstance() {
|
105 | 112 | assertOrder(new OrderedControllerAdvice(), 42);
|
106 | 113 | }
|
107 | 114 |
|
| 115 | + @Test |
| 116 | + public void orderedViaAnnotationForBeanName() { |
| 117 | + assertOrder(OrderAnnotationControllerAdvice.class, 42); |
| 118 | + assertOrder(PriorityAnnotationControllerAdvice.class, 42); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void orderedViaAnnotationForBeanInstance() { |
| 123 | + assertOrder(new OrderAnnotationControllerAdvice(), 42); |
| 124 | + assertOrder(new PriorityAnnotationControllerAdvice(), 42); |
| 125 | + } |
| 126 | + |
108 | 127 | @Test
|
109 | 128 | public void shouldMatchAll() {
|
110 | 129 | ControllerAdviceBean bean = new ControllerAdviceBean(new SimpleControllerAdvice());
|
@@ -186,6 +205,23 @@ private void assertOrder(Object bean, int expectedOrder) {
|
186 | 205 | assertThat(new ControllerAdviceBean(bean).getOrder()).isEqualTo(expectedOrder);
|
187 | 206 | }
|
188 | 207 |
|
| 208 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 209 | + private void assertOrder(Class beanType, int expectedOrder) { |
| 210 | + String beanName = "myBean"; |
| 211 | + BeanFactory beanFactory = mock(BeanFactory.class); |
| 212 | + given(beanFactory.containsBean(beanName)).willReturn(true); |
| 213 | + given(beanFactory.getType(beanName)).willReturn(beanType); |
| 214 | + |
| 215 | + ControllerAdviceBean controllerAdviceBean = new ControllerAdviceBean(beanName, beanFactory); |
| 216 | + |
| 217 | + assertThat(controllerAdviceBean.getOrder()).isEqualTo(expectedOrder); |
| 218 | + verify(beanFactory).containsBean(beanName); |
| 219 | + verify(beanFactory).getType(beanName); |
| 220 | + // Expecting 0 invocations of getBean() since Ordered is not yet supported. |
| 221 | + // TODO Change expected number of invocations once Ordered is properly supported |
| 222 | + verify(beanFactory, times(0)).getBean(beanName); |
| 223 | + } |
| 224 | + |
189 | 225 | private void assertApplicable(String message, ControllerAdviceBean controllerAdvice, Class<?> controllerBeanType) {
|
190 | 226 | assertThat(controllerAdvice).isNotNull();
|
191 | 227 | assertThat(controllerAdvice.isApplicableToBeanType(controllerBeanType)).as(message).isTrue();
|
|
0 commit comments