diff --git a/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/EmployeeApplication.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/EmployeeApplication.java similarity index 86% rename from spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/EmployeeApplication.java rename to spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/EmployeeApplication.java index 578479a81c..263df30e16 100644 --- a/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/EmployeeApplication.java +++ b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/EmployeeApplication.java @@ -1,4 +1,4 @@ -package com.baeldung.annotations.componentscanautoconfigure; +package com.baeldung.annotations; import com.baeldung.annotations.componentscanautoconfigure.teacher.Teacher; import org.springframework.boot.SpringApplication; @@ -6,15 +6,13 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -//@Configuration -@ComponentScan(basePackages = {"com.baeldung.annotations.componentscanautoconfigure.healthcare", "com.baeldung.annotations.componentscanautoconfigure.employee"}, +@ComponentScan(basePackages = {"com.baeldung.annotations.componentscanautoconfigure.healthcare", + "com.baeldung.annotations.componentscanautoconfigure.employee"}, basePackageClasses = Teacher.class) @EnableAutoConfiguration(exclude = {JdbcTemplateAutoConfiguration.class}) //@EnableAutoConfiguration(excludeName = {"org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration"}) public class EmployeeApplication { - public static void main(String[] args) { ApplicationContext context = SpringApplication.run(EmployeeApplication.class, args); System.out.println("Configures Employee: " + context.containsBeanDefinition("employee")); diff --git a/spring-boot-modules/spring-boot-annotations/src/main/test/com.baeldung.annotations/EmployeeApplicationTest.java b/spring-boot-modules/spring-boot-annotations/src/main/test/com.baeldung.annotations/EmployeeApplicationTest.java new file mode 100644 index 0000000000..66700cf781 --- /dev/null +++ b/spring-boot-modules/spring-boot-annotations/src/main/test/com.baeldung.annotations/EmployeeApplicationTest.java @@ -0,0 +1,23 @@ +package com.baeldung.annotations; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertAll; + +public class EmployeeApplicationTest { + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withUserConfiguration(EmployeeApplication.class); + + @Test + void whenApplicationContextRuns_thenContainAllDefinedBeans() { + contextRunner.run(context -> assertAll( + () -> assertTrue(context.containsBeanDefinition("employee")), + () -> assertTrue(context.containsBeanDefinition("seniorEmployee")), + () -> assertTrue(context.containsBeanDefinition("doctor")), + () -> assertTrue(context.containsBeanDefinition("hospital")), + () -> assertFalse(context.containsBeanDefinition("student")), + () -> assertTrue(context.containsBeanDefinition("teacher")))); + } +}