Added unit test

This commit is contained in:
Anirban Chatterjee 2020-10-06 22:33:24 +02:00
parent 14593ca02a
commit bd53673e2b
2 changed files with 26 additions and 5 deletions

View File

@ -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"));

View File

@ -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"))));
}
}