Skip to content

Commit

Permalink
update&test[utils]: Add positive and negative JUnit tests to Paramete…
Browse files Browse the repository at this point in the history
…rValidator class, ensuring full test coverage
  • Loading branch information
Fredrik Marjoni committed Mar 24, 2026
1 parent 316d43c commit 218a32f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/main/java/edu/group5/app/utils/ParameterValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public static final void stringChecker(String stringArg, String variableName) th
* @throws IllegalArgumentException if the integer is null or not a positive integer
*/
public static final void intChecker(int intArg, String variableName) throws IllegalArgumentException {
nullCheck(intArg, variableName);
if (intArg <= 0) {
throw new IllegalArgumentException(String.format("%s must be a positive integer", variableName));
}
Expand Down
63 changes: 63 additions & 0 deletions src/test/java/edu/group5/app/utils/ParameterValidatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package edu.group5.app.utils;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class ParameterValidatorTest {

@Test
void testValidatorDoesNotThrowWithValidParameters() {
assertDoesNotThrow(() -> ParameterValidator.stringChecker("valid", "validString"));
assertDoesNotThrow(() -> ParameterValidator.intChecker(1, "positiveInt"));
assertDoesNotThrow(() -> ParameterValidator.objectChecker(new Object(), "validObject"));
assertDoesNotThrow(() -> ParameterValidator.bigDecimalChecker(java.math.BigDecimal.valueOf(1), "positiveBigDecimal"));
}

@Test
void testValidatorThrowsWithStringChecker() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
ParameterValidator.stringChecker(null, "nullString");
});
IllegalArgumentException exception2 = assertThrows(IllegalArgumentException.class, () -> {
ParameterValidator.stringChecker("", "emptyString");
});
IllegalArgumentException exception3 = assertThrows(IllegalArgumentException.class, () -> {
ParameterValidator.stringChecker(" ", "blankString");
});
assertEquals("nullString can't be null", exception.getMessage());
assertEquals("emptyString can't be blank", exception2.getMessage());
assertEquals("blankString can't be blank", exception3.getMessage());
}

@Test
void testValidatorThrowsWithIntChecker() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
ParameterValidator.intChecker(-1, "negativeInt");
});
assertEquals("negativeInt must be a positive integer", exception.getMessage());
}

@Test
void testValidatorThrowsWithObjectChecker() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
ParameterValidator.objectChecker(null, "nullObject");
});
assertEquals("nullObject can't be null", exception.getMessage());
}

@Test
void testValidatorThrowsWithBigDecimalChecker() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
ParameterValidator.bigDecimalChecker(null, "nullBigDecimal");
});
IllegalArgumentException exception2 = assertThrows(IllegalArgumentException.class, () -> {
ParameterValidator.bigDecimalChecker(java.math.BigDecimal.valueOf(-1), "negativeBigDecimal");
});
IllegalArgumentException exception3 = assertThrows(IllegalArgumentException.class, () -> {
ParameterValidator.bigDecimalChecker(java.math.BigDecimal.ZERO, "zeroBigDecimal");
});
assertEquals("nullBigDecimal can't be null", exception.getMessage());
assertEquals("negativeBigDecimal must be larger than 0", exception2.getMessage());
assertEquals("zeroBigDecimal must be larger than 0", exception3.getMessage());
}
}

0 comments on commit 218a32f

Please sign in to comment.