2025-06-23 13:11:44 +01:00
|
|
|
package stirling.software.common.util;
|
|
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
import static org.mockito.Mockito.*;
|
|
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
|
|
|
|
|
class SpringContextHolderTest {
|
|
|
|
|
|
|
|
|
|
private ApplicationContext mockApplicationContext;
|
|
|
|
|
private SpringContextHolder contextHolder;
|
|
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
|
void setUp() {
|
|
|
|
|
mockApplicationContext = mock(ApplicationContext.class);
|
|
|
|
|
contextHolder = new SpringContextHolder();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testSetApplicationContext() {
|
|
|
|
|
// Act
|
|
|
|
|
contextHolder.setApplicationContext(mockApplicationContext);
|
2025-07-01 15:41:23 +01:00
|
|
|
|
2025-06-23 13:11:44 +01:00
|
|
|
// Assert
|
|
|
|
|
assertTrue(SpringContextHolder.isInitialized());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testGetBean_ByType() {
|
|
|
|
|
// Arrange
|
|
|
|
|
contextHolder.setApplicationContext(mockApplicationContext);
|
|
|
|
|
TestBean expectedBean = new TestBean();
|
|
|
|
|
when(mockApplicationContext.getBean(TestBean.class)).thenReturn(expectedBean);
|
2025-07-01 15:41:23 +01:00
|
|
|
|
2025-06-23 13:11:44 +01:00
|
|
|
// Act
|
|
|
|
|
TestBean result = SpringContextHolder.getBean(TestBean.class);
|
2025-07-01 15:41:23 +01:00
|
|
|
|
2025-06-23 13:11:44 +01:00
|
|
|
// Assert
|
|
|
|
|
assertSame(expectedBean, result);
|
|
|
|
|
verify(mockApplicationContext).getBean(TestBean.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testGetBean_ApplicationContextNotSet() {
|
|
|
|
|
// Don't set application context
|
2025-07-01 15:41:23 +01:00
|
|
|
|
2025-06-23 13:11:44 +01:00
|
|
|
// Act
|
|
|
|
|
TestBean result = SpringContextHolder.getBean(TestBean.class);
|
2025-07-01 15:41:23 +01:00
|
|
|
|
2025-06-23 13:11:44 +01:00
|
|
|
// Assert
|
|
|
|
|
assertNull(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testGetBean_BeanNotFound() {
|
|
|
|
|
// Arrange
|
|
|
|
|
contextHolder.setApplicationContext(mockApplicationContext);
|
|
|
|
|
when(mockApplicationContext.getBean(TestBean.class)).thenThrow(new org.springframework.beans.BeansException("Bean not found") {});
|
2025-07-01 15:41:23 +01:00
|
|
|
|
2025-06-23 13:11:44 +01:00
|
|
|
// Act
|
|
|
|
|
TestBean result = SpringContextHolder.getBean(TestBean.class);
|
2025-07-01 15:41:23 +01:00
|
|
|
|
2025-06-23 13:11:44 +01:00
|
|
|
// Assert
|
|
|
|
|
assertNull(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Simple test class
|
|
|
|
|
private static class TestBean {
|
|
|
|
|
}
|
2025-07-01 15:41:23 +01:00
|
|
|
}
|