StringUtilitiesTest.java 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import static org.junit.Assert.*;
  2. import org.junit.After;
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. public class StringUtilitiesTest {
  6. private StringUtilities utilities;
  7. @Before
  8. public void setUp() {
  9. utilities = new StringUtilities();
  10. }
  11. @Test
  12. public void testGetMiddleCharacter_ForOddWord(){
  13. //Given
  14. String word = "bokeh";
  15. char expected = 'k';
  16. //When
  17. char actual = utilities.getMiddleCharacter(word);
  18. //Then
  19. assertEquals(expected, actual);
  20. }
  21. @Test
  22. public void testGetMiddleCharacter_ForOddWord(){
  23. //Given
  24. String word = "disinformations";
  25. char expected = 'r';
  26. //When
  27. char actual = utilities.getMiddleCharacter(word);
  28. //Then
  29. assertEquals(expected, actual);
  30. }
  31. @Test
  32. public void testGetMiddleCharacter_ForEvenWord(){
  33. //Given
  34. String word = "dogs";
  35. char expected = 'g';
  36. //When
  37. char actual = utilities.getMiddleCharacter(word);
  38. //Then
  39. assertEquals(expected, actual);
  40. }
  41. @Test
  42. public void testRemoveCharacter(){
  43. // Given
  44. String word = "taradiddle";
  45. String expected = "taraile";
  46. //When
  47. String actual = utilities.removeCharacter(word, 'd');
  48. //Then
  49. assertEquals(expected, actual);
  50. }
  51. @Test
  52. public void testRemoveCharacter_atTheEnd(){
  53. // Given
  54. String word = "taradiddle";
  55. String expected = "taradiddl";
  56. //When
  57. String actual = utilities.removeCharacter(word, 'e');
  58. //Then
  59. assertEquals(expected, actual);
  60. }
  61. @Test
  62. public void testRemoveCharacter_thatIsNotInTheString(){
  63. // Given
  64. String word = "taradiddle";
  65. String expected = "taradiddle";
  66. //When
  67. String actual = utilities.removeCharacter(word, 'x');
  68. //Then
  69. assertEquals(expected, actual);
  70. }
  71. @Test
  72. public void testGetLastWord(){
  73. // Given
  74. String sentence = "I'm sorry for what I said when I was hungry";
  75. String expected = "hungry";
  76. // When
  77. String actual = utilities.getLastWord(sentence);
  78. // Then
  79. assertEquals(expected, actual);
  80. }
  81. }