NoIssue: Add two tests for random Strings

This commit is contained in:
Minimons
2025-12-03 10:39:51 +01:00
parent 2f25bc7dc4
commit c8d8829f83

View File

@@ -0,0 +1,54 @@
package com.r35157.libs.random.tests.sad;
import com.r35157.libs.random.RandomValueGeneratorInt;
import com.r35157.libs.random.RandomValueGeneratorString;
import com.r35157.libs.random.impl.ref.RandomValueGeneratorIntImpl;
import com.r35157.libs.random.impl.ref.RandomValueGeneratorStringImpl;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@DisplayName("Sad Random Strings Tests")
public class IncorrectRandomStringTests {
@DisplayName("Do initializing with <null> throw NPE?")
@Test
void testThatInitializingWithNullIntGeneratorThrows() {
// Arrange
String expectedErrorMsg = "Cannot initialize with <null> RandomValueGeneratorInt!";
RandomValueGeneratorInt rvgi = null;
// Act + Assert
NullPointerException thrown = assertThrows(
NullPointerException.class,
() -> new RandomValueGeneratorStringImpl(rvgi)
);
// Assert
String actualErrorMsg = thrown.getMessage();
assertEquals(expectedErrorMsg, actualErrorMsg);
}
@DisplayName("Do generating negative length Strings throw IAE?")
@Test
void testThatGeneratingStringsOfNegativeLengthThrowsIAE() {
// Arrange
int length = -1;
String expectedErrorMsg = "Cannot generate random Strings of size " + length + "!";
RandomValueGeneratorString sut = new RandomValueGeneratorStringImpl(rvgi);
// Act + Assert
IllegalArgumentException thrown = assertThrows(
IllegalArgumentException.class,
() -> sut.getSomeStringAlphaNumericOnly(length)
);
// Assert
String actualErrorMsg = thrown.getMessage();
assertEquals(expectedErrorMsg, actualErrorMsg);
}
private static final RandomValueGeneratorInt rvgi = new RandomValueGeneratorIntImpl();
}