diff --git a/spring-boot-nebula-web-common/pom.xml b/spring-boot-nebula-web-common/pom.xml index cf56da1..36d03c6 100644 --- a/spring-boot-nebula-web-common/pom.xml +++ b/spring-boot-nebula-web-common/pom.xml @@ -28,6 +28,19 @@ spring-boot-nebula-common + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.assertj + assertj-core + test + + diff --git a/spring-boot-nebula-web-common/src/test/java/com/nebula/web/common/utils/SpringBeanUtilsTest.java b/spring-boot-nebula-web-common/src/test/java/com/nebula/web/common/utils/SpringBeanUtilsTest.java new file mode 100644 index 0000000..204caf8 --- /dev/null +++ b/spring-boot-nebula-web-common/src/test/java/com/nebula/web/common/utils/SpringBeanUtilsTest.java @@ -0,0 +1,53 @@ +package com.nebula.web.common.utils; + +import com.nebula.web.common.autoconfigure.NebulaWebCommonAutoConfiguration; +import java.util.Objects; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import static org.junit.jupiter.api.Assertions.assertThrowsExactly; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * @author : wh + * @date : 2024/3/28 09:45 + * @description: + */ +@SpringBootTest(classes = SpringBeanUtilsTest.TestConfig.class) +public class SpringBeanUtilsTest { + + @Test + public void getBean() { + Object bean = SpringBeanUtils.getBean("testBean"); + assertTrue(bean instanceof TestBean); + TestBean bean1 = SpringBeanUtils.getBean(TestBean.class); + assertTrue(Objects.nonNull(bean1)); + assertThrowsExactly(NoSuchBeanDefinitionException.class, () -> SpringBeanUtils.getBean(NoTestBean.class)); + + } + + static final class TestBean { + } + + static final class NoTestBean { + } + + @Configuration + @EnableAutoConfiguration + @ImportAutoConfiguration({NebulaWebCommonAutoConfiguration.class}) + public static class TestConfig { + + @Bean + public TestBean testBean() { + return new TestBean(); + + } + + } + +} \ No newline at end of file