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