-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
V2.0 Changes on PersonTest, ValidatorTest, UserControllerTest #13
base: master
Are you sure you want to change the base?
Changes from all commits
7a8d6c2
e0a8b32
465d1d4
a66421d
eff3198
eae426f
8d95b31
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,97 @@ | ||
package assertions; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.LocalDate; | ||
import java.time.Period; | ||
|
||
class PersonTest { | ||
|
||
// getFullName | ||
|
||
@Test | ||
void getFullNameReturnsFirstnameSpaceLastname(){ | ||
void getFullName_ReturnsFirstnameSpaceLastname(){ | ||
// TODO implement | ||
throw new IllegalArgumentException("you should implement code here"); | ||
} | ||
Person p = new Person("Hannes", "Meier", LocalDate.of(1992, 3, 9)); | ||
|
||
String fullName = p.getFullName(); | ||
|
||
// TODO some more useful tests | ||
Assertions.assertEquals(fullName, "Hannes Meier"); | ||
} | ||
|
||
// getAge | ||
@Test | ||
void getAge_Returns10YearsIf10YearsAgo(){ | ||
Person p = new Person("Hannes", "Meier", LocalDate.now().minusYears(10)); | ||
|
||
Period age = p.getAge(); | ||
|
||
Assertions.assertEquals(10, age.getYears()); | ||
Assertions.assertEquals(0, age.getMonths()); | ||
Assertions.assertEquals(0, age.getDays()); | ||
} | ||
|
||
// TODO verbessern. Hinweis: Repeatable (wiederholbar) zu jeder Zeit. | ||
@Test | ||
void getAgeReturns10YearsIfBornIn2009() throws Exception { | ||
Person p = new Person("", "", LocalDate.of(2009, 1, 1)); | ||
void getMonth_Returns1MonthIf1MonthAgo(){ | ||
Person p = new Person("Hannes", "Meier", LocalDate.now().minusMonths(1)); | ||
|
||
throw new IllegalArgumentException("you should implement code here"); | ||
Period age = p.getAge(); | ||
|
||
Assertions.assertEquals(0, age.getYears()); | ||
Assertions.assertEquals(1, age.getMonths()); | ||
Assertions.assertEquals(0, age.getDays()); | ||
} | ||
|
||
|
||
@Test | ||
void getAgeReturns1DayIfYesterday() throws Exception { | ||
Person p = new Person("", "", LocalDate.now().minusDays(1)); | ||
void getAge_Returns1DayIfYesterday(){ | ||
Person p = new Person("Hannes", "Meier", LocalDate.now().minusDays(1)); | ||
|
||
// TODO implement | ||
throw new IllegalArgumentException("you should implement code here"); | ||
Period age = p.getAge(); | ||
|
||
Assertions.assertEquals(0, age.getYears()); | ||
Assertions.assertEquals(0, age.getMonths()); | ||
Assertions.assertEquals(1, age.getDays()); | ||
} | ||
|
||
|
||
// some more tests | ||
@Test | ||
void getName_ReturnsTrueWhenDifferentName(){ | ||
Person p = new Person("1Hannes", "2Meier", LocalDate.of(1992, 3, 9)); | ||
|
||
String fullName = p.getFullName(); | ||
|
||
Assertions.assertFalse(Boolean.parseBoolean(fullName), "Hannes Meier"); | ||
} | ||
|
||
@Test | ||
void getAge_ReturnsTrueWhenDifferentAge(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getAge retourniert eine Zahl, die wird nie True sein. |
||
Person p = new Person("Hannes", "Meier", LocalDate.now().minusYears(11)); | ||
|
||
Period age = p.getAge(); | ||
|
||
Assertions.assertNotEquals(age, 10); | ||
} | ||
|
||
@Test | ||
void Person_TrueIfObjectIsNotNul(){ | ||
Person p = new Person("Hannes", "Meier", LocalDate.now().minusYears(80)); | ||
Assertions.assertNotNull(p); | ||
} | ||
|
||
@Test | ||
void Person_TrueIf2ObjectsAreNotSame(){ | ||
Person p = new Person("Hannes", "Meier", LocalDate.of(1940, 6, 30)); | ||
Person c = new Person("Petra", "Meier", LocalDate.of(1950, 3, 12)); | ||
Assertions.assertNotSame(p, c); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assertNotSame prüft nicht auf Equality, sondern Identity (im Testnamen redest du aber von Equality). |
||
} | ||
|
||
@Test | ||
void Person_TrueIf2ObjectsAreSame(){ | ||
Person p = new Person("Hannes", "Meier", LocalDate.of(1940, 6, 30)); | ||
Person c = p; | ||
Assertions.assertSame(p, c); | ||
} | ||
// TODO some more useful tests | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package fakes; | ||
|
||
public class FakeUserValidator extends UserValidator { | ||
|
||
private boolean isValid; //injection sagt man dem, von aussen bestimmen was passieren soll | ||
|
||
public FakeUserValidator(boolean isValid, boolean isExisting){ | ||
this.isValid=isValid; | ||
this.isExisting=isExisting; | ||
} | ||
|
||
@Override | ||
public boolean isValidUsername(String username){ | ||
return isValid; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,28 +12,52 @@ class create { | |
|
||
// --- Testing with Fakes --- | ||
|
||
@Test | ||
void withValidInexistingUsername_returnsOK__NO_FAKE() { | ||
UserController ctrl = new UserController(); | ||
User user = new User("kalua"); | ||
@Test | ||
void withValidInexistingUsername_returnsOK_NO_FAKE(){ | ||
UserController ctrl = new UserController(); | ||
User user = new User("kalua"); | ||
|
||
Message result = ctrl.create(user); | ||
|
||
Assertions.assertEquals(result.status, Message.Status.OK); | ||
} | ||
|
||
@Test | ||
void withValidInexistingUsername_returnsOK__FAKE() { | ||
// TODO | ||
// 1. Test schneller machen | ||
// 2. UserController.create so beinflussen, | ||
// dass einmal der "if"- und einmal der "else"-Fall durchlaufen wird | ||
} | ||
@Test | ||
void withValidInexistingUsername_returnsOK_FAKE(){ | ||
UserController userController = new UserController(); | ||
|
||
@Test | ||
void withValidInexistingUsername_returnsOK__MOCKITO() { | ||
// TODO | ||
} | ||
} | ||
|
||
/* @Test | ||
// I DON'T UNDERSTAND THIS ONE :@ | ||
void MOCKITO_FAKE_withValidInexistingUsername_returnsOK() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { | ||
// TODO | ||
User user1= new User("Kalua"); | ||
User user2= new User("Hannes"); | ||
|
||
UserController test1 = new UserController(); | ||
UserController test2 = new UserController(); | ||
|
||
Message message1 = new Message(); | ||
Method privateMessage = Message.class.getDeclaredMethod("Message", String.class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Von welcher PowerPoint Folie oder Übung hast du denn das her :)? |
||
privateMessage.setAccessible(true); | ||
String returnValue = (String) privateMessage.invoke(message1, null); | ||
|
||
|
||
db.addUser(user1); // user1 bereits in die db rein | ||
|
||
//durchläuft else | ||
test1.create(user1); | ||
//durchläuft if | ||
test2.create(user2); | ||
|
||
//Assertions.assertTrue(!(!UserValidator.doesUsernameExist(user1.getUsername()) Message.createOK())); | ||
Assertions.assertEquals(message1, "OK"); | ||
// 1. Test schneller machen | ||
// 2. UserController.create so beinflussen, | ||
// dass einmal der "if"- und einmal der "else"-Fall durchlaufen wird | ||
} | ||
*/ | ||
|
||
@Test | ||
void withValidInexitingUserName_addUserToDB__FAKE() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,87 @@ | ||
package fakes; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
|
||
class UserValidatorTest { | ||
|
||
@Nested | ||
class isValidUsername{ | ||
|
||
@Test | ||
void returnsTrueIfOnlyLetters(){ | ||
throw new IllegalArgumentException("you should implement code here"); | ||
User user1 = new User("kalua"); | ||
User user2 = new User ("Hannes"); | ||
User user3 = new User("123Hannes123"); | ||
User user4 = new User("!!@"); | ||
String person1; | ||
String person2; | ||
String person3; | ||
String person4; | ||
person1 = user1.getUsername(); | ||
person2 = user2.getUsername(); | ||
person3 = user3.getUsername(); | ||
person4 = user4.getUsername(); | ||
Assertions.assertTrue(isAlpha(person1)); | ||
Assertions.assertTrue(isAlpha(person2)); | ||
Assertions.assertFalse(isAlpha(person3)); | ||
Assertions.assertFalse(isAlpha(person4)); | ||
} | ||
//Test only Letters | ||
public boolean isAlpha(String name){ | ||
char[] chars = name.toCharArray(); | ||
|
||
for (char c : chars){ | ||
if(!Character.isLetter(c)){ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
@Test | ||
void returnsFalseIfStartsWithNumber(){ | ||
throw new IllegalArgumentException("you should implement code here"); | ||
|
||
} | ||
|
||
@Test | ||
void returnsTrueIfContainsNumberButNotAsFirstChar(){ | ||
throw new IllegalArgumentException("you should implement code here"); | ||
|
||
} | ||
|
||
@Test | ||
void returnsFalseIfContainsAnyNonAlphanumericChar(){ | ||
throw new IllegalArgumentException("you should implement code here"); | ||
} | ||
} | ||
|
||
static class doesUsernameExist{ | ||
//DB used for the test | ||
private static Database db = FileDatabase.getInstance(); | ||
|
||
@Test | ||
void returnsFalseIfUsernameNotInDBYet(){ | ||
throw new IllegalArgumentException("you should implement code here"); | ||
} | ||
void returnsTrueIfUsernameNotInDBYet(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unten prüfst du auf "assertTrue(!UserValidator.doesUsernameExist())", das ist ja genau das Gegenteil von "returnsTrue". |
||
User user1= new User("Kalua"); | ||
User user2= new User("Hannes"); | ||
|
||
@Test | ||
void returnsTrueIfUsernameInDB(){ | ||
throw new IllegalArgumentException("you should implement code here"); | ||
db.addUser(user1); | ||
|
||
Assertions.assertTrue(UserValidator.isValidUsername(user2.getUsername()) && !UserValidator.doesUsernameExist(user2.getUsername())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Die inner static class heisst "doesUsernameExist", es ist also nicht nötig isValidUsername hier überhaupt aufzurufen, da du ja doesUsernameExist testen willst. Generell kann man sagen, dass es nicht empfohlen wird logische Verknüpfungen zu testen (ausser natürlich im Test geht es genau um darum ;)). |
||
//Also testing the opposite with user1 | ||
Assertions.assertTrue(UserValidator.isValidUsername(user1.getUsername()) && UserValidator.doesUsernameExist(user1.getUsername())); | ||
} | ||
|
||
@Test | ||
void returnsTrueIfSameNameInDBButWithDifferentLetterCasing(){ | ||
throw new IllegalArgumentException("you should implement code here"); | ||
void returnsFalseIfUsernameInDB(){ | ||
User user1= new User("Kalua"); | ||
User user2= new User("Kalue"); | ||
|
||
db.addUser(user1); | ||
|
||
Assertions.assertTrue(UserValidator.isValidUsername(user2.getUsername()) && !UserValidator.doesUsernameExist(user2.getUsername())); | ||
//False if in DB | ||
Assertions.assertFalse(!UserValidator.doesUsernameExist(user1.getUsername())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wie im obigen Test reicht dieses zweite assert aus, da es genau das ist, was du im Testnamen angibst zu testen. |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wieso machst du
parseBoolean("1Hannes 2Meier")
?Das zweite Argument "Hannes Meier" ist ja nur die Message welche angezeigt wird, wenn die Assertion fehlschlägt.