|
| 1 | +package com.bobocode; |
| 2 | + |
| 3 | +import com.bobocode.config.AppConfig; |
| 4 | +import com.bobocode.dao.AccountDao; |
| 5 | +import com.bobocode.dao.FakeAccountDao; |
| 6 | +import com.bobocode.model.Account; |
| 7 | +import com.bobocode.service.AccountService; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.context.ApplicationContext; |
| 11 | +import org.springframework.context.annotation.Bean; |
| 12 | +import org.springframework.context.annotation.ComponentScan; |
| 13 | +import org.springframework.context.annotation.Configuration; |
| 14 | +import org.springframework.stereotype.Component; |
| 15 | +import org.springframework.stereotype.Service; |
| 16 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 17 | + |
| 18 | +import java.lang.annotation.Annotation; |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.util.Comparator; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 24 | +import static org.hamcrest.Matchers.arrayContainingInAnyOrder; |
| 25 | +import static org.hamcrest.Matchers.arrayWithSize; |
| 26 | +import static org.hamcrest.Matchers.equalTo; |
| 27 | +import static org.hamcrest.Matchers.hasItem; |
| 28 | +import static org.hamcrest.Matchers.is; |
| 29 | +import static org.hamcrest.Matchers.notNullValue; |
| 30 | + |
| 31 | + |
| 32 | +@SpringJUnitConfig |
| 33 | +class AppConfigTest { |
| 34 | + @Configuration |
| 35 | + @ComponentScan(basePackages = "com.bobocode") |
| 36 | + static class TestConfig { |
| 37 | + } |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private ApplicationContext applicationContext; |
| 41 | + |
| 42 | + @Autowired |
| 43 | + private AccountService accountService; |
| 44 | + |
| 45 | + @Autowired |
| 46 | + private AccountDao accountDao; |
| 47 | + |
| 48 | + @Test |
| 49 | + void testConfigClassIsMarkedAsConfiguration() { |
| 50 | + Configuration configuration = AppConfig.class.getAnnotation(Configuration.class); |
| 51 | + |
| 52 | + assertThat(configuration, notNullValue()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testComponentScanIsEnabled() { |
| 57 | + ComponentScan componentScan = AppConfig.class.getAnnotation(ComponentScan.class); |
| 58 | + |
| 59 | + assertThat(componentScan, notNullValue()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testComponentScanPackagesAreSpecified() { |
| 64 | + ComponentScan componentScan = AppConfig.class.getAnnotation(ComponentScan.class); |
| 65 | + String[] packages = componentScan.basePackages(); |
| 66 | + if (packages.length == 0) { |
| 67 | + packages = componentScan.value(); |
| 68 | + } |
| 69 | + assertThat(packages, arrayContainingInAnyOrder("com.bobocode.dao", "com.bobocode.service")); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void testDataGeneratorHasOnlyOneBean() { |
| 74 | + Map<String, TestDataGenerator> testDataGeneratorMap = applicationContext.getBeansOfType(TestDataGenerator.class); |
| 75 | + |
| 76 | + assertThat(testDataGeneratorMap.size(), is(1)); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void testDataGeneratorBeanIsConfiguredExplicitly() { |
| 81 | + Method[] methods = AppConfig.class.getMethods(); |
| 82 | + Method testDataGeneratorBeanMethod = findTestDataGeneratorBeanMethod(methods); |
| 83 | + |
| 84 | + |
| 85 | + assertThat(testDataGeneratorBeanMethod, notNullValue()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void testDataGeneratorBeanName() { |
| 90 | + Map<String, TestDataGenerator> dataGeneratorBeanMap = applicationContext.getBeansOfType(TestDataGenerator.class); |
| 91 | + |
| 92 | + assertThat(dataGeneratorBeanMap.keySet(), hasItem("dataGenerator")); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void testDataGeneratorBeanNameIsNotSpecifiedExplicitly() { |
| 97 | + Method[] methods = AppConfig.class.getMethods(); |
| 98 | + Method testDataGeneratorBeanMethod = findTestDataGeneratorBeanMethod(methods); |
| 99 | + Bean bean = testDataGeneratorBeanMethod.getDeclaredAnnotation(Bean.class); |
| 100 | + |
| 101 | + assertThat(bean.name(), arrayWithSize(0)); |
| 102 | + assertThat(bean.value(), arrayWithSize(0)); |
| 103 | + } |
| 104 | + |
| 105 | + private Method findTestDataGeneratorBeanMethod(Method[] methods) { |
| 106 | + for (Method method : methods) { |
| 107 | + if (method.getReturnType().equals(TestDataGenerator.class) |
| 108 | + && method.getDeclaredAnnotation(Bean.class) != null) { |
| 109 | + return method; |
| 110 | + } |
| 111 | + } |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void testFakeAccountDaoIsConfiguredAsComponent() { |
| 117 | + Component component = FakeAccountDao.class.getAnnotation(Component.class); |
| 118 | + |
| 119 | + assertThat(component, notNullValue()); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void testAccountDaoHasOnlyOneBean() { |
| 124 | + Map<String, AccountDao> accountDaoBeanMap = applicationContext.getBeansOfType(AccountDao.class); |
| 125 | + |
| 126 | + assertThat(accountDaoBeanMap.size(), is(1)); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void testAccountDaoBeanName() { |
| 131 | + Map<String, AccountDao> accountDaoBeanMap = applicationContext.getBeansOfType(AccountDao.class); |
| 132 | + |
| 133 | + assertThat(accountDaoBeanMap.keySet(), hasItem("accountDao")); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + void testAccountDaoConstructorIsMarkedWithAutowired() throws NoSuchMethodException { |
| 138 | + Autowired autowired = FakeAccountDao.class.getConstructor(TestDataGenerator.class).getAnnotation(Autowired.class); |
| 139 | + |
| 140 | + assertThat(autowired, notNullValue()); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + void testAccountServiceHasOnlyOneBean() { |
| 145 | + Map<String, AccountService> accountServiceMap = applicationContext.getBeansOfType(AccountService.class); |
| 146 | + |
| 147 | + assertThat(accountServiceMap.size(), is(1)); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + void testAccountServiceIsConfiguredAsService() { |
| 152 | + Service service = AccountService.class.getAnnotation(Service.class); |
| 153 | + |
| 154 | + assertThat(service, notNullValue()); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + void testAccountServiceBeanName() { |
| 159 | + Map<String, AccountService> accountServiceMap = applicationContext.getBeansOfType(AccountService.class); |
| 160 | + |
| 161 | + assertThat(accountServiceMap.keySet(), hasItem("accountService")); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + void testAccountServiceBeanNameIsNotSpecifiedExplicitly() { |
| 166 | + Service service = AccountService.class.getAnnotation(Service.class); |
| 167 | + |
| 168 | + assertThat(service.value(), equalTo("")); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + void testAccountServiceDoesNotUseAutowired() throws NoSuchMethodException { |
| 173 | + Annotation[] annotations = AccountService.class.getConstructor(AccountDao.class).getDeclaredAnnotations(); |
| 174 | + |
| 175 | + assertThat(annotations, arrayWithSize(0)); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + void testFindRichestAccount() { |
| 180 | + Account richestAccount = accountService.findRichestAccount(); |
| 181 | + |
| 182 | + Account actualRichestAccount = accountDao.findAll().stream().max(Comparator.comparing(Account::getBalance)).get(); |
| 183 | + |
| 184 | + assertThat(richestAccount, equalTo(actualRichestAccount)); |
| 185 | + } |
| 186 | + |
| 187 | + |
| 188 | +} |
0 commit comments