This is an automated email from the ASF dual-hosted git repository.

gongchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git


The following commit(s) were added to refs/heads/master by this push:
     new acc617f9b [Improve] add springContextHolder unit test (#2287)
acc617f9b is described below

commit acc617f9bd9c027ef2fdfa2ffa5af0ec28703ff2
Author: YuLuo <[email protected]>
AuthorDate: Tue Jul 16 21:33:50 2024 +0800

    [Improve] add springContextHolder unit test (#2287)
    
    Signed-off-by: yuluo-yx <[email protected]>
    Co-authored-by: tomsun28 <[email protected]>
---
 .../common/support/SpringContextHolderTest.java    | 75 ++++++++++++++++++++--
 1 file changed, 69 insertions(+), 6 deletions(-)

diff --git 
a/common/src/test/java/org/apache/hertzbeat/common/support/SpringContextHolderTest.java
 
b/common/src/test/java/org/apache/hertzbeat/common/support/SpringContextHolderTest.java
index c49597455..e3b208b96 100644
--- 
a/common/src/test/java/org/apache/hertzbeat/common/support/SpringContextHolderTest.java
+++ 
b/common/src/test/java/org/apache/hertzbeat/common/support/SpringContextHolderTest.java
@@ -20,28 +20,91 @@ package org.apache.hertzbeat.common.support;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ConfigurableApplicationContext;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
 /**
  * Test case for {@link SpringContextHolder}
  */
 class SpringContextHolderTest {
 
+    private ApplicationContext applicationContext;
+
+    private ConfigurableApplicationContext configurableApplicationContext;
+
+    private SpringContextHolder springContextHolder;
+
     @BeforeEach
-    void setUp() {
+    public void setUp() {
+
+        applicationContext = mock(ApplicationContext.class);
+        configurableApplicationContext = 
mock(ConfigurableApplicationContext.class);
+        springContextHolder = new SpringContextHolder();
     }
 
     @Test
-    void setApplicationContext() {
+    public void testSetApplicationContext() throws BeansException {
+
+        
springContextHolder.setApplicationContext(configurableApplicationContext);
+
+        assertNotNull(SpringContextHolder.getApplicationContext());
     }
 
     @Test
-    void getApplicationContext() {
+    public void testGetApplicationContext() {
+
+        springContextHolder.setApplicationContext(applicationContext);
+        assertNotNull(SpringContextHolder.getApplicationContext());
     }
 
     @Test
-    void getBean() {
+    public void testGetBeanByClass() {
+
+        Class<String> beanClass = String.class;
+        String bean = "bean";
+        when(applicationContext.getBean(beanClass)).thenReturn(bean);
+
+        springContextHolder.setApplicationContext(applicationContext);
+        String retrievedBean = SpringContextHolder.getBean(beanClass);
+
+        assertEquals(bean, retrievedBean);
     }
 
     @Test
-    void testGetBean() {
+    public void testShutdown() {
+
+        
springContextHolder.setApplicationContext(configurableApplicationContext);
+        SpringContextHolder.shutdown();
+
+        verify(configurableApplicationContext, times(1)).close();
+    }
+
+    @Test
+    public void testIsActive() {
+
+        when(configurableApplicationContext.isActive()).thenReturn(true);
+        
springContextHolder.setApplicationContext(configurableApplicationContext);
+        assertTrue(SpringContextHolder.isActive());
     }
-}
\ No newline at end of file
+
+    @Test
+    public void testAssertApplicationContextThrowsException() {
+
+        RuntimeException exception = assertThrows(RuntimeException.class, 
SpringContextHolder::getApplicationContext);
+        assertEquals(
+                "applicationContext is null, please inject the 
springContextHolder",
+                exception.getMessage()
+        );
+    }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to