diff --git a/.gitignore b/.gitignore index 1d5ef7b..7f27d00 100644 Binary files a/.gitignore and b/.gitignore differ diff --git a/src/main/java/assertions/Person.java b/src/main/java/assertions/Person.java index ac7666b..c8d2494 100644 --- a/src/main/java/assertions/Person.java +++ b/src/main/java/assertions/Person.java @@ -22,4 +22,13 @@ public String getFullName(){ public Period getAge(){ return Period.between(birthDate, LocalDate.now()); } + + @Override + public String toString() { + return "Person{" + + "firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", birthDate=" + birthDate + + '}'; + } } diff --git a/src/main/java/fakes/InjectDemo.java b/src/main/java/fakes/InjectDemo.java new file mode 100644 index 0000000..326fdd2 --- /dev/null +++ b/src/main/java/fakes/InjectDemo.java @@ -0,0 +1,51 @@ +package fakes; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; + +interface CanLog { + void log(String message); +} + +class FileLogger implements CanLog { + @Override + public void log(String message) { + try { + Files.write(Paths.get("src/main/resources/log.txt"), message.getBytes(StandardCharsets.UTF_8)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} + +class ConsoleLogger implements CanLog { + @Override + public void log(String message) { + System.out.println("LOG: " + message); + } +} + +class Logger { + private final CanLog canLog; + + public Logger(CanLog canLog) { + this.canLog = canLog; + } + + public void doLog(String message) { + canLog.log(message); + } +} + +public class InjectDemo { + + public static void main(String[] args) { + final Logger logger = new Logger(new FileLogger()); + logger.doLog("test log 1"); + + final Logger logger2 = new Logger(new ConsoleLogger()); + logger2.doLog("test log 2"); + } +} diff --git a/src/test/java/assertions/PersonTest.java b/src/test/java/assertions/PersonTest.java index ddfacc8..c55d9af 100644 --- a/src/test/java/assertions/PersonTest.java +++ b/src/test/java/assertions/PersonTest.java @@ -1,5 +1,6 @@ package assertions; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.time.LocalDate; @@ -10,30 +11,62 @@ class PersonTest { @Test void getFullNameReturnsFirstnameSpaceLastname(){ - // TODO implement - throw new IllegalArgumentException("you should implement code here"); + String lastname = "Desax"; + String firstname = "Gion"; + Person p = new Person(firstname, lastname, null); + String fullname = firstname + " " + lastname; + Assertions.assertEquals(fullname, p.getFullName()); } - // TODO some more useful tests + @Test + void getFullNameNotReturnsFirstnameSpaceLastname(){ + String lastname = "Desax"; + String firstname = "Gion"; + Person p = new Person(firstname, lastname, null); + + String fullName = lastname + " " + firstname; + Assertions.assertNotEquals(fullName,p.getFullName()); + } + @Test + void getFullNameReturnsFirstCharsInBigLetters(){ + String lastname = "Desax"; + String firstname = "Gion"; + Person p = new Person(firstname, lastname, null); + String[] arrStr = p.getFullName().split(" "); + for(String a : arrStr){ + if (!Character.isUpperCase(a.charAt(0))) { + assert false; + } + } + } // --- getAge @Test - void getAgeReturns10YearsIfBornIn2009() throws Exception { - // TODO verbessern. Hinweis: Repeatable (wiederholbar) zu jeder Zeit. - Person p = new Person("", "", LocalDate.of(2009, 1, 1)); - - throw new IllegalArgumentException("you should implement code here"); + void getAgeReturns10YearsIfBorn10YearsAgo() { + Person p = new Person("", "", LocalDate.now().minusYears(10)); + Assertions.assertEquals(10,p.getAge().getYears()); + Assertions.assertEquals(0,p.getAge().getMonths()); + Assertions.assertEquals(0,p.getAge().getDays()); } @Test - void getAgeReturns1DayIfYesterday() throws Exception { + void getAgeReturns1DayIfYesterday() { Person p = new Person("", "", LocalDate.now().minusDays(1)); - - // TODO implement - throw new IllegalArgumentException("you should implement code here"); + Assertions.assertEquals(0, p.getAge().getYears()); + Assertions.assertEquals(0,p.getAge().getMonths()); + Assertions.assertEquals(1,p.getAge().getDays()); } - // TODO some more useful tests + @Test + void getAgeReturnIsNotTooHigh(){ + LocalDate birthDay = LocalDate.now().plusDays(0); + Person p = new Person("", "", birthDay); + if(p.getAge().isNegative()){ + AssertionError error = new AssertionError("LocalDate birthday " + birthDay + " is set to future"); + System.out.println(error.getMessage()); + assert false; + } + } } diff --git a/src/test/java/fakes/UserControllerTest.java b/src/test/java/fakes/UserControllerTest.java index 714b4c3..cc4b61b 100644 --- a/src/test/java/fakes/UserControllerTest.java +++ b/src/test/java/fakes/UserControllerTest.java @@ -11,9 +11,9 @@ class UserControllerTest { class create { // --- Testing with Fakes --- - @Test - void withValidInexistingUsername_returnsOK__NO_FAKE() { + @org.junit.jupiter.api.Disabled + void withValidInexistingUsername_returnsOK__NO_FAKE_DEMO() { UserController ctrl = new UserController(); User user = new User("kalua"); @@ -36,12 +36,14 @@ void withValidInexistingUsername_returnsOK__MOCKITO() { } @Test - void withValidInexitingUserName_addUserToDB__FAKE() { + void withValidInexitingUsername_addUserToDB__FAKE() { // TODO + // Der Test soll prüfen, ob der Benutzer tatsächlich der DB hinzugefügt wurde. + // Dazu soll ein Database Mock Objekt verwendet werden. } @Test - void withValidInexitingUserName_addUserToDB__MOCKITO() { + void withValidInexitingUsername_addUserToDB__MOCKITO() { // TODO }