Skip to content

Commit

Permalink
✅ Add a tests for utility class constructors
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Fihlon <[email protected]>
  • Loading branch information
McPringle committed Nov 3, 2024
1 parent c43fecd commit 2a66a5b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/swiss/fihlon/apus/util/DownloadUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static String getString(@NotNull final String location) throws IOExceptio
}

private DownloadUtil() {
throw new IllegalStateException("Utility class");
throw new IllegalStateException("Utility classes can't be instantiated!");
}

}
2 changes: 1 addition & 1 deletion src/main/java/swiss/fihlon/apus/util/HtmlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static String extractText(@NotNull final String html) {
}

private HtmlUtil() {
throw new IllegalStateException("Utility class");
throw new IllegalStateException("Utility classes can't be instantiated!");
}

}
2 changes: 1 addition & 1 deletion src/main/java/swiss/fihlon/apus/util/PasswordUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static boolean matches(@NotNull final String password, @NotNull final Str
}

private PasswordUtil() {
throw new IllegalStateException("Utility class");
throw new IllegalStateException("Utility classes can't be instantiated!");
}

}
14 changes: 14 additions & 0 deletions src/test/java/swiss/fihlon/apus/util/DownloadUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

import org.junit.jupiter.api.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class DownloadUtilTest {
Expand All @@ -30,4 +35,13 @@ void getString() throws Exception {
assertTrue(string.endsWith("}"));
}

@Test
void expectExceptionCallingConstructor() throws Exception {
final Constructor<DownloadUtil> constructor = DownloadUtil.class.getDeclaredConstructor();
constructor.setAccessible(true);
final var exception = assertThrows(InvocationTargetException.class, constructor::newInstance).getTargetException();
assertEquals(IllegalStateException.class, exception.getClass());
assertEquals("Utility classes can't be instantiated!", exception.getMessage());
}

}
13 changes: 13 additions & 0 deletions src/test/java/swiss/fihlon/apus/util/HtmlUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

import org.junit.jupiter.api.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class HtmlUtilTest {

Expand Down Expand Up @@ -55,4 +59,13 @@ void extractText() {
HtmlUtil.extractText("Test </p>>>>Test<<<<p> Test"));
}

@Test
void expectExceptionCallingConstructor() throws Exception {
final Constructor<HtmlUtil> constructor = HtmlUtil.class.getDeclaredConstructor();
constructor.setAccessible(true);
final var exception = assertThrows(InvocationTargetException.class, constructor::newInstance).getTargetException();
assertEquals(IllegalStateException.class, exception.getClass());
assertEquals("Utility classes can't be instantiated!", exception.getMessage());
}

}
14 changes: 14 additions & 0 deletions src/test/java/swiss/fihlon/apus/util/PasswordUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@

import org.junit.jupiter.api.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class PasswordUtilTest {
Expand Down Expand Up @@ -65,4 +70,13 @@ void positiveTests() {
assertTrue(PasswordUtil.matches(rawPassword, hashedPassword5));
}

@Test
void expectExceptionCallingConstructor() throws Exception {
final Constructor<PasswordUtil> constructor = PasswordUtil.class.getDeclaredConstructor();
constructor.setAccessible(true);
final var exception = assertThrows(InvocationTargetException.class, constructor::newInstance).getTargetException();
assertEquals(IllegalStateException.class, exception.getClass());
assertEquals("Utility classes can't be instantiated!", exception.getMessage());
}

}

0 comments on commit 2a66a5b

Please sign in to comment.