Build a simple PhoneBook program.

PhoneBookTest.java 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import static org.junit.Assert.*;
  2. import org.junit.After;
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. public class PhoneBookTest
  6. {
  7. @Test
  8. public void testAdd()
  9. {
  10. PhoneBook test = new PhoneBook();
  11. boolean expected = true;
  12. String name = "Jenny";
  13. String number = "(302)-867-5309";
  14. test.add(name, number);
  15. boolean actual = test.getPhoneBook().containsKey(name);
  16. assertEquals(expected, actual);
  17. }
  18. // vvvv I hope this is okay since I know the add method works now vvvv
  19. @Test
  20. public void testAddPhoneBookEntry_And_Lookup()
  21. {
  22. PhoneBook test = new PhoneBook();
  23. String name = "Jenny";
  24. String expectedNumber = "(302)-867-5309";
  25. test.add(name, expectedNumber);
  26. String actual = test.lookup(name);
  27. assertEquals(expectedNumber, actual);
  28. }
  29. @Test
  30. public void testRemoveEntry_And_Lookup()
  31. {
  32. PhoneBook test = new PhoneBook();
  33. String name = "Jenny";
  34. String number = "(302)-867-5309";
  35. test.add(name, number);
  36. test.removeEntry(name);
  37. String actual = test.lookup(name);
  38. assertEquals("N/A", actual);
  39. }
  40. @Test
  41. public void testReverseLookup()
  42. {
  43. PhoneBook test = new PhoneBook();
  44. String phoneNumber = "(302)-867-5309";
  45. String expectedName = "Jenny";
  46. test.add(expectedName, phoneNumber);
  47. String actual = test.reverseLookup(phoneNumber);
  48. assertEquals(expectedName, actual);
  49. }
  50. @Test
  51. public void testRemoveIndividualNumber() {
  52. PhoneBook test = new PhoneBook();
  53. String name = "Jenny";
  54. String number = "(302)-867-5309";
  55. String number2 = "(601)-482-9832";
  56. test.add(name, number);
  57. test.add(name, number2);
  58. test.remove(number);
  59. String actual = test.reverseLookup(number);
  60. assertEquals("N/A", actual);
  61. }
  62. @Test
  63. public void testDisplay()
  64. {
  65. PhoneBook test = new PhoneBook();
  66. test.add("hey", "302");
  67. test.add("yo", "521");
  68. test.add("hey", "498");
  69. test.display();
  70. }
  71. }