StringUtilitiesTest.java 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_ForEvenWord(){
  23. //Given
  24. String word = "dogs";
  25. char expected = 'g';
  26. //When
  27. char actual = utilities.getMiddleCharacter(word);
  28. //Then
  29. assertEquals(expected, actual);
  30. }
  31. @Test
  32. public void testRemoveCharacter(){
  33. // Given
  34. String word = "taradiddle";
  35. String expected = "taraile";
  36. //When
  37. String actual = utilities.removeCharacter(word, 'd');
  38. //Then
  39. assertEquals(expected, actual);
  40. }
  41. @Test
  42. public void testRemoveCharacter_atTheEnd(){
  43. // Given
  44. String word = "taradiddle";
  45. String expected = "taradiddl";
  46. //When
  47. String actual = utilities.removeCharacter(word, 'e');
  48. //Then
  49. assertEquals(expected, actual);
  50. }
  51. @Test
  52. public void testRemoveCharacter_thatIsNotInTheString(){
  53. // Given
  54. String word = "taradiddle";
  55. String expected = "taradiddle";
  56. //When
  57. String actual = utilities.removeCharacter(word, 'x');
  58. //Then
  59. assertEquals(expected, actual);
  60. }
  61. @Test
  62. public void testGetLastWord(){
  63. // Given
  64. String sentence = "I'm sorry for what I said when I was hungry";
  65. String expected = "hungry";
  66. // When
  67. String actual = utilities.getLastWord(sentence);
  68. // Then
  69. assertEquals(expected, actual);
  70. }
  71. }