Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/main/java/assertions/Person.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package assertions;

import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.Period;

Expand All @@ -19,7 +20,19 @@ public String getFullName(){
return firstName + " " + lastName;
}


public Period getAge(){
return Period.between(birthDate, LocalDate.now());
}
}


public static void main (String[] args){

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Klassen welche nur Daten beinhalten haben keine main Methode. Um die Klasse zu testen haben wir die Unit-Tests. Die Unit-Tests sind quasi unsere Main-Methoden, welche wir laufen lassen. Die Klasse Person verwenden wir dann einfach dort.


try {
Person p1 = new Person ("Hans", "Müller", LocalDate.of(1984,8,4));
System.out.println(p1.getAge());
}
catch (DateTimeException e){
System.out.println("Keine Null erlaubt");
}
}}
42 changes: 26 additions & 16 deletions src/test/java/assertions/PersonTest.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,49 @@
package assertions;

import com.sun.xml.internal.ws.policy.AssertionSet;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.time.LocalDate;
import java.time.Period;

class PersonTest {

// getFullName

@Test
void getFullNameReturnsFirstnameSpaceLastname(){
// TODO implement
throw new NotImplementedException();
}

// TODO some more useful tests

// getAge

Person p = new Person("Hans", "Müller", null);
String fullname = p.getFullName();
Assertions.assertEquals(fullname, "Hans Müller");
}

// TODO verbessern. Hinweis: Repeatable (wiederholbar) zu jeder Zeit.
@Test
void getAgeReturns10YearsIfBornIn2009() throws Exception {
Person p = new Person("", "", LocalDate.of(2009, 1, 1));
void getAgeReturned10Years() throws Exception{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Da fehlt die Bedingung im Methoden/Testnamen. z.B: getAgeReturns10YearsIfBorn10YearsAgo.


throw new NotImplementedException();
Person p1 = new Person ("", "", LocalDate.now().minusYears(10));
Period age = p1.getAge();
Assertions.assertEquals(10, age.getYears());
Assertions.assertEquals(0, age.getMonths());
Assertions.assertEquals(0, age.getDays());
}

@Test
void getAgeReturns1DayIfYesterday() throws Exception {
Person p = new Person("", "", LocalDate.now().minusDays(1));
Period age = p.getAge();
Assertions.assertEquals(0, age.getYears());
Assertions.assertEquals(0, age.getMonths());
Assertions.assertEquals(1, age.getDays());
}

// TODO implement
throw new NotImplementedException();
@Test
void getAgeReturs3Month(){
Person p2 = new Person("", "", LocalDate.now().minusMonths(3));
Period age = p2.getAge();
Assertions.assertEquals(0, age.getYears());
Assertions.assertEquals(3, age.getMonths());
Assertions.assertEquals(0, age.getDays());
}
// TODO some more useful tests
}
}