JUnit test cases for a string manipulation. Which test cases should I go for it?

Hi there;

For JUnit test cases for a string manipulation, which test cases should I go for roughly?

Reversing a string, or replacing a substring in a string may be examples for manipulation.

Kind regards.
LVL 12
jazzIIIloveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
I'm not sure which TestCase you should go for ?

I guess you should test the methods of your program - if you are doing reversing - test that method,
if you hacvemethod for replacing test that method.  You are testing those method which you are writing.
0
Jim CakalicSenior Developer/ArchitectCommented:
Think about the expected behavior of each feature - happy path as well as edge cases. With string reversal you might have the test cases that check:
happy path: choose some string and test that the reversal produces the expected reversed content
single char string: may be an optimization in the method
zero length string: another possible optimization; edge case
null: edge case; expect NullPointerException or return of null?

You would do something similar for string substitution:
replace matching segment once
replace matching segment twice
replace matching segment multiple times
match string not found in original string: return original string
replace single char match string with multichar string
replace multichar match string with single char string
null original string: NPE or return original string?
null match string: NPE or return original string?
null replacement string: NPE  or return original string?
zero length original string: return original string
zero length match string: return original string
zero length replacement string: matching segments removed
match string .equals() replace string: returns original string
match string == replace string: returns original string

If the string replace supports regular expressions then you would have more test cases that ensure the match string is treated as a regex and that flags like ignorecase are honored.

Regards,
Jim
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jazzIIIloveAuthor Commented:
jim, thanks.

Here you go for a string replacing, I am not sure how to implement your step 3 and 4. Could you fill out the test case below for those 2?

I mean should I do those in seperate functions?

public void replaceOccurence()
	{
		String search = "adim";
		String replace = "kabo";
		StringBuffer buffer =  new StringBuffer(str);

		for(int i = 0; i < str.length() - search.length() + 1; i++)
		{
			if(str.substring(i, i + search.length()).equals(search))
				buffer.replace(i, i + search.length(), replace);
		}
		System.out.println("Result: " + buffer);
	}

//Test case
import junit.framework.TestCase;
import org.junit.*;

public class TestStringManipulation extends TestCase {

public void testReplaceOccurence() {
		//fail("Not yet implemented");
	}
}

Open in new window

0
Jim CakalicSenior Developer/ArchitectCommented:
Assuming you define your utility method in a class like this:

public class Strings {

	public static String replaceOccurence(String str, String search, String replace) {
		StringBuffer buffer = new StringBuffer(str);
		for (int i = 0; i < str.length() - search.length() + 1; i++) {
			if (str.substring(i, i + search.length()).equals(search)) {
				buffer.replace(i, i + search.length(), replace);
			}
		}
		return buffer.toString();
	}
}

Open in new window


then the first few methods of your JUnit test class might look like this:

public class TestStringManipulation extends TestCase {

	public void testReplaceOnce() {
		String input = "aaa bbb ccc";
		String expected = "ddd bbb ccc";

		String output = Strings.replaceOccurence(input, "aaa", "ddd");

		assertEquals(expected, output);
	}

	public void testReplaceTwice() {
		String input = "aaa bbb ccc aaa";
		String expected = "ddd bbb ccc ddd";

		String output = Strings.replaceOccurence(input, "aaa", "ddd");

		assertEquals(expected, output);
	}

	public void testReplaceMultiple() {
		String input = "ab ab ac ad ab ac ad ab ab ad";
		String expected = "ba ba ac ad ba ac ad ba ba ad";

		String output = Strings.replaceOccurence(input, "ab", "ba");

		assertEquals(expected, output);
	}

	public void testMatchNotFound() {
		String input = "ab ab ac ad ab ac ad ab ab ad";
		String expected = "ab ab ac ad ab ac ad ab ab ad";

		String output = Strings.replaceOccurence(input, "zz", "ba");

		assertEquals(expected, output);
	}

	public void testReplaceSingleCharWithMultiChar() {
		String input = "abcdefgabcdefgabcdefg";
		String expected = "abcdefghijkabcdefghijkabcdefghijk";

		String output = Strings.replaceOccurence(input, "g", "ghijk");

		assertEquals(expected, output);
	}
}

Open in new window


If you run this you will see that the last method fails -- you have a defect in your replaceOccurence method. :-)

Regards,
Jim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Software

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.