From 82d80e43f9e640a518d8904c1f63f025203aa45c Mon Sep 17 00:00:00 2001 From: BinHong Lee Date: Sat, 8 Jul 2017 02:02:56 -0700 Subject: [PATCH] Added test for deleteUser function --- src/main/kotlin/libsys/UserFactory.kt | 17 ++++++++++++----- src/test/kotlin/libsys/UserFactoryTest.kt | 8 +++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/libsys/UserFactory.kt b/src/main/kotlin/libsys/UserFactory.kt index f491633..d2c2c24 100644 --- a/src/main/kotlin/libsys/UserFactory.kt +++ b/src/main/kotlin/libsys/UserFactory.kt @@ -147,7 +147,14 @@ class UserFactory */ fun getUser(index: Int): User { - return search(index, 0, users.size - 1) + try + { + return search(index, 0, users.size - 1) + } + catch (e: Exception) + { + throw NullPointerException() + } } /** @@ -186,11 +193,11 @@ class UserFactory } } - fun deleteUser(id: Int): Boolean + fun deleteUser(user: User): Boolean { try { - return users.remove(getUser(id)) + return users.remove(user) } catch (e: Exception) { @@ -198,11 +205,11 @@ class UserFactory } } - fun deleteUser(user: User): Boolean + fun deleteUser(i: Int): Boolean { try { - return users.remove(user) + return users.remove(getUser(i)) } catch (e: Exception) { diff --git a/src/test/kotlin/libsys/UserFactoryTest.kt b/src/test/kotlin/libsys/UserFactoryTest.kt index 524e214..a982fb1 100644 --- a/src/test/kotlin/libsys/UserFactoryTest.kt +++ b/src/test/kotlin/libsys/UserFactoryTest.kt @@ -6,7 +6,8 @@ import java.io.File /** * Test Book related operations */ -class UserFactoryTest { +class UserFactoryTest +{ private var userFactory: UserFactory? = null init @@ -85,6 +86,11 @@ class UserFactoryTest { val anotherUserFactory = UserFactory("noSuchFile.json") Assert.assertEquals("Check ID of new User of non-existent import file", anotherUserFactory.newUser("Some person", 1).id, 0) + Assert.assertTrue(userFactory!!.deleteUser(0)) + Assert.assertFalse(userFactory!!.deleteUser(0)) + Assert.assertTrue(userFactory!!.deleteUser(user2)) + Assert.assertFalse(userFactory!!.deleteUser(user2)) + try { anotherUserFactory.getUser(1)