-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathServiceConfigTest.java
53 lines (45 loc) · 2.63 KB
/
ServiceConfigTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.example.service.config;
import com.example.persistence.config.DataSourceConfig;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.lang.reflect.Method;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
// TODO 2-12 このテストを実行して、ServiceConfigの実装が正しいかチェックする(テストがグリーンになればOK)
public class ServiceConfigTest {
ServiceConfig serviceConfig = new ServiceConfig();
DataSourceConfig dataSourceConfig = new DataSourceConfig();
@Test
@DisplayName("クラスに必要なアノテーションが付加されている")
public void annotationTest() {
Configuration configuration = ServiceConfig.class.getAnnotation(Configuration.class);
ComponentScan componentScan = ServiceConfig.class.getAnnotation(ComponentScan.class);
EnableTransactionManagement enableTransactionManagement = ServiceConfig.class.getAnnotation(EnableTransactionManagement.class);
assertAll("annotations required",
() -> assertNotNull(configuration),
() -> assertNotNull(componentScan),
() -> assertNotNull(enableTransactionManagement),
() -> assertArrayEquals(new String[]{"com.example.service.impl"},
componentScan.basePackages())
);
}
@Test
@DisplayName("transactionManagerのBeanが定義されている")
public void transactionManagerTest() {
Method transactionManagerMethod = assertDoesNotThrow(() -> ServiceConfig.class.getMethod("transactionManager", DataSource.class));
Bean bean = transactionManagerMethod.getAnnotation(Bean.class);
assertNotNull(bean, "annotation required");
PlatformTransactionManager transactionManager = serviceConfig.transactionManager(dataSourceConfig.dataSource());
assertTrue(transactionManager instanceof DataSourceTransactionManager, "not correct transaction manager");
}
}