BAEL-6044: Fix the IllegalArgumentException: No enum const class (#13171)
This commit is contained in:
parent
63b714f00f
commit
3b770c4dee
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.exception.noenumconst;
|
||||
|
||||
public enum Priority {
|
||||
|
||||
HIGH("High"), MEDIUM("Medium"), LOW("Low");
|
||||
|
||||
private String name;
|
||||
|
||||
Priority(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.exception.noenumconst;
|
||||
|
||||
public class PriorityUtils {
|
||||
|
||||
public static Priority getByName(String name) {
|
||||
return Priority.valueOf(name);
|
||||
}
|
||||
|
||||
public static Priority getByUpperCaseName(String name) {
|
||||
if (name == null || name.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Priority.valueOf(name.toUpperCase());
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(getByName("Low"));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.exception.noenumconst;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class PriorityUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
void givenCustomName_whenUsingGetByName_thenThrowIllegalArgumentException() {
|
||||
assertThrows(IllegalArgumentException.class, () -> PriorityUtils.getByName("Low"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenCustomName_whenUsingGetByUpperCaseName_thenReturnEnumConstant() {
|
||||
assertEquals(Priority.HIGH, PriorityUtils.getByUpperCaseName("High"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenInvalidCustomName_whenUsingGetByUpperCaseName_thenThrowIllegalArgumentException() {
|
||||
assertThrows(IllegalArgumentException.class, () -> PriorityUtils.getByUpperCaseName("invalid"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenEmptyName_whenUsingGetByUpperCaseName_thenReturnNull() {
|
||||
assertNull(PriorityUtils.getByUpperCaseName(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNull_whenUsingGetByUpperCaseName_thenReturnNull() {
|
||||
assertNull(PriorityUtils.getByUpperCaseName(null));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue