BAEL-3828 Where Should @Service Annotation Be Kept? (#10430)

* BAEL-3828 add code samples and tests for the article

* BAEL-3828 fix maven test folder structure and test executions

* BAEL-3828 fix indentation in continuation lines
This commit is contained in:
Yavuz Tas
2021-01-26 16:10:30 +01:00
committed by GitHub
parent b018825dc4
commit 609c4db4fb
10 changed files with 154 additions and 1 deletions
@@ -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 EmployeeApplicationUnitTest {
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"))));
}
}
@@ -0,0 +1,51 @@
package com.baeldung.annotations.service;
import com.baeldung.annotations.service.abstracts.AbstractAuthenticationService;
import com.baeldung.annotations.service.config.AbstractsAnnotatedTestConfiguration;
import com.baeldung.annotations.service.config.ConcreteClassesAnnotatedTestConfiguration;
import com.baeldung.annotations.service.config.InterfacesAnnotatedTestConfiguration;
import com.baeldung.annotations.service.interfaces.AuthenticationService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
public class AuthApplicationUnitTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
@Test
void whenOnlyInterfacesAnnotated_noSuchBeanDefinitionExceptionThrown() {
contextRunner
.withUserConfiguration(InterfacesAnnotatedTestConfiguration.class)
.run(context -> {
Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> {
context.getBean(AuthenticationService.class);
});
});
}
@Test
void whenOnlyAbstractClassesAnnotated_noSuchBeanDefinitionExceptionThrown() {
contextRunner
.withUserConfiguration(AbstractsAnnotatedTestConfiguration.class)
.run(context -> {
Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> {
context.getBean(AbstractAuthenticationService.class);
});
});
}
@Test
void whenConcreteClassesAnnotated_noExceptionThrown() {
contextRunner
.withUserConfiguration(ConcreteClassesAnnotatedTestConfiguration.class)
.run(context -> {
AuthenticationService inMemoryAuthService = context.getBean(AuthenticationService.class);
AbstractAuthenticationService ldapAuthService = context.getBean(AbstractAuthenticationService.class);
Assertions.assertNotNull(inMemoryAuthService);
Assertions.assertNotNull(ldapAuthService);
});
}
}
@@ -0,0 +1,10 @@
package com.baeldung.annotations.service.config;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.ComponentScan;
@TestConfiguration
@ComponentScan("com.baeldung.annotations.service.abstracts")
public class AbstractsAnnotatedTestConfiguration {
}
@@ -0,0 +1,10 @@
package com.baeldung.annotations.service.config;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.ComponentScan;
@TestConfiguration
@ComponentScan("com.baeldung.annotations.service.concretes")
public class ConcreteClassesAnnotatedTestConfiguration {
}
@@ -0,0 +1,10 @@
package com.baeldung.annotations.service.config;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.ComponentScan;
@TestConfiguration
@ComponentScan("com.baeldung.annotations.service.interfaces")
public class InterfacesAnnotatedTestConfiguration {
}