-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInitializedDatabaseTest.java
53 lines (46 loc) · 1.73 KB
/
InitializedDatabaseTest.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;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Duration;
import oracle.jdbc.pool.OracleDataSource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.testcontainers.oracle.OracleContainer;
import static org.assertj.core.api.Assertions.assertThat;
public class InitializedDatabaseTest {
/**
* Use a containerized Oracle Database instance for testing.
*/
static OracleContainer oracleContainer = new OracleContainer("gvenzl/oracle-free:23.7-slim-faststart")
.withStartupTimeout(Duration.ofMinutes(5))
.withUsername("testuser")
.withPassword("testpwd")
.withInitScript("students.sql");
static OracleDataSource ds;
@BeforeAll
static void setUp() throws SQLException {
oracleContainer.start();
// Configure the OracleDataSource to use the database container
ds = new OracleDataSource();
ds.setURL(oracleContainer.getJdbcUrl());
ds.setUser(oracleContainer.getUsername());
ds.setPassword(oracleContainer.getPassword());
}
/**
* Verifies the database is initialized with a student
* @throws SQLException
*/
@Test
void getStudent() throws SQLException {
// Query Database version to verify connection
try (Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from students where first_name = 'Alice'")) {
Assertions.assertTrue(rs.next());
assertThat(rs.getString(2)).isEqualTo("Alice");
}
}
}