Skip to content

Commit

Permalink
Merge pull request #11 from weihubeats/add-spring-boot-nebula-all
Browse files Browse the repository at this point in the history
add SpringBeanUtilsTest
  • Loading branch information
weihubeats authored Mar 28, 2024
2 parents 4736105 + d61f85d commit 927930e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
13 changes: 13 additions & 0 deletions spring-boot-nebula-web-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@
<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>

</project>
Original file line number Diff line number Diff line change
@@ -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();

}

}

}

0 comments on commit 927930e

Please sign in to comment.