Build a simple PhoneBook program.

PhoneBookTest.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import static org.junit.Assert.*;
  2. import org.junit.After;
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. /**
  6. * The test class PhoneBookTest.
  7. *
  8. * @author (your name)
  9. * @version (a version number or a date)
  10. */
  11. public class PhoneBookTest
  12. {
  13. @Test
  14. public void testAdd(){
  15. //given
  16. PhoneBook phoneBook = new PhoneBook();
  17. //actual
  18. phoneBook.add("eric", "3025884804");
  19. String actual = phoneBook.lookup("eric");
  20. //expected
  21. String expected = "3025884804";
  22. //test
  23. assertEquals(expected, actual);
  24. }
  25. @Test
  26. public void testAddMultiple(){
  27. //given
  28. PhoneBook phoneBook = new PhoneBook();
  29. //actual
  30. phoneBook.add("eric", "3025884804");
  31. phoneBook.add("john", "3025844804");
  32. phoneBook.add("jane", "3025884704");
  33. String actual = phoneBook.lookup("eric");
  34. String actual1 = phoneBook.lookup("john");
  35. String actual2 = phoneBook.lookup("jane");
  36. //expected
  37. String expected = "3025884804";
  38. String expected1 = "3025844804";
  39. String expected2 = "3025884704";
  40. //test
  41. assertEquals(expected, actual);
  42. }
  43. @Test
  44. public void testRemove(){
  45. //given
  46. PhoneBook phoneBook = new PhoneBook();
  47. //actual
  48. phoneBook.add("eric", "3025884804");
  49. phoneBook.remove("eric");
  50. String actual = phoneBook.lookup("eric");
  51. //expected
  52. String expected = null;
  53. //test
  54. assertEquals(expected, actual);
  55. }
  56. @Test
  57. public void testRemoveMultiple(){
  58. //given
  59. PhoneBook phoneBook = new PhoneBook();
  60. //actual
  61. phoneBook.add("eric", "3025884804");
  62. phoneBook.add("john", "3025844804");
  63. phoneBook.add("jane", "3025884704");
  64. phoneBook.remove("john");
  65. phoneBook.remove("eric");
  66. String actual = phoneBook.lookup("john");
  67. String actual1 = phoneBook.lookup("eric");
  68. String actual2 = phoneBook.lookup("jane");
  69. //expected
  70. String expected = null;
  71. String expected1 = null;
  72. String expected2 = "3025884704";
  73. //test
  74. assertEquals(expected, actual);
  75. assertEquals(expected1, actual1);
  76. assertEquals(expected2, actual2);
  77. }
  78. @Test
  79. public void testReverseLookup(){
  80. //given
  81. PhoneBook phoneBook = new PhoneBook();
  82. //actual
  83. phoneBook.add("eric", "3025884804");
  84. phoneBook.add("john", "3025844804");
  85. phoneBook.add("jane", "3025884704");
  86. String actual = phoneBook.reverseLookup("3025884804");
  87. String actual1 = phoneBook.reverseLookup("3025844804");
  88. String actual2 = phoneBook.reverseLookup("3025884704");
  89. String actual3 = phoneBook.reverseLookup("4025884704");
  90. //expected
  91. String expected = "eric";
  92. String expected1 = "john";
  93. String expected2 = "jane";
  94. String expected3 = null;
  95. //test
  96. assertEquals(expected, actual);
  97. assertEquals(expected1, actual1);
  98. assertEquals(expected2, actual2);
  99. }
  100. }