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:
parent
b018825dc4
commit
609c4db4fb
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.annotations.service;
|
||||
|
||||
import com.baeldung.annotations.service.abstracts.AbstractAuthenticationService;
|
||||
import com.baeldung.annotations.service.interfaces.AuthenticationService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class AuthApplication {
|
||||
|
||||
@Autowired
|
||||
private AuthenticationService inMemoryAuthService;
|
||||
|
||||
@Autowired
|
||||
private AbstractAuthenticationService ldapAuthService;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AuthApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.annotations.service.abstracts;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public abstract class AbstractAuthenticationService {
|
||||
|
||||
public boolean authenticate(String username, String password) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.annotations.service.concretes;
|
||||
|
||||
import com.baeldung.annotations.service.interfaces.AuthenticationService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class InMemoryAuthenticationService implements AuthenticationService {
|
||||
|
||||
@Override
|
||||
public boolean authenticate(String username, String password) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.annotations.service.concretes;
|
||||
|
||||
import com.baeldung.annotations.service.abstracts.AbstractAuthenticationService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class LdapAuthenticationService extends AbstractAuthenticationService {
|
||||
|
||||
@Override
|
||||
public boolean authenticate(String username, String password) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.annotations.service.interfaces;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public interface AuthenticationService {
|
||||
|
||||
boolean authenticate(String username, String password);
|
||||
|
||||
}
|
|
@ -6,7 +6,7 @@ import static org.junit.Assert.assertFalse;
|
|||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
|
||||
public class EmployeeApplicationTest {
|
||||
public class EmployeeApplicationUnitTest {
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withUserConfiguration(EmployeeApplication.class);
|
||||
|
|
@ -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 {
|
||||
|
||||
}
|
Loading…
Reference in New Issue