Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Commit e15b8da

Browse files
committed
GP-6: upgrade jdbc-api-exercised with "Junit 5" tests
1 parent 8583675 commit e15b8da

File tree

5 files changed

+115
-98
lines changed

5 files changed

+115
-98
lines changed

account-db-initializer/src/test/java/com/bobocode/AccountDbInitializerTest.java

+22-24
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package com.bobocode;
22

33
import com.bobocode.util.JdbcUtil;
4-
import org.junit.BeforeClass;
5-
import org.junit.Test;
6-
import org.junit.runner.RunWith;
7-
import org.junit.runners.JUnit4;
4+
import org.junit.jupiter.api.BeforeAll;
5+
import org.junit.jupiter.api.Test;
86

97
import javax.sql.DataSource;
108
import java.sql.Connection;
@@ -14,22 +12,22 @@
1412
import java.util.ArrayList;
1513
import java.util.List;
1614

17-
import static org.junit.Assert.assertEquals;
18-
import static org.junit.Assert.assertTrue;
15+
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
17+
1918

20-
@RunWith(JUnit4.class)
2119
public class AccountDbInitializerTest {
2220
private static DataSource dataSource;
2321

24-
@BeforeClass
25-
public static void init() throws SQLException {
22+
@BeforeAll
23+
static void init() throws SQLException {
2624
dataSource = JdbcUtil.createDefaultInMemoryH2DataSource();
2725
AccountDbInitializer dbInitializer = new AccountDbInitializer(dataSource);
2826
dbInitializer.init();
2927
}
3028

3129
@Test
32-
public void testTableHasCorrectName() throws SQLException {
30+
void testTableHasCorrectName() throws SQLException {
3331
try (Connection connection = dataSource.getConnection()) {
3432
Statement statement = connection.createStatement();
3533

@@ -42,7 +40,7 @@ public void testTableHasCorrectName() throws SQLException {
4240
}
4341

4442
@Test
45-
public void testTableHasPrimaryKey() throws SQLException {
43+
void testTableHasPrimaryKey() throws SQLException {
4644
try (Connection connection = dataSource.getConnection()) {
4745
Statement statement = connection.createStatement();
4846
ResultSet resultSet = statement.executeQuery("SELECT * FROM INFORMATION_SCHEMA.CONSTRAINTS" +
@@ -55,7 +53,7 @@ public void testTableHasPrimaryKey() throws SQLException {
5553
}
5654

5755
@Test
58-
public void testPrimaryKeyHasCorrectName() throws SQLException {
56+
void testPrimaryKeyHasCorrectName() throws SQLException {
5957
try (Connection connection = dataSource.getConnection()) {
6058
Statement statement = connection.createStatement();
6159
ResultSet resultSet = statement.executeQuery("SELECT * FROM INFORMATION_SCHEMA.CONSTRAINTS" +
@@ -69,7 +67,7 @@ public void testPrimaryKeyHasCorrectName() throws SQLException {
6967
}
7068

7169
@Test
72-
public void testPrimaryKeyBasedOnIdField() throws SQLException {
70+
void testPrimaryKeyBasedOnIdField() throws SQLException {
7371
try (Connection connection = dataSource.getConnection()) {
7472
Statement statement = connection.createStatement();
7573
ResultSet resultSet = statement.executeQuery("SELECT * FROM INFORMATION_SCHEMA.CONSTRAINTS" +
@@ -83,7 +81,7 @@ public void testPrimaryKeyBasedOnIdField() throws SQLException {
8381
}
8482

8583
@Test
86-
public void testTableHasCorrectAlternativeKey() throws SQLException {
84+
void testTableHasCorrectAlternativeKey() throws SQLException {
8785
try (Connection connection = dataSource.getConnection()) {
8886
Statement statement = connection.createStatement();
8987
ResultSet resultSet = statement.executeQuery("SELECT * FROM INFORMATION_SCHEMA.CONSTRAINTS" +
@@ -99,7 +97,7 @@ public void testTableHasCorrectAlternativeKey() throws SQLException {
9997
}
10098

10199
@Test
102-
public void testTableHasAllRequiredColumns() throws SQLException {
100+
void testTableHasAllRequiredColumns() throws SQLException {
103101
try (Connection connection = dataSource.getConnection()) {
104102
Statement statement = connection.createStatement();
105103
ResultSet resultSet = statement.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS" +
@@ -123,7 +121,7 @@ private List<String> fetchColumnsNames(ResultSet resultSet) throws SQLException
123121

124122

125123
@Test
126-
public void testRequiredColumnsHaveHaveNotNullConstraint() throws SQLException {
124+
void testRequiredColumnsHaveHaveNotNullConstraint() throws SQLException {
127125
try (Connection connection = dataSource.getConnection()) {
128126
Statement statement = connection.createStatement();
129127
ResultSet resultSet = statement.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS" +
@@ -137,7 +135,7 @@ public void testRequiredColumnsHaveHaveNotNullConstraint() throws SQLException {
137135
}
138136

139137
@Test
140-
public void testIdHasTypeBiInteger() throws SQLException {
138+
void testIdHasTypeBiInteger() throws SQLException {
141139
try (Connection connection = dataSource.getConnection()) {
142140
Statement statement = connection.createStatement();
143141
ResultSet resultSet = statement.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS" +
@@ -151,7 +149,7 @@ public void testIdHasTypeBiInteger() throws SQLException {
151149
}
152150

153151
@Test
154-
public void testCreationTimeHasTypeTimestamp() throws SQLException {
152+
void testCreationTimeHasTypeTimestamp() throws SQLException {
155153
try (Connection connection = dataSource.getConnection()) {
156154
Statement statement = connection.createStatement();
157155
ResultSet resultSet = statement.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS" +
@@ -165,7 +163,7 @@ public void testCreationTimeHasTypeTimestamp() throws SQLException {
165163
}
166164

167165
@Test
168-
public void testCreationTimeHasDefaultValue() throws SQLException {
166+
void testCreationTimeHasDefaultValue() throws SQLException {
169167
try (Connection connection = dataSource.getConnection()) {
170168
Statement statement = connection.createStatement();
171169
ResultSet resultSet = statement.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS" +
@@ -179,7 +177,7 @@ public void testCreationTimeHasDefaultValue() throws SQLException {
179177
}
180178

181179
@Test
182-
public void testEmailColumnHasCorrectSize() throws SQLException {
180+
void testEmailColumnHasCorrectSize() throws SQLException {
183181
try (Connection connection = dataSource.getConnection()) {
184182
Statement statement = connection.createStatement();
185183
ResultSet resultSet = statement.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS" +
@@ -195,7 +193,7 @@ public void testEmailColumnHasCorrectSize() throws SQLException {
195193
}
196194

197195
@Test
198-
public void testBirthdayColumnHasCorrectType() throws SQLException {
196+
void testBirthdayColumnHasCorrectType() throws SQLException {
199197
try (Connection connection = dataSource.getConnection()) {
200198
Statement statement = connection.createStatement();
201199
ResultSet resultSet = statement.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS" +
@@ -209,7 +207,7 @@ public void testBirthdayColumnHasCorrectType() throws SQLException {
209207
}
210208

211209
@Test
212-
public void testBalanceColumnHasCorrectType() throws SQLException {
210+
void testBalanceColumnHasCorrectType() throws SQLException {
213211
try (Connection connection = dataSource.getConnection()) {
214212
Statement statement = connection.createStatement();
215213
ResultSet resultSet = statement.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS" +
@@ -227,7 +225,7 @@ public void testBalanceColumnHasCorrectType() throws SQLException {
227225
}
228226

229227
@Test
230-
public void testBalanceIsNotMandatory() throws SQLException {
228+
void testBalanceIsNotMandatory() throws SQLException {
231229
try (Connection connection = dataSource.getConnection()) {
232230
Statement statement = connection.createStatement();
233231
ResultSet resultSet = statement.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS" +
@@ -241,7 +239,7 @@ public void testBalanceIsNotMandatory() throws SQLException {
241239
}
242240

243241
@Test
244-
public void testStringColumnsHaveCorrectTypeAndLength() throws SQLException {
242+
void testStringColumnsHaveCorrectTypeAndLength() throws SQLException {
245243
try (Connection connection = dataSource.getConnection()) {
246244
Statement statement = connection.createStatement();
247245
ResultSet resultSet = statement.executeQuery("SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS" +

pom.xml

+16-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545
<version>1.7.24</version>
4646
</dependency>
4747
<dependency>
48-
<groupId>junit</groupId>
49-
<artifactId>junit</artifactId>
50-
<version>4.12</version>
48+
<groupId>org.junit.jupiter</groupId>
49+
<artifactId>junit-jupiter-engine</artifactId>
50+
<version>5.5.1</version>
5151
<scope>test</scope>
5252
</dependency>
5353
<dependency>
@@ -63,4 +63,17 @@
6363
</dependency>
6464

6565
</dependencies>
66+
67+
<build>
68+
<pluginManagement>
69+
<plugins>
70+
<plugin>
71+
<groupId>org.apache.maven.plugins</groupId>
72+
<artifactId>maven-surefire-plugin</artifactId>
73+
<version>3.0.0-M3</version>
74+
</plugin>
75+
</plugins>
76+
</pluginManagement>
77+
</build>
78+
6679
</project>

product-dao/src/test/java/com/bobocode/ProductDaoTest.java

+20-19
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
import com.bobocode.util.JdbcUtil;
88
import org.apache.commons.lang3.RandomStringUtils;
99
import org.apache.commons.lang3.RandomUtils;
10-
import org.junit.BeforeClass;
11-
import org.junit.Test;
12-
import org.junit.runner.RunWith;
13-
import org.junit.runners.JUnit4;
10+
import org.junit.jupiter.api.BeforeAll;
11+
import org.junit.jupiter.api.Test;
1412

1513
import javax.sql.DataSource;
1614
import java.math.BigDecimal;
@@ -21,14 +19,17 @@
2119
import java.time.Month;
2220
import java.util.List;
2321

24-
import static org.junit.Assert.*;
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertFalse;
24+
import static org.junit.jupiter.api.Assertions.assertNotNull;
25+
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
import static org.junit.jupiter.api.Assertions.fail;
2527

26-
@RunWith(JUnit4.class)
2728
public class ProductDaoTest {
2829
private static ProductDao productDao;
2930

30-
@BeforeClass
31-
public static void init() throws SQLException {
31+
@BeforeAll
32+
static void init() throws SQLException {
3233
DataSource h2DataSource = JdbcUtil.createDefaultInMemoryH2DataSource();
3334
createAccountTable(h2DataSource);
3435
productDao = new ProductDaoImpl(h2DataSource);
@@ -62,7 +63,7 @@ private Product generateTestProduct() {
6263
}
6364

6465
@Test
65-
public void testSave() {
66+
void testSave() {
6667
Product fanta = createTestFantaProduct();
6768

6869
int productsCountBeforeInsert = productDao.findAll().size();
@@ -75,7 +76,7 @@ public void testSave() {
7576
}
7677

7778
@Test
78-
public void testSaveInvalidProduct() {
79+
void testSaveInvalidProduct() {
7980
Product invalidTestProduct = createInvalidTestProduct();
8081

8182
try {
@@ -104,7 +105,7 @@ private Product createInvalidTestProduct() {
104105

105106

106107
@Test
107-
public void testFindAll() {
108+
void testFindAll() {
108109
List<Product> newProducts = createTestProductList();
109110
List<Product> oldProducts = productDao.findAll();
110111
newProducts.forEach(productDao::save);
@@ -138,7 +139,7 @@ private List<Product> createTestProductList() {
138139
}
139140

140141
@Test
141-
public void testFindById() {
142+
void testFindById() {
142143
Product testProduct = generateTestProduct();
143144
productDao.save(testProduct);
144145

@@ -152,7 +153,7 @@ public void testFindById() {
152153
}
153154

154155
@Test
155-
public void testFindByNotExistingId() {
156+
void testFindByNotExistingId() {
156157
long invalidId = -1L;
157158
try {
158159
productDao.findOne(invalidId);
@@ -164,7 +165,7 @@ public void testFindByNotExistingId() {
164165
}
165166

166167
@Test
167-
public void testUpdate() {
168+
void testUpdate() {
168169
Product testProduct = generateTestProduct();
169170
productDao.save(testProduct);
170171
List<Product> productsBeforeUpdate = productDao.findAll();
@@ -206,7 +207,7 @@ private Product findById(List<Product> products, Long id) {
206207
}
207208

208209
@Test
209-
public void testUpdateNotStored() {
210+
void testUpdateNotStored() {
210211
Product notStoredProduct = generateTestProduct();
211212

212213
try {
@@ -219,7 +220,7 @@ public void testUpdateNotStored() {
219220
}
220221

221222
@Test
222-
public void testUpdateProductWithInvalidId() {
223+
void testUpdateProductWithInvalidId() {
223224
Product testProduct = generateTestProduct();
224225
long invalidId = -1L;
225226
testProduct.setId(invalidId);
@@ -234,7 +235,7 @@ public void testUpdateProductWithInvalidId() {
234235
}
235236

236237
@Test
237-
public void testRemove() {
238+
void testRemove() {
238239
Product testProduct = generateTestProduct();
239240
productDao.save(testProduct);
240241
List<Product> productsBeforeRemove = productDao.findAll();
@@ -247,7 +248,7 @@ public void testRemove() {
247248
}
248249

249250
@Test
250-
public void testRemoveNotStored() {
251+
void testRemoveNotStored() {
251252
Product notStoredProduct = generateTestProduct();
252253

253254
try {
@@ -260,7 +261,7 @@ public void testRemoveNotStored() {
260261
}
261262

262263
@Test
263-
public void testRemoveProductWithInvalidId() {
264+
void testRemoveProductWithInvalidId() {
264265
Product testProduct = generateTestProduct();
265266
long invalidId = -1L;
266267
testProduct.setId(invalidId);

0 commit comments

Comments
 (0)