Added a new module (spring-core-5) for the code examples of BAEL-4764. (#10484)

This commit is contained in:
Jordan Simpson
2021-02-13 14:29:50 -06:00
committed by GitHub
parent 89eb93a46c
commit 354018732d
22 changed files with 236 additions and 2 deletions
@@ -0,0 +1,54 @@
package com.baeldung.component.inscope;
import com.baeldung.component.outsidescope.OutsideScopeBeanExample;
import com.baeldung.component.outsidescope.OutsideScopeExample;
import com.baeldung.component.scannedscope.ScannedScopeExample;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
@ExtendWith(SpringExtension.class)
public class ComponentUnitTest {
@Autowired
private ApplicationContext applicationContext;
@Test
public void givenInScopeComponents_whenSearchingInApplicationContext_thenFindThem() {
assertNotNull(applicationContext.getBean(ControllerExample.class));
assertNotNull(applicationContext.getBean(ServiceExample.class));
assertNotNull(applicationContext.getBean(RepositoryExample.class));
assertNotNull(applicationContext.getBean(ComponentExample.class));
assertNotNull(applicationContext.getBean(CustomComponentExample.class));
}
@Test
public void givenOutsideScopeComponent_whenSearchingInApplicationContext_thenFail() {
assertThrows(NoSuchBeanDefinitionException.class, () -> applicationContext.getBean(OutsideScopeExample.class));
}
@Test
public void givenScannedScopeComponent_whenSearchingInApplicationContext_thenFindIt() {
assertNotNull(applicationContext.getBean(ScannedScopeExample.class));
}
@Test
public void givenBeanComponents_whenSearchingInApplicationContext_thenFindThem() {
assertNotNull(applicationContext.getBean(BeanExample.class));
assertNotNull(applicationContext.getBean(OutsideScopeBeanExample.class));
}
@Test
public void givenAmbiguousBeanSetToB_whenSearchingInApplicationContext_thenFindImplB() {
AmbiguousBean ambiguousBean = applicationContext.getBean(AmbiguousBean.class);
assertNotNull(ambiguousBean);
assertTrue(ambiguousBean instanceof BeanImplB);
}
}
@@ -0,0 +1 @@
ambiguous-bean: 'B'