diff --git a/src/main/java/assertions/CMain.java b/src/main/java/assertions/CMain.java new file mode 100644 index 0000000..5065329 --- /dev/null +++ b/src/main/java/assertions/CMain.java @@ -0,0 +1,32 @@ +package assertions; + +/** + * @author - John Schmidt + * 23.11.2021, 00:42 + */ +public class CMain { + + public static void voidMethode(){ + System.out.println("ich bin VoidMethode von Main"); + } + + public static void voidMethodeMitInt(int f){ + System.out.println("ich bin VoidMethode von Main mit int f"); + } + + + public static void main(String[] args) { + + + Calculator c1 = new Calculator(); + System.out.println(c1.add1(2, 3)); + + // System.out.println( c1.addVoid(99,88) );//nicht systemprintln... + //System.out.println(); + // voidMethode(); + CMain.voidMethode(); + CMain.voidMethodeMitInt(1); + c1.addVoid(99,88); + + } +} diff --git a/src/main/java/assertions/Calculator.java b/src/main/java/assertions/Calculator.java new file mode 100644 index 0000000..cfeabe8 --- /dev/null +++ b/src/main/java/assertions/Calculator.java @@ -0,0 +1,31 @@ +package assertions; + +/** + * @author - John Schmidt + * 23.11.2021, 00:39 + */ +public class Calculator { + CalculatorService service; //egal... muss man nicht verstehen + + + public Calculator(CalculatorService service){ + this.service=service; + } +//weil ez, darum leerer Construktor... + public Calculator(){} + + + public int perform(int i, int j){ + return service.add(i,j); + } + + + public int add1(int i, int j){ + return i+j+1;//+1 + } + + public void addVoid(int i, int j){ + System.out.println("irgendwas void zeugs"); + } + +} diff --git a/src/main/java/assertions/CalculatorService.java b/src/main/java/assertions/CalculatorService.java new file mode 100644 index 0000000..62faa00 --- /dev/null +++ b/src/main/java/assertions/CalculatorService.java @@ -0,0 +1,11 @@ +package assertions; + +/** + * @author - John Schmidt + * 23.11.2021, 00:39 + */ +public interface CalculatorService { + public int add(int i, int j); + + public void addVoid(int i, int j); +} diff --git a/src/main/java/assertions/PMain.java b/src/main/java/assertions/PMain.java new file mode 100644 index 0000000..785c5ac --- /dev/null +++ b/src/main/java/assertions/PMain.java @@ -0,0 +1,27 @@ +package assertions; + +import java.time.LocalDate; + +/** + * @author - John Schmidt + * 08.11.2021, 00:40 + */ +public class PMain { + public static void main(String[] args) { + + Person p1 = new Person("Hans","Muster",null); + Person p2 = new Person("Frau","Musterfrau", LocalDate.of(2005,11,28)); + Person p3 = new Person("Hans","Muster",LocalDate.now()); + //Person p4 = new Person("Hans","Muster",(LocalDate.now()-(LocalDate.of(2021,10,14)))); + Person p5 = new Person("Frau","Musterfrau", LocalDate.of(2021,10,14)); + //getAgeReturns10YearsIfBornIn2009() //2021-10=2011 + + + //System.out.println("p1: "+p1.getAge()); + System.out.println("p2: "+p2.getAge()); + System.out.println("p3: "+p3.getAge()); + + + + } +} diff --git a/src/main/java/fakes/UMain.java b/src/main/java/fakes/UMain.java new file mode 100644 index 0000000..27f8467 --- /dev/null +++ b/src/main/java/fakes/UMain.java @@ -0,0 +1,20 @@ +package fakes; + +/** + * @author - John Schmidt + * 08.11.2021, 00:41 + */ +public class UMain { + public static void main (String[] args){ + + User u1 = new User("Hansi"); + + + UserValidator uv1 = new UserValidator(); + uv1.doesUsernameExist("peter"); + //System.out.println("abcd "+UserValidator.isValidUsername("m9mk")); + + + } + +} diff --git a/src/main/java/fakes/User.java b/src/main/java/fakes/User.java index 69c0db5..99bcb19 100644 --- a/src/main/java/fakes/User.java +++ b/src/main/java/fakes/User.java @@ -9,6 +9,8 @@ public User(String username){ } public String getUsername() { + + System.out.println("User getUserName: Hello Kitty"+username); return username; } } diff --git a/src/main/java/fakes/UserController.java b/src/main/java/fakes/UserController.java index d61b7e8..2fe89cc 100644 --- a/src/main/java/fakes/UserController.java +++ b/src/main/java/fakes/UserController.java @@ -4,17 +4,53 @@ public class UserController { private static Database db = FileDatabase.getInstance(); + private UserValidator userValidator; //fakeuservalidator //aus Unterricht + private Database database= FileDatabase.getInstance();; //MockDatabase jedoch MockDatabase db geht nicht //aus Unterricht + + public UserController(UserValidator UserValidator){//inject db.adduser + userValidator = UserValidator; + } //aus Unterricht + public UserController(UserValidator UserValidator, Database database){//inject db.adduser //aus fake + userValidator = UserValidator; + this.database = database; //Mok-database + } + public Message create(User user){ if(user == null){ throw new IllegalArgumentException("user required"); } - Boolean canCreate = UserValidator.isValidUsername(user.getUsername()) - && !UserValidator.doesUsernameExist(user.getUsername()); + Boolean canCreate = false;//userValidator.isValidUsername(user.getUsername()) //aus Unterricht //static bei Uservalidator weg, dann hier Uservalidator klein , weil hier oberhalb konstruktor eine instanz reinkommt irgendwas hier rein komt + //&& !userValidator.doesUsernameExist(user.getUsername()); //aus Unterricht// nur mal für erste test if(canCreate){ - db.addUser(user); + db.addUser(user);//db + database.addUser(user); return Message.createOK(); }else{ return Message.createNotOK(); } } } + + + +// +//package fakes; +// +//public class UserController { +// +// private static Database db = FileDatabase.getInstance(); +// +// public Message create(User user){ +// if(user == null){ +// throw new IllegalArgumentException("user required"); +// } +// Boolean canCreate = UserValidator.isValidUsername(user.getUsername()) +// && !UserValidator.doesUsernameExist(user.getUsername()); +// if(canCreate){ +// db.addUser(user); +// return Message.createOK(); +// }else{ +// return Message.createNotOK(); +// } +// } +//} diff --git a/src/main/java/fakes/UserValidator.java b/src/main/java/fakes/UserValidator.java index 0bd28b2..06925ea 100644 --- a/src/main/java/fakes/UserValidator.java +++ b/src/main/java/fakes/UserValidator.java @@ -1,25 +1,66 @@ package fakes; +import java.util.regex.Pattern; + public class UserValidator { - private static Database db = FileDatabase.getInstance(); + private static Database db = FileDatabase.getInstance();//static löschen - public static boolean doesUsernameExist(String username){ - try { - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } + //konstruktor für fake + public UserValidator(Database db){ + this.db = db; + } + + //default konstruktor + public UserValidator(){} + + // MockDatabase database; //mockdatabase versuch eigener test..., schule bei usercontroller + //fakeUservalidator hat gleiche methode, mit override + public boolean doesUsernameExist(String username) { //static wird im Untericht gelöscht, dann hier auch static löschen +// try { +// Thread.sleep(2000);//5000 +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } - for(User user : db.getUsers()){ - if (user.getUsername().equals(username)){ +// int zaehler = 0; + for (User user : db.getUsers()) { //Mock aus db wird database + // zaehler++; + if (user.getUsername().equals(username)) { + System.out.println("Bitte was? "+user.getUsername().equals(username)); + System.out.println("Bitte was222 "+user.getUsername()); return true; } } + // System.out.println("Zaehler von doesUserNameExist: "+zaehler); return false; } - public static boolean isValidUsername(String username){ - return true; + + public static boolean isValidUsername(String username) { //static im Unterricht gelöscht, wegen FakeUserValidator + //wenn nur buchstabe true // methode für Tests verändert + //falsch wenn startet mit nummer + //wenn zahl aber nicht erste + //falsch wenn Sonderzeichen + boolean booleanIVU = false; + String str = username; + //System.out.println("str Methode: " + str); + + if (str.length() == 0) { + booleanIVU = false; + //System.out.println("leer"); + } else if (str.length() == 1 && Pattern.matches("[a-zA-Z]+", str)) {//ifOnlyLetters + booleanIVU = true; + //System.out.println("1 "); + } else if (str.length() == 2 && (Pattern.matches("[a-zA-Z]+", str.substring(0, 1))) && Pattern.matches("[a-zA-Z0-9 ]+", str.substring(1, 2))) {//&&str.substring(1, 2).equals("a")) {//Pattern.matches("[a-zA-Z0-9 ]+", str) + booleanIVU = true; + //System.out.println("2 "); + } else if (str.length() > 2 && (Pattern.matches("[a-zA-Z]+", str.substring(0, 1))) && Pattern.matches("[a-zA-Z0-9 ]+", str.substring(1, str.length()))) { + booleanIVU = true; + //System.out.println("3 "); + } + + return booleanIVU; } + } diff --git a/src/test/java/assertions/CalculatorTest.java b/src/test/java/assertions/CalculatorTest.java new file mode 100644 index 0000000..8890da8 --- /dev/null +++ b/src/test/java/assertions/CalculatorTest.java @@ -0,0 +1,97 @@ +package assertions; + +import fakes.User; +import org.junit.*; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.util.Arrays; +import java.util.Collections; + +/** + * @author - John Schmidt + * 23.11.2021, 00:40 + */ +public class CalculatorTest { + + @Test + public void add1Test(){ + Calculator c1 = new Calculator(); + Assertions.assertEquals(5,c1.add1(2,3));//+1... + } + + +// CalculatorService service1 = new CalculatorService() { mach es automatisch, warum auch immer +// @Override +// public int add(int i, int j) { +// return 0; +// } +// } + + CalculatorService service1 = Mockito.mock(CalculatorService.class); //egal... muss man nicht verstehen + + @Test + public void performTest(){ + Calculator c1 = new Calculator(service1);//mit service1, programm sagt create a constructor, klar weil ruft ja konstruktor auf + //Assertions.assertEquals(5,service.add1(2,3)); +// Assertions.assertEquals(5,c1.perform(2,3));//ohne service1, sagt irgend ein Objekt ist null + + //jetzt alles rot, weil möchte 5, aber gebe 0,0, da isch eher verständlich das da passiert + // damit jetzt 2 + 3 reinkommen: + //Mockito.doNothing() + //Mockito.when(service1.add(2,3)) + Mockito.when(service1.add(1,4)).thenReturn(5); + Assertions.assertEquals(5,c1.perform(1,4)); + } + + @Test + public void performTestzumspielen(){ + Calculator c1 = new Calculator(service1);//mit service1, programm sagt create a constructor, klar weil ruft ja konstruktor auf + //Assertions.assertEquals(5,service.add1(2,3)); +// Assertions.assertEquals(5,c1.perform(2,3));//ohne service1, sagt irgend ein Objekt ist null + + //jetzt alles rot, weil möchte 5, aber gebe 0,0, da isch eher verständlich das da passiert + // damit jetzt 2 + 3 reinkommen: + //Mockito.doNothing() + //Mockito.when(service1.add(2,3)) + Mockito.when(service1.add(1,4)).thenReturn(5); + // System.out.println(Mockito.doReturn(Arrays.asList(new User("peter"))).when(mokitoDb).getUsers());//github + // System.out.println("uu "+ Mockito.doReturn(Collections.emptyList()).when(mokitoDb).getUsers() );//github + System.out.println("?? "+Mockito.when(service1.add(1,4)).thenReturn(5)); + + //System.out.println("?? "+Mockito.when(service1.add(1,4)).thenReturn(5)); + + //Assertions.assertEquals(5,c1.perform(1,4)); + } + + @Test + public void addVoidTest(){ + Calculator c1 = new Calculator(service1);//mit service1, programm sagt create a constructor, klar weil ruft ja konstruktor auf + //Assertions.assertEquals(5,service.add1(2,3)); +// Assertions.assertEquals(5,c1.perform(2,3));//ohne service1, sagt irgend ein Objekt ist null + + //jetzt alles rot, weil möchte 5, aber gebe 0,0, da isch eher verständlich das da passiert + // damit jetzt 2 + 3 reinkommen: + //Mockito.doNothing() + //Mockito.when(service1.add(2,3)) +// Mockito.when(service1.add(1,4)).thenReturn(5); +// Assertions.assertEquals(5,c1.perform(1,4)); + // Mockito.when(service1.addVoid(2,3);)//au da git strichpuktund klammer + // Mockito.when(service1.add(2,3))//gibt kein strichpunkt Klammer + // Mockito.when(service1.addVoid(2,3));geht nicht + + // System.out.println(Mockito.doAnswer(service1.addVoid(2,4)); + + + // System.out.println(Mockito.doReturn(Arrays.asList(new User("peter"))).when(mokitoDb).getUsers());//github + // System.out.println("uu "+ Mockito.doReturn(Collections.emptyList()).when(mokitoDb).getUsers() );//github + +// int x = 5; +// System.out.println("?? "+Mockito.when(Arrays.asList(service1.add(1,4))).thenReturn(x)); + + } + + +} diff --git a/src/test/java/assertions/PersonTest.java b/src/test/java/assertions/PersonTest.java index ddfacc8..d0235a9 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; @@ -11,7 +12,9 @@ class PersonTest { @Test void getFullNameReturnsFirstnameSpaceLastname(){ // TODO implement - throw new IllegalArgumentException("you should implement code here"); + Person p1 = new Person("Peter", "Mustermannomann",null); + Assertions.assertEquals(p1.getFullName(), "Peter Mustermannomann"); + //throw new IllegalArgumentException("you should implement code here"); } // TODO some more useful tests @@ -22,17 +25,38 @@ void getFullNameReturnsFirstnameSpaceLastname(){ @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"); + //Person p = new Person("", "", LocalDate.of(2009, 1, 1)); + Person p = new Person("", "", LocalDate.now());//.of(2011, 1, 1)); + + //int year = LocalDate.of(2011,5,30).getYear(); + int yearNow = LocalDate.now().getYear(); + System.out.println("yearNow: "+yearNow); + int yearMinusTen = LocalDate.now().getYear()-10; + System.out.println("yearMinusTen: "+yearMinusTen); + int ten = yearNow-yearMinusTen; + System.out.println("ten: "+ten); + Assertions.assertEquals(ten,10); + //throw new IllegalArgumentException("you should implement code here"); } @Test void getAgeReturns1DayIfYesterday() throws Exception { - Person p = new Person("", "", LocalDate.now().minusDays(1)); + //Person p = new Person("", "", LocalDate.now().minusDays(1)); // TODO implement - throw new IllegalArgumentException("you should implement code here"); + Person p = new Person("", "", LocalDate.now().minusDays(1)); + + System.out.println("MinusDay(1)...: "+LocalDate.now().minusDays(1)); + int dayNow = LocalDate.now().getDayOfMonth(); //-LocalDate.now().minusDays(1); + System.out.println("dayNow: "+dayNow); + int dayMinusOne = LocalDate.now().getDayOfMonth()-1; + System.out.println("dayMinusOne "+dayMinusOne); + int one = dayNow-dayMinusOne; + System.out.println("one: "+one); + + Assertions.assertEquals(one,1); + + //throw new IllegalArgumentException("you should implement code here"); } // TODO some more useful tests diff --git a/src/test/java/fakes/FakeDatabase.java b/src/test/java/fakes/FakeDatabase.java new file mode 100644 index 0000000..51cb66e --- /dev/null +++ b/src/test/java/fakes/FakeDatabase.java @@ -0,0 +1,39 @@ +package fakes; + +//import jdk.internal.icu.text.UnicodeSet; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author - John Schmidt + * 19.11.2021, 13:39 + */ +public class FakeDatabase extends Database{ + //private UnicodeSet users;//extends + + private List users = new ArrayList<>(); + +// public void ausgebenFakeDBLaenge(){ +// System.out.println("Länge der FakeDB.size: "+users.size()); +// } + + @Override + public void addUser(User user){ + //mockList.add(user); + users.add(user); + System.out.println("addUser getUserName: "+user.getUsername()); + System.out.println("Länge der addUser FakeDB.size: "+users.size()); + } + + @Override + public List getUsers(){ + //return mockList; +// for (int i = 0; i< users.size();i++){ +// +// System.out.println(users); +// } + System.out.println("Länge der getUser FakeDB.size: "+users.size()); + return users; + } +} diff --git a/src/test/java/fakes/FakeUserValidator.java b/src/test/java/fakes/FakeUserValidator.java new file mode 100644 index 0000000..b94f1a3 --- /dev/null +++ b/src/test/java/fakes/FakeUserValidator.java @@ -0,0 +1,27 @@ +package fakes; + +/** + * @author - John Schmidt + * 12.11.2021, 21:42 + */ +public class FakeUserValidator extends UserValidator{ //Stand von Schule + private boolean userExist; + //private String userExist; + + public FakeUserValidator(boolean b) { + this.userExist=userExist; + } + + public FakeUserValidator() {} + + + @Override + public boolean doesUsernameExist(String username){ +//FalseIfUsernameNotInDBYet, dann wo db? + + + return username.equals("kalua"); + } + + +} diff --git a/src/test/java/fakes/MockDatabase.java b/src/test/java/fakes/MockDatabase.java new file mode 100644 index 0000000..84885d4 --- /dev/null +++ b/src/test/java/fakes/MockDatabase.java @@ -0,0 +1,26 @@ +package fakes; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author - John Schmidt + * 12.11.2021, 19:20 + */ +public class MockDatabase extends Database{ //Name Mock okay; weil extends, hat addUser + getUser, weil original database ist abstract + + //List mockList = new ArrayList<>(); //irgendwie mit Zeit kann man sich vorstellen, warum man diese Liste mit Objekten braucht, bzw, wahrscheinlich, dasmit man es auch bei anderen sachen benutzen kann... + List users = new ArrayList<>(); //damit stand von Schule... + @Override + public void addUser(User user){ + //mockList.add(user); + users.add(user); + } + + @Override + public List getUsers(){ + //return mockList; + return users; + } + +} diff --git a/src/test/java/fakes/UserControllerTest.java b/src/test/java/fakes/UserControllerTest.java index 4c943af..c80ce56 100644 --- a/src/test/java/fakes/UserControllerTest.java +++ b/src/test/java/fakes/UserControllerTest.java @@ -15,9 +15,17 @@ class create { @Test @org.junit.jupiter.api.Disabled void withValidInexistingUsername_returnsOK__NO_FAKE_DEMO() { +// UserController ctrl = new UserController(new FakeUserValidator()); Letzter Stand Schule vor update +// User user = new User("kalua");//kalua +// +// Message result = ctrl.create(user); +// +// Assertions.assertEquals(result.status, Message.Status.OK); + + // Arrange - UserController ctrl = new UserController(); - User user = new User("kalua"); + UserController ctrl = new UserController(new FakeUserValidator()); + User user = new User("peter");//kalua // Act Message result = ctrl.create(user); @@ -41,11 +49,46 @@ void withValidInexistentUsername_returnsOK__FAKE() { // 3. Assert: Rückgabewert von UserController.create prüfen // TODO implement test + UserController uc1 = new UserController(new FakeUserValidator());//false + User u11 = new User("hanso");//peter + + Message result = uc1.create(u11); + + Assertions.assertEquals(result.status, Message.Status.OK); } @Test - void withValidInexistentUsername_returnsOK__MOCKITO() { + void withValidInexistentUsername_returnsOK__MOCKITO() {//mokito testen validaor, nicht controller // TODO implement test +// Database mockDatabase = mock(Database.class); +// mockDatabase.addUser(new User("Hand")); +// System.out.println(mockDatabase.getUsers()); + + //MockDatabase md1= new MockDatabase();//findet mockdatabase nicht, muss er finden, oder override + + + + + + + + Database mockDatabase = new MockDatabase(); + mockDatabase.addUser(new User("Hand")); + System.out.println("irgendwas: "+mockDatabase.getUsers()); + + //UserController ctrl = new UserController(new FakeUserValidator(false), md1); + + UserController uc1 = new UserController(new FakeUserValidator()); + + User user1 = new User("peter");//kalua + uc1.create(user1); + System.out.println(); + + + Message result = uc1.create(user1); + System.out.println("result: "+result); + Assertions.assertEquals(result.status, Message.Status.OK); + } @Test @@ -56,6 +99,23 @@ void withValidInexistentUsername_shouldAddUserToDB__FAKE() { // TODO implement test // Tipp: Wie kann dein Test feststellen, ob der UserController der Datenbank einen Benutzer hinzugefügt hat? // Welche Art von Fake (Stub oder Mock) kann dir weiterhelfen? +// +// final MockDatabase database = new MockDatabase(); +// UserController ctrl = new UserController(new FakeUserValidator(false), database); +// User user = new User("peter");//kalua +// Message result = ctrl.create(user); +// +// Assertions.assertEquals(result.status, Message.Status.OK); +// boolean doesUserExist = false; +// for(User u : database.getUsers()){ +// if(u.getUsername().equals(("Peter"))) { +// doesUserExist = true; +// } +// } +// Assertions.assertTrue(doesUserExist); + //mit getuser + + } @Test @@ -71,21 +131,21 @@ void withValidInexistentUsername_shouldAddUserToDB__MOCKITO() { // --- Testing Exceptions --- @Test - void withNullUser_throwsIllegalArgumentExc__TRY_CATCH() { + void withNullUser_throwsIllegalArgumentExc__TRY_CATCH() {//test von Folie try { - UserController ctrl = new UserController(); + UserController ctrl = new UserController(new FakeUserValidator()); ctrl.create(null); Assertions.fail("No IllegalArgumentException was thrown"); - } catch (IllegalArgumentException ex) { + } catch (IllegalArgumentException ex) {//ist in usercontroller implementiert // Optional: Test message Assertions.assertEquals("user required", ex.getMessage()); } } @Test - void withNullUser_throwsIllegalArgumentException__THROWN() { + void withNullUser_throwsIllegalArgumentException__THROWN() {//braucht kein try catch, kein boolean Assertions.assertThrows(IllegalArgumentException.class, () -> { - UserController ctrl = new UserController(); + UserController ctrl = new UserController(new FakeUserValidator()); ctrl.create(null); }); } @@ -93,10 +153,10 @@ void withNullUser_throwsIllegalArgumentException__THROWN() { @Test void withNullUser_throwsIllegalArgumentExceptionWithMessage__THROWN_MESSAGE() { Exception thrown = Assertions.assertThrows(IllegalArgumentException.class, () -> { - UserController ctrl = new UserController(); + UserController ctrl = new UserController(new FakeUserValidator()); ctrl.create(null); }); - Assertions.assertTrue(thrown.getMessage().contains("required")); + Assertions.assertTrue(thrown.getMessage().contains("required"));//wie oben, aber mit rückgabewert } } } diff --git a/src/test/java/fakes/UserValidatorTest.java b/src/test/java/fakes/UserValidatorTest.java index be3486d..7aff829 100644 --- a/src/test/java/fakes/UserValidatorTest.java +++ b/src/test/java/fakes/UserValidatorTest.java @@ -4,73 +4,328 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -class UserValidatorTest { +import static org.mockito.Matchers.any; +import static org.mockito.junit.MockitoJUnit.*; +//import static org.mockito.*; +import static org.mockito.Mockito.*; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoJUnitRunner.*; +import org.mockito.junit.*; +import org.w3c.dom.ls.LSOutput; + +import java.util.*; + +class UserValidatorTest {//methode die zu testen ist von der klasse /** * Alle Tests in der folgenden Klasse isValidUsername sollen die Methode UserValidator.isValidUsername testen. * Damit alle Tests grün werden, musst du die Implementation von UserValidator.isValidUsername anpassen! */ - @Nested + @Nested//block von test cases class isValidUsername { @Test void returnsTrueIfOnlyLetters() { // TODO Testcode anpassen, damit er das testet was der Testname sagt. - Assertions.assertTrue(new UserValidator().isValidUsername(null)); + //Assertions.assertTrue(new UserValidator().isValidUsername(null)); + //Assertions.assertTrue(new UserValidator().isValidUsername("null"));//dann gut, obwohl das b methode noch leer ist + UserValidator validator = new UserValidator(); //Stub + Assertions.assertTrue(validator.isValidUsername("Heidi"),"returnsTrueIfOnlyLetters"); + //throw new IllegalArgumentException("you should implement code here"); } @Test void returnsFalseIfStartsWithNumber(){ // TODO implement test + UserValidator validator = new UserValidator(); //Stub + //Assertions.assertFalse(new UserValidator().isValidUsername("5haxxorr")); + Assertions.assertFalse(validator.isValidUsername("3Heidi"),"returnsFalseIfStartsWithNumber"); + //throw new IllegalArgumentException("you should implement code here"); } @Test void returnsTrueIfContainsNumberButNotAsFirstChar() { // TODO implement test + UserValidator validator = new UserValidator(); //Stub + Assertions.assertTrue(validator.isValidUsername("H8eidi"),"returnsTrueIfContainsNumberButNotAsFirstChar"); + //throw new IllegalArgumentException("you should implement code here"); } @Test void returnsFalseIfContainsAnyNonAlphanumericChar() { // TODO implement test + UserValidator validator = new UserValidator(); //Stub + Assertions.assertFalse(false,"Hei!@#&()–[{}]:;'`,?/*~$^+=<>“di"); + //throw new IllegalArgumentException("you should implement code here"); } } - +//zb noch test, wenn null ist /** * Alle Tests in der folgenden Klasse doesUsernameExist sollen die Methode UserValidator.doesUsernameExist testen. * Damit alle Tests grün werden, musst du die Implementation von UserValidator.doesUsernameExist anpassen! */ static class doesUsernameExist { @Test - void returnsFalseIfUsernameNotInDBYet__FAKE() { + void returnsFalseIfUsernameNotInDBYet__FAKE() {//testen mit fake klasse // TODO implementiere / ergänze den Test hier, so dass dieser kompiliert und grün ist. + //falls user noch nicht in db ist + //abhänigkeit in methode: ist der db + //sowas muss mann faken oder mock oder stub + //abhängigkeit faken, nicht uservalidatorfake + //2. db, konstruktor und default konstruktor + Database db = new FakeDatabase();//fakedatabase erstellen + final UserValidator uv = new UserValidator(db); //1.3. fakedb übergeben + //2. neuer konstruktor machen, um die db zu übergeben + boolean usernameExist = uv.doesUsernameExist("peter"); //weil noch kein peter, darum gut + Assertions.assertFalse(usernameExist); - // boolean usernameExist = uv.doesUsernameExist("peter"); - // Assertions.assertFalse(usernameExist); + +// //return falsch, wenn name noch nicht in db ist +// //retur falsch, wenn name schon in db ist --> +// //UserValidator uv = new UserValidator(); +// +// boolean usernameExist = uv.doesUsernameExist("eter");//eter statt peter geht auch//für lesbarkeit schreibt mann boolean result, assert result +// System.out.println("Boolean usernameExist: "+usernameExist); +// Assertions.assertFalse(usernameExist); + //muss Assertion.assertFalse sein } - @Test + + @Test//falls user noch nicht in db ist// (expected = IndexOutOfBoundsException.class) void returnsFalseIfUsernameNotInDBYet__MOCKITO() { // TODO implement test + + //vorteil nicht mehr selber klassen machen, sondern generieren lassen + Database mokitoDb = Mockito.mock(Database.class);// erstellen mit mokito, also eingeben mockitopunktmock (punktnotation) + //static import denn nur mock.(Database.class) //von der Database-Klasse + //Database mokitoDb = mock(Database.class); + // Mockito.doReturn(Collections.emptyList()).when(db).getUsers(); + //Mockito.doReturn()... + final UserValidator uv = new UserValidator(mokitoDb);//uservalidator soll Mokitodatenbank nutzen //ev am schluss final machern + System.out.println("wer schon in MokitoDB drin?: "+mokitoDb.getUsers()); + + User u1 = new User("Anton"); //User erstellen + User u2 = new User("Antonella"); //User erstellen + User u3 = new User("abcde"); //User erstellen + User u4 = new User("xyz"); + System.out.println("u1 name: "+u1.getUsername()); //name ausgeben + System.out.println("u2 name: "+u2.getUsername()); + System.out.println("u3 name: "+u3.getUsername()); + System.out.println("u43 name: "+u4.getUsername()); + mokitoDb.addUser(u1); //Mokitodb user hinzufügen//ist immer leer + mokitoDb.addUser(u2); //Mokitodb user hinzufügen + mokitoDb.addUser(u3); + //mokitoDb.addUser(u4); + + // Arrays.asList(u4); + System.out.println("?Arrays.asList?"+Arrays.asList(u4.getUsername())); + System.out.println("?Arrays.asList?"+Arrays.asList(mokitoDb)); + + + //----------------ist void-der addUser von Database------------------------ +// Mockito.when(mokitoDb.addUser(new User("Ueli"))).thenReturn(uv.doesUsernameExist("Ueli")); +//// String str = ""; +//// Mockito.when(mokitoDb.addUser(new User("Ueli"))).thenReturn(str = "Hallo"); +// Mockito.when(mokitoDb.addUser(new User("Ueli"))).thenAnswer(true); +// Mockito.when(mokitoDb.addUser(new User("Ueli"))); +// Mockito.doNothing(); +// System.out.println(Mockito.doReturn(Arrays.asList(new User("peter"))).when(mokitoDb).getUsers());//github + //--------------------------------------------------------------------------------------- + // Mockito.when(service1.add(1,4)).thenReturn(5); + // Mockito.when(mokitoDb.addUser(u4)).thenReturn(u1); + //----------------------------------------------------------------------------------- + System.out.println(Mockito.doReturn(Arrays.asList(new User("peter"))).when(mokitoDb).getUsers());//github + System.out.println("uu "+ Mockito.doReturn(Collections.emptyList()).when(mokitoDb).getUsers() );//github + + + + // System.out.println("?Arrays.asList?"+mokitoDb.addUser(Arrays.asList(u4))); + //System.out.println("dddd "+Mockito.doReturn(Arrays.asList(u1))) + // System.out.println("dddd "+mokitoDb.addUser(Mockito.doReturn(Arrays.asList(u1))) ); + // System.out.println("dddd "+mokitoDb.addUser(Arrays.asList(u1)) ); + // System.out.println("uu "+ Mockito.doReturn(Collections.emptyList()).when(mokitoDb).getUsers() ); + + + //System.out.println("wwww "+Mockito.when(uv.doesUsernameExist("Hansoloo")).thenReturn(false)); + // System.out.println("wwww "+Mockito.when(uv..doesUsernameExist("Hansoloo")).thenReturn(false)); + // boolean antonboolean = uv.doesUsernameExist("Anton"); + // System.out.println("antonboolean "+antonboolean); + +// User user = new User("Martin"); +// //Mockito Returnwert setzen +// Mockito.doReturn(true).when(uv).doesUsernameExist("Martin"); +// boolean Martinboolean = uv.doesUsernameExist("Martin"); +// System.out.println("Martinboolean "+Martinboolean); + + System.out.println("mokitoDb.getUsers"+mokitoDb.getUsers());//ist leer + //System.out.println(Mockito.spy(db.getUsers()));spy.get(0) + //System.out.println("eee"+Mockito.RETURNS_MOCKS); + // System.out.println(Mockito.when(.size()).thenReturn(10)); + // Mockito.when(mokitoDb.addUser(new User("Ueli"))).thenReturn(true); + // Mockito.when(uv.doesUsernameExist("sssssssn")).thenReturn(true); + // System.out.println("????????????? "+Mockito.when(uv.doesUsernameExist("sss")).thenReturn(true)); + // System.out.println("????????????? "+Mockito.when(mokitoDb.addUser(new User(""))).thenReturn(true)); + // System.out.println("????????????? "+Mockito.when(mokitoDb.addUser(new User())).thenReturn()); + // when(mokitoDb.get(0)).thenReturn("first"); +// System.out.println(Mockito.doReturn(Arrays.asList(new User("peter"))).when(mokitoDb).getUsers());//github +// System.out.println(Mockito.doReturn(Arrays.asList(u1)).when(mokitoDb).getUsers()); +// System.out.println(Mockito.doReturn(Arrays.asList(u1)).when(mokitoDb).getUsers());// ); + int x = 1; + // System.out.println(Mockito.doReturn(Arrays.asList(u1)).when(x=1)); + System.out.println("dddd "+Mockito.doReturn(Arrays.asList(u1))); + // boolean w =Mockito.doReturn(Arrays.asList(new User("peter"))).when(mokitoDb).getUsers(); + System.out.println(); + // System.out.println("??? "+Mockito.when(mokitoDb.get(0)).thenReturn("first")); + // System.out.println("??"+Mockito.when(mokitoDb.getUsers()).thenReturn(true)); + //System.out.println("spy: "+Mockito.spy(0));//geht nicht stürtzt ab + //System.out.println(mokitoDb.getUsers().); + +// List list = new LinkedList(); +// // List spy = spy(list); +// +// //optionally, you can stub out some methods: +// //when(spy.size()).thenReturn(100); +// +// //using the spy calls *real* methods +// spy.add("one"); +// spy.add("two"); +// +// //prints "one" - the first element of a list +// System.out.println(spy.get(0)); +// +// //size() method was stubbed - 100 is printed +// System.out.println(spy.size()); + + + + //Mockito.doReturn(true).when(uv.doesUsernameExist("ich bin der erste Name in DB")); +//Mockito.mock(uv.doesUsernameExist(),"ddxdcc"); + + //System.out.println(FakeDatabase.); + +// User u1 = new User("qqqq1"); //User erstellen +// User u2 = new User("qqqq2"); //User erstellen +// System.out.println("u1 name: "+u1.getUsername()); //name ausgeben +// System.out.println("u2 name: "+u2.getUsername()); +// db.addUser(u1); //Mokitodb user hinzufügen +// db.addUser(u2); +// System.out.println("db.getUsers"+db.getUsers()); + + // for(int i = 0; i