From 950cbae5a4f2fbc0c84d1b79b78a3b63621044a5 Mon Sep 17 00:00:00 2001 From: fdai5728 Date: Wed, 16 Feb 2022 17:09:09 +0100 Subject: [PATCH] Implements testable function signature and first unit test --- src/main/java/Credential.java | 2 +- src/main/java/CredentialRepository.java | 10 +++++++++- src/main/java/CredentialRepositoryInterface.java | 2 +- src/test/java/CredentialRepositoryTest.java | 14 ++++++++++++-- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/main/java/Credential.java b/src/main/java/Credential.java index 702a60b..b7b4fbd 100644 --- a/src/main/java/Credential.java +++ b/src/main/java/Credential.java @@ -10,7 +10,7 @@ public class Credential { public Credential(String name, String password, int id) throws Exception { if(name == null || password == null) - throw new Exception("kein leeres Objekt erstellen bliat"); + throw new Exception("kein leeres Objekt erstellen"); this.name = name; this.password = password; diff --git a/src/main/java/CredentialRepository.java b/src/main/java/CredentialRepository.java index 3964dd6..1a5a6b5 100644 --- a/src/main/java/CredentialRepository.java +++ b/src/main/java/CredentialRepository.java @@ -10,7 +10,7 @@ public class CredentialRepository implements CredentialRepositoryInterface{ this.credentials = new ArrayList(); } - public void createNewCredential(String name, String password) + public int createNewCredential(String name, String password) { try { this.credentials.add(new Credential(name, password, this.idCounter++)); @@ -19,6 +19,8 @@ public class CredentialRepository implements CredentialRepositoryInterface{ { System.err.println(fail.getMessage()); } + + return this.getListSize(); } @Override @@ -50,6 +52,12 @@ public class CredentialRepository implements CredentialRepositoryInterface{ public void loadCredentialsFromJSON() { } + + private int getListSize() + { + return this.credentials.size(); + } + } diff --git a/src/main/java/CredentialRepositoryInterface.java b/src/main/java/CredentialRepositoryInterface.java index 055b59e..7730f2d 100644 --- a/src/main/java/CredentialRepositoryInterface.java +++ b/src/main/java/CredentialRepositoryInterface.java @@ -1,6 +1,6 @@ public interface CredentialRepositoryInterface { - void createNewCredential(String name, String password); + int createNewCredential(String name, String password); void edit(); void delete(); void getCredentials(); diff --git a/src/test/java/CredentialRepositoryTest.java b/src/test/java/CredentialRepositoryTest.java index bf29066..d3a6f67 100644 --- a/src/test/java/CredentialRepositoryTest.java +++ b/src/test/java/CredentialRepositoryTest.java @@ -1,5 +1,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; class CredentialRepositoryTest { @@ -15,8 +16,17 @@ class CredentialRepositoryTest { @Test void createNewCredentialAndAddToList() { - - + Assertions.assertEquals(1, this.credentialRepository.createNewCredential("mock", "mock")); + Assertions.assertEquals(2, this.credentialRepository.createNewCredential("kek", "mate")); } } + + + /* + Exception failed = Assertions.assertThrows(Exception.class, () -> { + this.credentialRepository.createNewCredential(null,null); + }); + + Assertions.assertEquals("kein leeres Objekt erstellen", failed.getMessage()); + */ \ No newline at end of file