add SpringBeanUtilsTest

This commit is contained in:
weihu 2024-03-28 10:12:04 +08:00
parent 2af3ebc1aa
commit ed1a3dc361
2 changed files with 65 additions and 0 deletions

View File

@ -28,6 +28,18 @@
<groupId>io.github.weihubeats</groupId>
<artifactId>spring-boot-nebula-common</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -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();
}
}
}