-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAccountRepositoryTest.java
More file actions
102 lines (79 loc) · 3.26 KB
/
AccountRepositoryTest.java
File metadata and controls
102 lines (79 loc) · 3.26 KB
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
package eu.arima.springdatajdbctestcontainers;
import java.util.Arrays;
import java.util.Date;
import org.junit.Assert;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.transaction.annotation.Transactional;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
@Transactional
@SpringBootTest
@Testcontainers
public class AccountRepositoryTest {
@Container
public static PostgreSQLContainer<?> postgresqlContainer = new PostgreSQLContainer<>();
@Autowired
private AccountRepository accountRepository;
@Autowired
private NamedParameterJdbcTemplate template;
@Test
public void findById() {
Account account = this.accountRepository.findById(1).get();
Assert.assertEquals("user1", account.getUsername());
Assert.assertEquals("user1@company.com", account.getEmail());
System.out.println(account.getCreated());
}
@Test
public void save() {
Account account = new Account();
account.setName("new");
account.setUsername("newusername");
account.setEmail("newemail@company.com");
account.setCreated(new Date());
Account newAccount = this.accountRepository.save(account);
Assert.assertEquals(1, JdbcTestUtils.countRowsInTableWhere(
(JdbcTemplate) template.getJdbcOperations(), "account", "id = " + newAccount.getId()
));
}
@Disabled("Paging repositories not supported https://jira.spring.io/browse/DATAJDBC-101")
@Test
public void pageable() {
Pageable pageable = PageRequest.of(0, 20);
Page<Account> accounts = this.accountRepository.findAll(pageable);
Assert.assertEquals(3, accounts.getSize());
}
@Test
public void length() {
int length = this.accountRepository.accountsLength();
Assert.assertEquals(3, length);
}
@Test
public void updateName() {
boolean updated = this.accountRepository.updateName(3, "updatedname");
Assert.assertTrue(updated);
}
@Test
public void deleteList() {
this.accountRepository.deleteIdList(Arrays.asList(1, 2));
int length = this.accountRepository.accountsLength();
Assert.assertEquals(1, length);
}
@DynamicPropertySource
static void postgresqlProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgresqlContainer::getJdbcUrl);
registry.add("spring.datasource.username", postgresqlContainer::getUsername);
registry.add("spring.datasource.password", postgresqlContainer::getPassword);
}
}