From 3c3035eb08786dc8b90b9045fb5e1f172fb3ad0a Mon Sep 17 00:00:00 2001 From: AdrianBalunan Date: Sun, 15 Mar 2026 19:03:05 +0100 Subject: [PATCH] Feat: DatabaseConnectionTest implemented --- .../database/DatabaseConnectionTest.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DatabaseConnectionTest.java diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DatabaseConnectionTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DatabaseConnectionTest.java new file mode 100644 index 0000000..0bfc9b6 --- /dev/null +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DatabaseConnectionTest.java @@ -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(); + } +} \ No newline at end of file