Reputation: 724
I have very simple scenario here, I just want to know how can I write a test case effective enough to catch regression issues in future.
All I want to test is that the argument being passed to my method under test(SUT) reside in the valid range.
code
public class LengthValidator{
public static final int MAX_LENGTH = 300;
public boolean isValid(String pattern){
if (pattern.length() < 0 || pattern.length() > MAX_LENGTH){
return false;
}
return true;
}
}
test
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.ArrayList;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class LengthValidatorTest {
@DisplayName("Should validate pattern of varied lengths against maxValue=" + LengthValidator.MAX_LENGTH)
@ParameterizedTest(name = "test: {index} => pattern of length: {0} should result into: {1}")
@MethodSource(value = "createPatternsWithVariedLength")
public void testShouldVerifyDiffernetLengthsOfPattern(int patternLength, boolean expectedResult) {
//given
String pattern = createPatternString(patternLength);
//when
boolean result = LengthValidator.validatePattern(pattern, new ArrayList<>());
//then
assertEquals(expectedResult, result);
}
private static Stream<Arguments> createPatternsWithVariedLength() {
return Stream.of(
Arguments.of(0, true)
Arguments.of(LengthValidator.MAX_LENGTH - 1, true),
Arguments.of(LengthValidator.MAX_LENGTH, true),
Arguments.of(LengthValidator.MAX_LENGTH + 1, false));
}
private String createPatternString(int length) {
StringBuilder patternBuilder = new StringBuilder();
for (int i = 0; i < length; i++) {
patternBuilder.append("G");
}
return patternBuilder.toString();
}
}
Is my current unit test enough for this functionality?
Is there an efficient way I can write a test for 0<pattern.length()<500
such that, for scenario where lets say a developer adds an exemption to this range the current test should fail? e.g
if (pattern.length() < 0 || pattern.length() > MAX_LENGTH || pattern.length()==20){
return false;
}
PS: I know I can use random but then still test relies on random() throwing a number which might or might not fail for the exemption scenario I specified earlier.
Upvotes: 1
Views: 1984
Reputation: 2293
Let's imagine following situation:
void validateUserName(String name){
if(!VALID_NAME.equals(name){
throw new BadNameException();
}
}
Using your approach you have to check ALL words in the world just in case if someone will add exception into if condition. Seems wrong, right?
Instead I would test following:
Success case: number is in range 0..300
Failure case: number is out of range
Corner case: number equals to 0 or 300.
p.s. team members should understand why do you need testing. If you have situations when someone adds something without tests it is not tests problem. You can't catch all kind of such changes and it is not worth it. It could be solved by code reviews only.
Upvotes: 2