Exception handling added in UserTest
authorMichael <Michael@Michael-LT.bfh.ch>
Tue, 27 Oct 2015 09:34:30 +0000 (10:34 +0100)
committerMichael <Michael@Michael-LT.bfh.ch>
Tue, 27 Oct 2015 09:34:30 +0000 (10:34 +0100)
src/test/java/ch/bfh/ti/srs/core/UserTest.java

index d1b5656..985f1f4 100644 (file)
@@ -11,31 +11,62 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;\r
 import static org.junit.Assert.assertTrue;\r
 \r
+import org.junit.Rule;\r
 import org.junit.Test;\r
+import org.junit.rules.ExpectedException;\r
 \r
 public class UserTest {\r
-\r
+       //@Rule\r
+       //public ExpectedException createUserException = ExpectedException.none();\r
+       \r
        @Test\r
-       public void createUserTest() {\r
+       public void createUserTest() throws Exception{\r
                User testUser = new User("TestUser", "pass");\r
                assertEquals("TestUser", testUser.getUserName());\r
+               \r
+               //Zweite M√∂glichkeit um Exceptions zu testen, so lange keine Exception erwartet wird reicht es\r
+               //eine allf√§llige mit 'throws Exception' an junit weiterzuleiten.\r
+               //--------------------------------------------------------------------------------------------\r
+               /*createUserException.expect(IllegalArgumentException.class);\r
+               createUserException.expectMessage("Invalid username");\r
+               User wrongArgumentUser1 = new User(null, "1234");\r
+               createUserException.expectMessage("Invalid password");\r
+               User wrongArgumentUser2 = new User("TestUser2", null);*/\r
+       }\r
+       \r
+       @Test(expected=IllegalArgumentException.class)\r
+       public void createUserInvalidUsernameTest() {\r
+               User testUser = new User(new String(),"pass");\r
+       }\r
+       \r
+       @Test(expected=IllegalArgumentException.class)\r
+       public void createUserInvalidPasswordTest() {\r
+               User testUser = new User("TestUser", new String());\r
        }\r
 \r
        @Test\r
-       public void checkPasswordTest() {\r
+       public void checkPasswordTest() throws Exception{\r
                User testUser = new User("TestUser", "pass");\r
                assertTrue(testUser.checkPassword("pass"));\r
                assertFalse(testUser.checkPassword("notmypass"));\r
+               assertFalse(testUser.checkPassword(null));\r
        }\r
-\r
+       \r
        @Test\r
-       public void changePasswordTest() {\r
+       public void changePasswordTest() throws Exception{\r
                User testUser = new User("TestUser", "pass");\r
                assertTrue(testUser.checkPassword("pass"));\r
                testUser.changePassword("newpass");\r
                assertTrue(testUser.checkPassword("newpass"));\r
                assertFalse(testUser.checkPassword("pass"));\r
                assertFalse(testUser.checkPassword("notmypass"));\r
+               assertFalse(testUser.checkPassword(null));\r
+       }\r
+       \r
+       @Test(expected=IllegalArgumentException.class)\r
+       public void changePasswordInvalidPasswordTest() {\r
+               User testUser = new User("TestUser", "pass");\r
+               testUser.changePassword(null);\r
        }\r
 \r
        @Test\r
@@ -48,6 +79,7 @@ public class UserTest {
                assertTrue(testUser.setMail(goodMail));\r
                assertFalse(testUser.setMail(wrongMail1));\r
                assertFalse(testUser.setMail(wrongMail2));\r
+               assertFalse(testUser.setMail(null));\r
        }\r
 \r
        @Test\r
@@ -64,6 +96,8 @@ public class UserTest {
                User testUser = new User("TestUser", "pass");\r
                testUser.setName("Tester");\r
                assertEquals("Tester",testUser.getName());\r
+               testUser.setName(null);\r
+               assertEquals(null, testUser.getName());\r
        }\r
 \r
        @Test\r
@@ -71,5 +105,7 @@ public class UserTest {
                User testUser = new User("TestUser", "pass");\r
                testUser.setLastname("Dummy");\r
                assertEquals("Dummy", testUser.getLastname());\r
+               testUser.setLastname(null);\r
+               assertEquals(null, testUser.getLastname());\r
        }\r
 }\r