Problem Statement
Design unit test cases for the stringStyle method in the Utils class using the standard requirements of the simple loop coverage method. Implement the unit test code in the UtilsTest class.
Source Code Functionality
The application retrieves username information where the string length must be between 3 and 12 characters. To maintain visual consistency, the string is processed based on the provided minimum and maximum length cosntraints:
- If the input string is
null, processing cannot proceed. The method returns "String cannot be empty" in this case. - The string must fall within the specified minimum to maximum length range. The method trims leading and trailing whitespace before validation. If the trimmed length does not meet the requirements, it returns "String length must be between min and max characters."
- For strings shorter than the maximum length, spaces are appended until the string reaches the maximum length, then the processed string is returned.
Requirements
- Design 6 test cases using the simple loop coverage method to achieve 100% decision loop coverage.
- Implement tests using the Java JUnit testing framework.
- Write the test code directly in the provided
UtilsTest.javafile. - Use the
Assertclass from theorg.junitpackage. - Do not modify the provided filename, or the submission will receive zero credit.
Scoring Criteria
Maximum score is 30 points, distributed as follows:
- Code quality: 5 points
- Test case coverage: 25 points
- Execution: If tests fail to run, the coverage score is forfeited, though code quality points remain unaffected.
Implementation Class
package com.testing.utils;
/**
* Utility class for string processing and validation.
*
* Core functionality:
* 1. Rejects null input with appropriate error message
* 2. Validates trimmed string length against provided constraints
* 3. Pads strings shorter than maximum length with trailing spaces
*/
public class StringProcessor {
/**
* Processes a string to meet length requirements by padding with spaces.
*
* @param inputText The string to process
* @param minLen Minimum allowed length
* @param maxLen Maximum allowed length
* @return Processed string or error message
*/
public static String formatString(String inputText, int minLen, int maxLen) {
String output = null;
if (inputText != null) {
// Trim whitespace from both ends
inputText = inputText.trim();
int currentLength = inputText.length();
if (currentLength >= minLen && currentLength <= maxLen) {
output = inputText;
// Pad with spaces if length is less than maximum
for (int index = 0; index < maxLen - currentLength; index++) {
output += " ";
}
} else {
output = "String length must be between " + minLen + " and " + maxLen + " characters";
}
} else {
output = "String cannot be empty";
}
return output;
}
}
Test Class
package com.testing.utils;
import org.junit.Assert;
import org.junit.Test;
/**
* Unit tests for StringProcessor.formatString() method.
* Implements simple loop coverage with 6 test cases.
*
* Loop iterations covered: 0, 1, 2, 4, 5, and 6 times
* (maximum iteration count for this test scenario)
*/
public class StringProcessorTest {
/**
* Test case: Loop executes zero times
* Input length equals maximum length (9), so padding is unnecessary
*/
@Test
public void testLoopZeroIterations() {
String input = "123456789";
String expected = "123456789";
Assert.assertEquals(expected, StringProcessor.formatString(input, 3, 9));
}
/**
* Test case: Loop executes once
* Input length is 8, requiring 1 space for padding
*/
@Test
public void testLoopOneIteration() {
String input = "12345678";
String expected = "12345678 ";
Assert.assertEquals(expected, StringProcessor.formatString(input, 3, 9));
}
/**
* Test case: Loop executes twice
* Input length is 7, requiring 2 spaces for padding
*/
@Test
public void testLoopTwoIterations() {
String input = "1234567";
String expected = "1234567 ";
Assert.assertEquals(expected, StringProcessor.formatString(input, 3, 9));
}
/**
* Test case: Loop executes four times
* Input length is 5, requiring 4 spaces for padding
*/
@Test
public void testLoopFourIterations() {
String input = "12345";
String expected = "12345 ";
Assert.assertEquals(expected, StringProcessor.formatString(input, 3, 9));
}
/**
* Test case: Loop executes five times (maximum minus one)
* Input length is 4, requiring 5 spaces for padding
*/
@Test
public void testLoopFiveIterations() {
String input = "1234";
String expected = "1234 ";
Assert.assertEquals(expected, StringProcessor.formatString(input, 3, 9));
}
/**
* Test case: Loop executes six times (maximum iterations)
* Input length equals minimum length (3), requiring 6 spaces for padding
*/
@Test
public void testLoopSixIterations() {
String input = "123";
String expected = "123 ";
Assert.assertEquals(expected, StringProcessor.formatString(input, 3, 9));
}
}
Understanding Simple Loop Coverage
The simple loop coverage technqiue focuses on testing loops with different iteration counts. For a loop that can execute up to n times, the method requires test cases that trigger the loop 0 times, 1 time, 2 times, a value less than n, and the maximum n times. This approach ensures that loop boundary conditions and internal logic are thoroughly exercised.
In this implementation, the loop pads strings with spaces to reach the maximum length. By testing with input strings of lengths 9, 8, 7, 5, 4, and 3 (where maximum is 9 and minimum is 3), we achieve comprehensive coverage of the loop's behavior across all critical iteration points.