|
| 1 | +package com.bobocode; |
| 2 | + |
| 3 | +import com.bobocode.config.RootConfig; |
| 4 | +import com.bobocode.dao.impl.FakeAccountDao; |
| 5 | +import com.bobocode.service.AccountService; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.springframework.context.annotation.ComponentScan; |
| 8 | +import org.springframework.context.annotation.Import; |
| 9 | +import org.springframework.stereotype.Component; |
| 10 | +import org.springframework.stereotype.Service; |
| 11 | + |
| 12 | +import java.lang.annotation.Annotation; |
| 13 | +import java.lang.reflect.Method; |
| 14 | +import java.util.List; |
| 15 | +import java.util.stream.Collectors; |
| 16 | +import java.util.stream.Stream; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +class RootConfigHacksTest { |
| 21 | + |
| 22 | + @Test |
| 23 | + void rootConfigShouldNotUseComponentScan() { |
| 24 | + ComponentScan componentScan = RootConfig.class.getAnnotation(ComponentScan.class); |
| 25 | + |
| 26 | + assertThat(componentScan).isNull(); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + void rootConfigShouldNotImportOtherConfigs() { |
| 31 | + Import importAnnotation = RootConfig.class.getAnnotation(Import.class); |
| 32 | + |
| 33 | + assertThat(importAnnotation).isNull(); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void accountServiceBeanShouldBeConfiguredExplicitly() { |
| 38 | + Annotation[] annotations = AccountService.class.getAnnotations(); |
| 39 | + List<Class> annotationClasses = Stream.of(annotations).map(Annotation::annotationType).collect(Collectors.toList()); |
| 40 | + |
| 41 | + assertThat(annotationClasses).doesNotContain(Component.class, Service.class); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void fakeAccountDaoBeanShouldBeConfiguredExplicitly() { |
| 46 | + Annotation[] annotations = FakeAccountDao.class.getAnnotations(); |
| 47 | + List<Class> annotationClasses = Stream.of(annotations).map(Annotation::annotationType).collect(Collectors.toList()); |
| 48 | + |
| 49 | + assertThat(annotationClasses).doesNotContain(Component.class, Service.class); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void rootConfigShouldUseInterBeanDependencies() { |
| 54 | + Method[] methods = RootConfig.class.getDeclaredMethods(); |
| 55 | + |
| 56 | + assertThat(methods).noneMatch(method -> method.getParameterCount() > 0); |
| 57 | + } |
| 58 | +} |
0 commit comments