add SpringBeanUtilsTest
This commit is contained in:
parent
2af3ebc1aa
commit
ed1a3dc361
|
@ -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>
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue