JAVA-19555 Move articles from spring-core-5 module to spring-boot-annotations-2 module (#13711)

* JAVA-19555 Move articles from spring-core-5 module to spring-boot-annotations-2 module

* JAVA-19555 Move articles from spring-core module to spring-boot-annotations-2 module
This commit is contained in:
anuragkumawat
2023-03-26 23:55:47 +05:30
committed by GitHub
parent 2e129bd64b
commit a50d41a8ce
39 changed files with 9 additions and 4 deletions
@@ -0,0 +1,61 @@
package com.baeldung.aliasfor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.Method;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyMappingController.class)
public class AliasForUnitTest {
@Autowired
private ConfigurableApplicationContext context;
Class controllerClass;
@Before
public void setControllerBean() {
MyMappingController controllerBean = context.getBean(MyMappingController.class);
controllerClass = controllerBean.getClass();
}
@Test
public void givenComposedAnnotation_whenExplicitAlias_thenMetaAnnotationAttributeOverridden() {
for (Method method : controllerClass.getMethods()) {
if (method.isAnnotationPresent(MyMapping.class)) {
MyMapping annotation = AnnotationUtils.findAnnotation(method, MyMapping.class);
RequestMapping metaAnnotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
assertEquals(RequestMethod.PATCH, annotation.action()[0]);
assertEquals(0, metaAnnotation.method().length);
}
}
}
@Test
public void givenComposedAnnotation_whenImplictAlias_thenAttributesEqual() {
for (Method method : controllerClass.getMethods()) {
if (method.isAnnotationPresent(MyMapping.class)) {
MyMapping annotationOnBean = AnnotationUtils.findAnnotation(method, MyMapping.class);
assertEquals(annotationOnBean.mapping()[0], annotationOnBean.route()[0]);
assertEquals(annotationOnBean.value()[0], annotationOnBean.route()[0]);
}
}
}
}
@@ -0,0 +1,34 @@
package com.baeldung.lazy;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class LazyAnnotationUnitTest {
@Test
public void givenLazyAnnotation_whenConfigClass_thenLazyAll() {
// Add @Lazy to AppConfig.class while testing
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();
ctx.getBean(Region.class);
ctx.getBean(Country.class);
}
@Test
public void givenLazyAnnotation_whenAutowire_thenLazyBean() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();
Region region = ctx.getBean(Region.class);
region.getCityInstance();
}
@Test
public void givenLazyAnnotation_whenBeanConfig_thenLazyBean() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();
ctx.getBean(Region.class);
}
}
@@ -0,0 +1,65 @@
package com.baeldung.springbean.naming;
import com.baeldung.springbean.naming.component.Cat;
import com.baeldung.springbean.naming.component.Dog;
import com.baeldung.springbean.naming.service.PetShow;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@ExtendWith(SpringExtension.class)
public class SpringBeanNamingUnitTest {
private AnnotationConfigApplicationContext context;
@BeforeEach
void setUp() {
context = new AnnotationConfigApplicationContext();
context.scan("com.baeldung.springbean.naming");
context.refresh();
}
// To name a bean spring gets the class name and converts the first letter to lowercase.
// Default naming strategy of the spring bean which is using class level annotation
@Test
void givenLoggingServiceBeanIsCreated_whenThereIsNoValueProvided_thenBeanNameShouldBeDefaultName() {
assertNotNull(context.getBean("loggingService"));
}
// In this case, to name a bean spring gets the class name and converts the first letter to lowercase.
@Test
void givenAuditServiceBeanIsCreatedWithMethodLevelAnnotation_whenThereIsNoValueProvided_thenBeanNameShouldBeTheNameOfMethod() {
assertNotNull(context.getBean("audit"));
}
// spring will create the bean of type CustomComponent with the name "myBean".
// As we're explicitly giving the name to the bean, spring will use this name to refer to it.
@Test
void givenCustomComponentBeanIsCreate_whenThereIsCustomNameGivenToBean_thenBeanShouldBeIdentifiedByThatName() {
assertNotNull(context.getBean("myBean"));
}
@Test
void givenCustomComponentBeanIsCreated_whenCustomNameIsGivenOnMethodLevelAnnotation_thenBeanShouldBeIdentifiedByThatName() {
assertNotNull(context.getBean("beanComponent"));
assertNotNull(context.getBean("configuration"));
assertNotNull(context.getBean("qualifierComponent"));
}
@Test
void givenMultipleImplementationsOfAnimal_whenFieldIsInjectedWithQualifiedName_thenTheSpecificBeanShouldGetInjected() {
PetShow petShow = (PetShow) context.getBean("petShow");
assertNotNull(context.getBean("cat"));
assertNotNull(context.getBean("dog"));
assertThat(petShow.getCat().getClass()).isEqualTo(Cat.class);
assertThat(petShow.getDog().getClass()).isEqualTo(Dog.class);
}
}