假定一个美酒类型

PizzaStatusEnum readyStatus = PizzaStatusEnum.READY;

使用 valueOf(“”)方法

@Test
public void whenConvertedIntoEnum_thenGetsConvertedCorrectly() {

String pizzaEnumValue = "READY";
PizzaStatusEnum pizzaStatusEnum
= PizzaStatusEnum.valueOf(pizzaEnumValue);
assertTrue(pizzaStatusEnum == PizzaStatusEnum.READY);
}

注意: 如果参数不对,会抛出非法参数异常

@Test(expected = IllegalArgumentException.class)
public void whenConvertedIntoEnum_thenThrowsException() {
String pizzaEnumValue = "invalid";
PizzaStatusEnum pizzaStatusEnum = PizzaStatusEnum.valueOf(pizzaEnumValue);
}
本文总阅读量