Skip to content

Commit

Permalink
Feat: DatabaseConnectionTest implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianBalunan committed Mar 15, 2026
1 parent ac74eff commit 3c3035e
Showing 1 changed file with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ntnu.systemutvikling.team6.database;

import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.SQLException;

import static org.junit.jupiter.api.Assertions.*;

class DatabaseConnectionTest {

@Test
void constructorShouldCreateInstanceWithValidCredentials() {
DatabaseConnection db = new DatabaseConnection(
"jdbc:mysql://localhost:3306/test",
"user",
"password"
);

assertNotNull(db);
}

@Test
void constructorShouldThrowExceptionIfUrlIsNull() {
assertThrows(IllegalArgumentException.class, () -> {
new DatabaseConnection(null, "user", "password");
});
}

@Test
void constructorShouldThrowExceptionIfUsernameIsBlank() {
assertThrows(IllegalArgumentException.class, () -> {
new DatabaseConnection("jdbc:mysql://localhost:3306/test", "", "password");
});
}

@Test
void constructorShouldThrowExceptionIfPasswordIsBlank() {
assertThrows(IllegalArgumentException.class, () -> {
new DatabaseConnection("jdbc:mysql://localhost:3306/test", "user", "");
});
}

@Test
void getMySqlConnectionShouldReturnConnection() throws SQLException {

DatabaseConnection db = new DatabaseConnection();

Connection connection = db.getMySqlConnection();

assertNotNull(connection);
assertFalse(connection.isClosed());

connection.close();
}
}

0 comments on commit 3c3035e

Please sign in to comment.