diff --git a/core-java-modules/core-java-string-operations-5/src/test/java/com/baeldung/replace/ReplaceStringUnitTest.java b/core-java-modules/core-java-string-operations-5/src/test/java/com/baeldung/replace/ReplaceStringUnitTest.java new file mode 100644 index 0000000000..081ffabab1 --- /dev/null +++ b/core-java-modules/core-java-string-operations-5/src/test/java/com/baeldung/replace/ReplaceStringUnitTest.java @@ -0,0 +1,22 @@ +package com.baeldung.replace; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class ReplaceStringUnitTest { + private final String ORIGINAL_STRING = "This is 'Baeldung' tutorial."; + private final String EXPECTED_STRING = "This is \\'Baeldung\\' tutorial."; + + @Test + public void givenString_thenReplaceUsinReplaceAllMethod() { + String modifiedString = ORIGINAL_STRING.replaceAll("'", "\\\\'"); + assertEquals(EXPECTED_STRING, modifiedString); + } + + @Test + public void givenString_thenReplaceUsinReplaceMethod() { + String modifiedString = ORIGINAL_STRING.replace("'", "\\'"); + assertEquals(EXPECTED_STRING, modifiedString); + } +} \ No newline at end of file