pankaj_ar
pankaj_ar

Reputation: 765

Update enum using Reflection in java

I have declared an enum

enum Animal {
    Cat("Tom"), Rat("Jerry");
}

I have to add test cases on the values. Is there a way to add another Animal Type on the go using reflection in enum, for which I have to fail the test case.

P.S. These values have some resemblance with another module runs parallel on server, so in future if some other coder updates enum, to ensure he has to make those changes in another module as well. So, I am adding a functional test case around it.

Upvotes: 0

Views: 68

Answers (1)

gargkshitiz
gargkshitiz

Reputation: 2168

All the values in an enum should be known upfront. In this case, you can also write a test case which 'gets' all the values from this enum and asserts that the size is 2 , values are as expected etc. On a separate note, having duplicate enums in two modules which should remain in sync seems like a design/architecture smell. May be both modules should pick up these values from a shared db or a shared cache to properly mitigate this.

Upvotes: 1

Related Questions