-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGetDatabaseConnectionTest.java
48 lines (42 loc) · 1.49 KB
/
GetDatabaseConnectionTest.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
package com.example;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Duration;
import oracle.jdbc.pool.OracleDataSource;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.oracle.OracleContainer;
@Testcontainers
public class GetDatabaseConnectionTest {
/**
* Use a containerized Oracle Database instance for testing.
*/
static OracleContainer oracleContainer = new OracleContainer("gvenzl/oracle-free:23.6-slim-faststart")
.withStartupTimeout(Duration.ofMinutes(5))
.withUsername("testuser")
.withPassword("testpwd");
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 containerized database connection.
* @throws SQLException
*/
@Test
void getConnection() throws SQLException {
// Query Database version to verify connection
try (Connection conn = ds.getConnection();
Statement stmt = conn.createStatement()) {
stmt.executeQuery("select * from v$version");
}
}
}