Skip to content
Binary file modified .gitignore
Binary file not shown.
9 changes: 9 additions & 0 deletions src/main/java/assertions/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
'}';
}
}
51 changes: 51 additions & 0 deletions src/main/java/fakes/InjectDemo.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
59 changes: 46 additions & 13 deletions src/test/java/assertions/PersonTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package assertions;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
Expand All @@ -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;
Comment thread
jhonnyd76 marked this conversation as resolved.
}
}
}
10 changes: 6 additions & 4 deletions src/test/java/fakes/UserControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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
}

Expand Down