简体   繁体   English

在Java中使用Reflection更新枚举

[英]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. 有没有一种方法可以使用枚举中的反射来随时添加其他动物类型,为此我必须使测试用例失败。

PS 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. PS这些值与在服务器上并行运行的另一个模块有些相似,因此将来如果某些其他编码器更新枚举,以确保他也必须在另一个模块中进行这些更改。 So, I am adding a functional test case around it. 因此,我正在围绕它添加一个功能测试用例。

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. 在这种情况下,您还可以编写一个测试用例,该用例从该枚举中“获取”所有值,并断言大小为2,值是预期的,依此类推。另外,在两个模块中都有重复的枚举,这些枚举应保留在同步似乎是设计/建筑的味道。 May be both modules should pick up these values from a shared db or a shared cache to properly mitigate this. 可能两个模块都应该从共享数据库或共享缓存中选取这些值以适当地减轻这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM