Given a simple enum like this:
@AllArgsConstructor
@Getter
public enum PaymentMethod {
CREDITCARD(1),
PAYPAL(2),
APPLE_PAY(3),
GOOGLE_PAY(4);
private final long id;
}
How can I write a JUnit test with AssertJ assertions which checks that the enum values and their fields are exactly as defined in the enum? So that in the end the test checks for each payment type:
- Does payment type exist in the enum?
- Does payment type X have id Y?
My current apporach works but feels a bit cumbersome. I'm pretty sure that AssertJ provides easier statements but I haven't found a solution yet.
class PaymentTypeTest {
private static final Map<Long, PaymentType> expectedPaymentTypes = new HashMap<>();
@BeforeAll
static void beforeAll() {
expectedPaymentTypes.put(1L, CREDITCARD);
expectedPaymentTypes.put(2L, PAYPAL);
expectedPaymentTypes.put(3L, APPLE_PAY);
expectedPaymentTypes.put(4L, GOOGLE_PAY);
}
@Test
void should_contain_exact_values() {
for (Map.Entry<Long, PaymentType> entry : expectedPaymentTypes.entrySet()) {
final PaymentType paymentType = entry.getValue();
assertThat(paymentType.getId()).isEqualTo(entry.getKey());
}
}
}```