|
1 | 1 | /* |
2 | | - * Copyright 2018-present the original author or authors. |
| 2 | + * Copyright 2026-present the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
19 | 19 |
|
20 | 20 | import org.junit.jupiter.api.Test; |
21 | 21 | import org.junit.jupiter.api.extension.ExtendWith; |
22 | | -import org.mockito.Mock; |
23 | 22 | import org.mockito.junit.jupiter.MockitoExtension; |
24 | 23 |
|
25 | 24 | import org.springframework.beans.factory.ListableBeanFactory; |
26 | 25 | import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 26 | +import org.springframework.context.ApplicationContext; |
27 | 27 | import org.springframework.context.ConfigurableApplicationContext; |
28 | 28 | import org.springframework.context.event.ContextRefreshedEvent; |
29 | 29 | import org.springframework.context.support.GenericApplicationContext; |
|
32 | 32 | /** |
33 | 33 | * Unit tests for {@link DeferredRepositoryInitializationListener}. |
34 | 34 | * |
35 | | - * @author Oliver Drotbohm |
36 | | - * @author seongjun-rpls |
| 35 | + * @author Seongjun Ha |
| 36 | + * @author Mark Paluch |
37 | 37 | */ |
38 | 38 | @ExtendWith(MockitoExtension.class) |
39 | 39 | class DeferredRepositoryInitializationListenerUnitTests { |
40 | 40 |
|
41 | | - @Mock ListableBeanFactory beanFactory; |
42 | | - |
43 | 41 | @Test // GH-3459 |
44 | | - void triggersInitializationOnMatchingContextEvent() { |
45 | | - |
46 | | - var context = new GenericApplicationContext(); |
47 | | - context.refresh(); |
| 42 | + void triggersInitializationCorrectly() { |
48 | 43 |
|
49 | | - // Create listener with the real context's BeanFactory |
50 | | - var listener = new DeferredRepositoryInitializationListener(context.getBeanFactory()); |
| 44 | + var beanFactory = mock(DefaultListableBeanFactory.class); |
| 45 | + var context = mock(ApplicationContext.class); |
| 46 | + var listener = new DeferredRepositoryInitializationListener(beanFactory); |
51 | 47 |
|
52 | | - // Fire event from the same context |
53 | 48 | listener.onApplicationEvent(new ContextRefreshedEvent(context)); |
54 | 49 |
|
55 | | - // The test verifies no exception is thrown and listener completes normally |
56 | | - // Since we're using real context, we can't easily mock getBeansOfType |
57 | | - context.close(); |
| 50 | + verify(beanFactory).getBeansOfType(Repository.class); |
58 | 51 | } |
59 | 52 |
|
60 | 53 | @Test // GH-3459 |
61 | | - void ignoresEventsFromChildContext() { |
| 54 | + void triggersInitializationForConfigurableApplicationContext() { |
62 | 55 |
|
63 | | - // Mock beanFactory that should NOT be called |
64 | | - ListableBeanFactory mockBeanFactory = mock(ListableBeanFactory.class); |
65 | | - var listener = new DeferredRepositoryInitializationListener(mockBeanFactory); |
| 56 | + var beanFactory = mock(DefaultListableBeanFactory.class); |
| 57 | + var context = mock(ConfigurableApplicationContext.class); |
| 58 | + var listener = new DeferredRepositoryInitializationListener(beanFactory); |
| 59 | + when(context.getBeanFactory()).thenReturn(beanFactory); |
66 | 60 |
|
67 | | - // Create a real context (will have different beanFactory) |
68 | | - var context = new GenericApplicationContext(); |
69 | | - context.refresh(); |
70 | | - |
71 | | - // Fire event - should be ignored because context.getBeanFactory() != mockBeanFactory |
72 | 61 | listener.onApplicationEvent(new ContextRefreshedEvent(context)); |
73 | 62 |
|
74 | | - // Verify the mock was never called |
75 | | - verify(mockBeanFactory, never()).getBeansOfType(Repository.class); |
76 | | - |
77 | | - context.close(); |
| 63 | + verify(beanFactory).getBeansOfType(Repository.class); |
78 | 64 | } |
79 | 65 |
|
80 | 66 | @Test // GH-3459 |
81 | | - void triggersInitializationOnParentContextEvent() { |
82 | | - |
83 | | - // Create a mock beanFactory |
84 | | - DefaultListableBeanFactory mockBeanFactory = mock(DefaultListableBeanFactory.class); |
85 | | - |
86 | | - // Create a mock ConfigurableApplicationContext |
87 | | - ConfigurableApplicationContext mockContext = mock(ConfigurableApplicationContext.class); |
88 | | - when(mockContext.getBeanFactory()).thenReturn(mockBeanFactory); |
| 67 | + void ignoresEventsFromChildContext() { |
89 | 68 |
|
90 | | - // Create listener with the same mock beanFactory |
91 | | - var listener = new DeferredRepositoryInitializationListener(mockBeanFactory); |
| 69 | + var beanFactory = mock(ListableBeanFactory.class); |
| 70 | + var listener = new DeferredRepositoryInitializationListener(beanFactory); |
92 | 71 |
|
93 | | - // Fire event from the mock context |
94 | | - listener.onApplicationEvent(new ContextRefreshedEvent(mockContext)); |
| 72 | + listener.onApplicationEvent(new ContextRefreshedEvent(new GenericApplicationContext())); |
95 | 73 |
|
96 | | - // Verify getBeansOfType WAS called |
97 | | - verify(mockBeanFactory).getBeansOfType(Repository.class); |
| 74 | + verify(beanFactory, never()).getBeansOfType(Repository.class); |
98 | 75 | } |
99 | 76 |
|
100 | | - @Test // GH-3459 |
101 | | - void ignoresEventsFromSiblingChildContexts() { |
102 | | - |
103 | | - // Create a mock beanFactory for the "parent" listener |
104 | | - DefaultListableBeanFactory mockParentBeanFactory = mock(DefaultListableBeanFactory.class); |
105 | 77 |
|
106 | | - // Create listener as if registered in a parent context |
107 | | - var listener = new DeferredRepositoryInitializationListener(mockParentBeanFactory); |
108 | | - |
109 | | - // Create real child contexts (simulates Feign-like scenario) |
110 | | - var childContext1 = new GenericApplicationContext(); |
111 | | - childContext1.refresh(); |
112 | | - |
113 | | - var childContext2 = new GenericApplicationContext(); |
114 | | - childContext2.refresh(); |
115 | | - |
116 | | - // Fire events from child contexts - should be ignored |
117 | | - listener.onApplicationEvent(new ContextRefreshedEvent(childContext1)); |
118 | | - listener.onApplicationEvent(new ContextRefreshedEvent(childContext2)); |
119 | | - |
120 | | - // Verify that getBeansOfType was NOT called for child context events |
121 | | - verify(mockParentBeanFactory, never()).getBeansOfType(Repository.class); |
122 | | - |
123 | | - // Now create a mock parent context that returns our mock beanFactory |
124 | | - ConfigurableApplicationContext mockParentContext = mock(ConfigurableApplicationContext.class); |
125 | | - when(mockParentContext.getBeanFactory()).thenReturn(mockParentBeanFactory); |
126 | | - |
127 | | - // Fire event from "parent" context |
128 | | - listener.onApplicationEvent(new ContextRefreshedEvent(mockParentContext)); |
129 | | - |
130 | | - // Verify that getBeansOfType WAS called exactly once for parent context |
131 | | - verify(mockParentBeanFactory, times(1)).getBeansOfType(Repository.class); |
132 | | - |
133 | | - childContext1.close(); |
134 | | - childContext2.close(); |
135 | | - } |
136 | 78 | } |
0 commit comments