-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathCustomerServiceTest.java
103 lines (89 loc) · 4.43 KB
/
CustomerServiceTest.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.example.service;
import com.example.persistence.config.DataSourceConfig;
import com.example.persistence.config.JdbcConfig;
import com.example.persistence.entity.Customer;
import com.example.service.config.ServiceConfig;
import com.example.service.impl.CustomerServiceImpl;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.sql.DataSource;
import java.lang.reflect.Method;
import java.sql.Date;
import java.time.LocalDate;
import java.util.Map;
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.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
// TODO 2-13 このテストを実行して、CustomerServiceの実装が正しいかチェックする(テストがグリーンになればOK)
@SpringJUnitConfig(classes = {DataSourceConfig.class, JdbcConfig.class, ServiceConfig.class})
public class CustomerServiceTest {
@Autowired
CustomerService customerService;
JdbcTemplate jdbcTemplate;
@Autowired
public void setJdbcTemplate(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Test
@DisplayName("findAll()で顧客が全件(5件)取得できる")
public void findAllTest() {
Method findAllMethod = assertDoesNotThrow(
() -> CustomerServiceImpl.class.getMethod("findAll"));
Transactional transactional = findAllMethod.getAnnotation(Transactional.class);
assertAll("Transactional annotation not configured correctly",
() -> assertNotNull(transactional),
() -> assertEquals(Propagation.REQUIRED, transactional.propagation()),
() -> assertTrue(transactional.readOnly())
);
Iterable<Customer> customers = customerService.findAll();
int count = 0;
for (Customer customer : customers) {
assertAll(
() -> assertNotNull(customer.getId()),
() -> assertNotNull(customer.getFirstName()),
() -> assertNotNull(customer.getLastName()),
() -> assertNotNull(customer.getEmail()),
() -> assertNotNull(customer.getBirthday())
);
count++;
}
assertEquals(5, count);
}
@Test
@DisplayName("save()で顧客が1件追加できる")
@Transactional // テスト終了後にロールバック
public void saveTest() {
Method saveMethod = assertDoesNotThrow(
() -> CustomerServiceImpl.class.getMethod("save", Customer.class));
Transactional transactional = saveMethod.getAnnotation(Transactional.class);
assertAll("Transactional annotation not configured correctly",
() -> assertNotNull(transactional),
() -> assertEquals(Propagation.REQUIRED, transactional.propagation()),
() -> assertFalse(transactional.readOnly())
);
Customer newCustomer = new Customer("絵梨花", "生田",
"[email protected]", LocalDate.of(1997, 1, 22));
customerService.save(newCustomer);
assertEquals(Integer.valueOf(6), newCustomer.getId());
assertEquals(6, JdbcTestUtils.countRowsInTable(jdbcTemplate, "customer"));
Map<String, Object> map = jdbcTemplate.queryForMap(
"SELECT id, first_name, last_name, email, birthday" +
" FROM customer WHERE id = ?", 6);
assertAll(
() -> assertEquals(newCustomer.getId(), map.get("id")),
() -> assertEquals(newCustomer.getFirstName(), map.get("first_name")),
() -> assertEquals(newCustomer.getLastName(), map.get("last_name")),
() -> assertEquals(newCustomer.getEmail(), map.get("email")),
() -> assertEquals(newCustomer.getBirthday(), ((Date) map.get("birthday")).toLocalDate())
);
}
}