New issue 207 by kedarbhat: how to mock enum class.
http://code.google.com/p/mockito/issues/detail?id=207
What steps will reproduce the problem?
1.create a enum class.
2. try to mock it.
3.
What is the expected output? What do you see instead?
What version of the product are you using? On what operating system?
Please provide any additional information below.
I don't know what happens, I assume Mockito tells you that you cannot mock
an Enum :)
Why do you mock enum. Can't you create an enum instance and pass it to the
system under test?
BTW. enums classes are implicitly final so Mockito will not be able to mock
them.
is power mockito support it..?
I don't know but you really should not be mocking enums. This is a code
smell.
It is a code smell yes but sometimes you can't help it. For example, I'm
using a enum that is being used as a singleton (EJ2 item 3). I would like
to mock out the business logic that the singleton is doing but can't ATM.
I agree and if I could make it happen I would allow mocking final classes.
But ATM you have to either wrap this dodgy enum or user powermocking tools
and burn in hell :)
Comment #7 on issue 207 by szczepiq: how to mock enum class.
http://code.google.com/p/mockito/issues/detail?id=207
Not possible...
Actually I was trying to mock an enum for exactly the reason explained <a
href"http://www.javaspecialists.eu/archive/Issue161.html>here</a>, a use
case which is _not_ a code smell. It also explains a quite lengthy way of
mocking an enum, pretty ugly if you ask me since it depends on 'sun'
internal reflection API, but apparently it's necessary. I couldn't come up
with a better solution so far, but I'm thinking about it :-).
I understand why it is difficult to fit this behavior in Mockito, but I
would certainly appreciate it if Mockito allowed mocking enums.
What mockito does if you try to mock an enum: it doesn't fail, instead the
mock behaves as the enum instance with ordinal '0' (the 'first' enum
constant).
PowerMock does mock enums nicely. I needed to mock enum to test method that
checks that enum is from supported list of enums, no other way of doing
that apart from passing in null. :-)