-
Notifications
You must be signed in to change notification settings - Fork 25
/
DataSourceConfigTest.java
42 lines (35 loc) · 1.96 KB
/
DataSourceConfigTest.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
package com.example.persistence.config;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.test.jdbc.JdbcTestUtils;
import java.lang.reflect.Method;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
// TODO 1-05 このテストを実行して、DataSourceConfigの実装が正しいかチェックする(テストがグリーンになればOK)
public class DataSourceConfigTest {
@Test
@DisplayName("クラスに必要なアノテーションが付加されている")
public void annotationTest() {
Configuration configuration = DataSourceConfig.class.getAnnotation(Configuration.class);
assertNotNull(configuration, "annotation required");
}
@Test
@DisplayName("DataSourceのBeanが正しく定義されている")
public void dataSourceTest() {
Method dataSourceMethod = assertDoesNotThrow(() -> DataSourceConfig.class.getMethod("dataSource"));
assertNotNull(dataSourceMethod.getAnnotation(Bean.class), "annotation required");
EmbeddedDatabase dataSource = new DataSourceConfig().dataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
assertAll("SQL files are not executed correctly",
() -> assertEquals(5, JdbcTestUtils.countRowsInTable(jdbcTemplate, "customer")),
() -> assertEquals(3, JdbcTestUtils.countRowsInTable(jdbcTemplate, "account")),
() -> assertEquals(3, JdbcTestUtils.countRowsInTable(jdbcTemplate, "account_authority"))
);
}
}