Build a simple PhoneBook program.

PhoneBookTest.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import org.junit.Assert;
  2. import org.junit.After;
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. import java.util.Iterator;
  6. import java.util.Map;
  7. import java.util.Set;
  8. import java.util.SortedMap;
  9. import java.util.TreeMap;
  10. import java.util.ArrayList;
  11. /**
  12. * The test class PhoneBookTest.
  13. *
  14. * @author (your name)
  15. * @version (a version number or a date)
  16. */
  17. public class PhoneBookTest
  18. {
  19. PhoneBook book;
  20. @Before
  21. public void setUp()
  22. {
  23. this.book = new PhoneBook();
  24. }
  25. @Test
  26. public void addTest() {
  27. //Given
  28. //When
  29. book.add("Leeks", "194-345-3458");
  30. String lookup = book.lookup("Leeks");
  31. //Then
  32. Assert.assertEquals("194-345-3458", lookup);
  33. }
  34. @Test
  35. public void lookupTest() {
  36. //Given
  37. book.add("Leeks", "194-345-3458");
  38. book.add("Leeks", "345-234-4643");
  39. String phoneNumber = book.lookup("Leeks");
  40. //Then
  41. Assert.assertEquals("194-345-3458, 345-234-4643", phoneNumber);
  42. }
  43. @Test
  44. public void lookupTest1() {
  45. //Given
  46. book.add("Leeks", "194-345-3458");
  47. book.add("Leeks", "345-234-4643");
  48. String phoneNumber = book.lookup("Banana");
  49. //Then
  50. Assert.assertEquals("This entry doesn't exist. Please enter valid entry.", phoneNumber);
  51. }
  52. @Test
  53. public void removeTest() {
  54. //Given
  55. book.add("Leeks", "194-345-3458");
  56. book.add("Leeks", "345-234-4643");
  57. book.remove("Leeks", "194-345-3458");
  58. //When
  59. String phoneNumber = book.lookup("Leeks");
  60. //Then
  61. Assert.assertEquals("345-234-4643", phoneNumber);
  62. }
  63. @Test
  64. public void removeTest1() {
  65. //Given
  66. //When
  67. book.add("Leeks", "194-345-3458");
  68. book.add("Leeks", "345-234-4643");
  69. book.remove("Leeks", "194-345-3458");
  70. book.remove("Leeks", "345-234-4643");
  71. //Then
  72. Assert.assertEquals("This entry doesn't exist. Please enter valid entry.", book.lookup("Leeks"));
  73. }
  74. @Test
  75. public void reverseLookupTest() {
  76. //Given
  77. book.add("Leeks", "194-345-3458");
  78. //When
  79. String name = book.reverseLookup("194-345-3458");
  80. //Then
  81. Assert.assertEquals("Leeks", name);
  82. }
  83. @Test
  84. public void reverseLookupTest1() {
  85. //Given
  86. book.add("Leeks", "194-345-3458");
  87. book.remove("Leeks", "194-345-3458");
  88. //When
  89. String name = book.reverseLookup("194-345-3458");
  90. //Then
  91. Assert.assertEquals("This entry doesn't exist. Please enter valid entry.", name);
  92. }
  93. }