Build a simple PhoneBook program.

PhoneBookTest.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package com.zipcodewilmington.phonebook;
  2. import org.junit.Assert;
  3. import org.junit.Test;
  4. import java.util.TreeMap;
  5. /**
  6. * Created by leon on 1/23/18.
  7. */
  8. public class PhoneBookTest {
  9. @Test
  10. public void testDefaultConstructor() {
  11. PhoneBook book = new PhoneBook();
  12. Assert.assertNotNull(book);
  13. }
  14. @Test
  15. public void testConstructorWithArgument() {
  16. PhoneBook book = new PhoneBook();
  17. book.add("Bob", "302-555-1234");
  18. Assert.assertNotNull(book);
  19. }
  20. @Test
  21. public void testAdd() {
  22. //Given
  23. PhoneBook book = new PhoneBook();
  24. String expectedAddition = "302-555-1234";
  25. //When
  26. book.add("Bob", "302-555-1234");
  27. String actualAddition = book.lookup("Bob");
  28. //Then
  29. Assert.assertEquals(expectedAddition, actualAddition);
  30. }
  31. @Test
  32. public void testRemove() {
  33. //Given
  34. PhoneBook book = new PhoneBook();
  35. book.add("Bob", "302-555-1234");
  36. String expectedValue = "";
  37. //When
  38. book.remove("Bob");
  39. String actualValue = book.display();
  40. //Then
  41. Assert.assertEquals(expectedValue, actualValue);
  42. }
  43. @Test
  44. public void testRemove2() {
  45. //Given
  46. PhoneBook book = new PhoneBook();
  47. book.add("Bob", "302-555-1234");
  48. String expectedValue = "";
  49. //When
  50. book.remove("Robert");
  51. String actualValue = book.display();
  52. //Then
  53. Assert.assertNotEquals(expectedValue, actualValue);
  54. }
  55. @Test
  56. public void testLookup(){
  57. //Given
  58. PhoneBook book = new PhoneBook();
  59. String expectedNumber = "302-555-1234";
  60. book.add("Bob", expectedNumber);
  61. //When
  62. String actualNumber = book.lookup("Bob");
  63. //Then
  64. Assert.assertEquals(expectedNumber, actualNumber);
  65. }
  66. @Test
  67. public void testLookup2(){
  68. //Given
  69. PhoneBook book = new PhoneBook();
  70. book.add("Bob", "302-555-1234");
  71. String expectedNumber = null;
  72. //When
  73. String actualNumber = book.lookup("Frank");
  74. //Then
  75. Assert.assertEquals(expectedNumber, actualNumber);
  76. }
  77. @Test
  78. public void testLookup3(){
  79. //Given
  80. PhoneBook book = new PhoneBook();
  81. book.add("Bob", "302-555-1234");
  82. String expectedNumber = "302-555-9988";
  83. book.add("Frank", expectedNumber);
  84. //When
  85. String actualNumber = book.lookup("Frank");
  86. //Then
  87. Assert.assertEquals(expectedNumber, actualNumber);
  88. }
  89. @Test
  90. public void testreverseLookup(){
  91. //Given
  92. PhoneBook book = new PhoneBook();
  93. String expectedName = "Bob";
  94. book.add(expectedName, "302-555-1234");
  95. //When
  96. String actualName = book.reverseLookup("302-555-1234");
  97. //Then
  98. Assert.assertEquals(expectedName, actualName);
  99. }
  100. @Test
  101. public void testDisplay() {
  102. //Given: need help from instructor
  103. String expectedNameNumber = "Bob 302-555-1234\nFrank 777-555-1111\n";
  104. PhoneBook book = new PhoneBook();
  105. book.add("Bob", "302-555-1234");
  106. book.add("Frank", "777-555-1111");
  107. //When
  108. String actualNameNumber = book.display();
  109. //Then
  110. Assert.assertEquals(expectedNameNumber, actualNameNumber);
  111. }
  112. }