

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A java code snippet for testing the replace method of the string class. The method replaces all occurrences of a specified character with another character in a given string. An example of how to write a junit test for the replace method and suggests adding more assertions to test various scenarios.
Typology: Assignments
1 / 2
This page cannot be seen from the preview
Don't miss anything!


// Returns a new string resulting from replacing // all occurrences of oldChar in this string with newChar. public String replace (char oldChar, char newChar)
// A Unit test to demonstrate the replace method of class String // Programmer C Sc 127A import static org.junit.Assert. assertEquals ; import org.junit.Test; public class TwoMethodsTest { @Test public void testStringReplace() { // 0. Test replace when one character is replaced with a different character String s1 = "abcde"; assertEquals ("abTde", s1.replace('c', 'T')); // Add at least five more assertions for these circumstances // 1. When more than one character is replaced with a different character // 2. When no characters are replaced // 3. When only the first and last characters are replaced with newChar // 4. When all characters are replaced with newChar // 5. Make sure the original string did not change after oldChar was found }