Build a simple PhoneBook program.

PhoneBookTest.java 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 testRemoveIndividualNumber() {
  42. PhoneBook test = new PhoneBook();
  43. String name = "Jenny";
  44. String number = "(302)-867-5309";
  45. String number2 = "(601)-482-9832";
  46. test.add(name, number);
  47. test.add(name, number2);
  48. test.remove(number);
  49. String actual = test.reverseLookup(number);
  50. assertEquals("N/A", actual);
  51. }
  52. @Test
  53. public void testReverseLookup()
  54. {
  55. PhoneBook test = new PhoneBook();
  56. String phoneNumber = "(302)-867-5309";
  57. String expectedName = "Jenny";
  58. test.add(expectedName, phoneNumber);
  59. String actual = test.reverseLookup(phoneNumber);
  60. assertEquals(expectedName, actual);
  61. }
  62. @Test
  63. public void testDisplay() {
  64. PhoneBook test = new PhoneBook();
  65. test.add("hey", "302");
  66. test.add("yo", "521");
  67. test.add("hey", "498");
  68. test.display();
  69. }
  70. }