Build a simple PhoneBook program.

PhoneBookTest.java 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import static org.junit.Assert.*;
  2. import org.junit.After;
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. import java.util.*;
  6. /**
  7. * The test class PhoneBookTest.
  8. *
  9. * @author (your name)
  10. * @version (a version number or a date)
  11. */
  12. public class PhoneBookTest
  13. {
  14. PhoneBook phoneBook;
  15. @Before
  16. public void setUp()
  17. {
  18. phoneBook = new PhoneBook();
  19. }
  20. @Test
  21. public void addTest(){
  22. phoneBook.add("Zebra", "111-222-333");
  23. phoneBook.add("Dog", "222-444-4444");
  24. int expected = 2;
  25. int actual = phoneBook.size();
  26. assertEquals(expected, actual);
  27. }
  28. @Test
  29. public void addToMapTest(){
  30. ArrayList<String> numbers = new ArrayList<>();
  31. numbers.add("111-222-333");
  32. numbers.add("222-444-4444");
  33. phoneBook.add("Zebra", numbers);
  34. //phoneBook.add("Dog", "222-444-4444");
  35. int expected = 1;
  36. int actual = phoneBook.size();
  37. assertEquals(expected, actual);
  38. }
  39. @Test
  40. public void removeTest(){
  41. ArrayList<String> numbers1 = new ArrayList<>();
  42. ArrayList<String> numbers2 = new ArrayList<>();
  43. numbers1.add("111-222-333");
  44. numbers1.add("222-444-4444");
  45. numbers2.add("444-222-332");
  46. numbers2.add("555-444-6666");
  47. phoneBook.add("Roy", numbers1);
  48. phoneBook.add("Josh", numbers2);
  49. int expected = 1;
  50. phoneBook.remove("Josh");
  51. int actual = phoneBook.size();
  52. assertEquals(expected, actual);
  53. }
  54. @Test
  55. public void lookUpTest(){
  56. ArrayList<String> numbers1 = new ArrayList<>();
  57. ArrayList<String> numbers2 = new ArrayList<>();
  58. numbers1.add("111-222-333");
  59. numbers1.add("222-444-4444");
  60. numbers2.add("444-222-332");
  61. numbers2.add("555-444-6666");
  62. phoneBook.add("Roy", numbers1);
  63. phoneBook.add("Josh", numbers2);
  64. ArrayList<String> expected = numbers1;
  65. ArrayList<String> actual = phoneBook.lookUp("Roy");
  66. assertEquals(expected, actual);
  67. }
  68. @Test
  69. public void reverseLookUpTest(){
  70. ArrayList<String> numbers1 = new ArrayList<>();
  71. ArrayList<String> numbers2 = new ArrayList<>();
  72. numbers1.add("111-222-333");
  73. numbers1.add("222-444-4444");
  74. numbers2.add("444-222-332");
  75. numbers2.add("555-444-6666");
  76. phoneBook.add("Roy", numbers1);
  77. phoneBook.add("Josh", numbers2);
  78. String expected = "Roy";
  79. String actual = phoneBook.reverseLookUp(numbers1);
  80. assertEquals(expected, actual);
  81. }
  82. }