Browse Source

Implements testable function signature and first unit test

feature-credentialneu
fdai5728 3 years ago
parent
commit
950cbae5a4
  1. 2
      src/main/java/Credential.java
  2. 10
      src/main/java/CredentialRepository.java
  3. 2
      src/main/java/CredentialRepositoryInterface.java
  4. 14
      src/test/java/CredentialRepositoryTest.java

2
src/main/java/Credential.java

@ -10,7 +10,7 @@ public class Credential {
public Credential(String name, String password, int id) throws Exception { public Credential(String name, String password, int id) throws Exception {
if(name == null || password == null) if(name == null || password == null)
throw new Exception("kein leeres Objekt erstellen bliat");
throw new Exception("kein leeres Objekt erstellen");
this.name = name; this.name = name;
this.password = password; this.password = password;

10
src/main/java/CredentialRepository.java

@ -10,7 +10,7 @@ public class CredentialRepository implements CredentialRepositoryInterface{
this.credentials = new ArrayList<Credential>(); this.credentials = new ArrayList<Credential>();
} }
public void createNewCredential(String name, String password)
public int createNewCredential(String name, String password)
{ {
try { try {
this.credentials.add(new Credential(name, password, this.idCounter++)); this.credentials.add(new Credential(name, password, this.idCounter++));
@ -19,6 +19,8 @@ public class CredentialRepository implements CredentialRepositoryInterface{
{ {
System.err.println(fail.getMessage()); System.err.println(fail.getMessage());
} }
return this.getListSize();
} }
@Override @Override
@ -50,6 +52,12 @@ public class CredentialRepository implements CredentialRepositoryInterface{
public void loadCredentialsFromJSON() { public void loadCredentialsFromJSON() {
} }
private int getListSize()
{
return this.credentials.size();
}
} }

2
src/main/java/CredentialRepositoryInterface.java

@ -1,6 +1,6 @@
public interface CredentialRepositoryInterface { public interface CredentialRepositoryInterface {
void createNewCredential(String name, String password);
int createNewCredential(String name, String password);
void edit(); void edit();
void delete(); void delete();
void getCredentials(); void getCredentials();

14
src/test/java/CredentialRepositoryTest.java

@ -1,5 +1,6 @@
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
class CredentialRepositoryTest { class CredentialRepositoryTest {
@ -15,8 +16,17 @@ class CredentialRepositoryTest {
@Test @Test
void createNewCredentialAndAddToList() { 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());
*/
Loading…
Cancel
Save