Issue 207 in mockito: how to mock enum class.

6,332 views
Skip to first unread message

moc...@googlecode.com

unread,
Jul 27, 2010, 10:54:13 AM7/27/10
to mocki...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

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.

moc...@googlecode.com

unread,
Jul 27, 2010, 11:40:17 AM7/27/10
to mocki...@googlegroups.com

Comment #1 on issue 207 by szczepiq: how to mock enum class.
http://code.google.com/p/mockito/issues/detail?id=207

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?

moc...@googlecode.com

unread,
Jul 27, 2010, 11:48:36 AM7/27/10
to mocki...@googlegroups.com

Comment #2 on issue 207 by szczepiq: how to mock enum class.
http://code.google.com/p/mockito/issues/detail?id=207

BTW. enums classes are implicitly final so Mockito will not be able to mock
them.

moc...@googlecode.com

unread,
Jul 27, 2010, 1:57:07 PM7/27/10
to mocki...@googlegroups.com

Comment #3 on issue 207 by kedarbhat: how to mock enum class.
http://code.google.com/p/mockito/issues/detail?id=207

is power mockito support it..?

moc...@googlecode.com

unread,
Jul 27, 2010, 2:43:52 PM7/27/10
to mocki...@googlegroups.com

Comment #4 on issue 207 by szczepiq: how to mock enum class.
http://code.google.com/p/mockito/issues/detail?id=207

I don't know but you really should not be mocking enums. This is a code
smell.

moc...@googlecode.com

unread,
Sep 23, 2010, 7:59:40 PM9/23/10
to mocki...@googlegroups.com

Comment #5 on issue 207 by chengt: how to mock enum class.
http://code.google.com/p/mockito/issues/detail?id=207

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.

moc...@googlecode.com

unread,
Sep 24, 2010, 4:53:21 AM9/24/10
to mocki...@googlegroups.com

Comment #6 on issue 207 by szczepiq: how to mock enum class.
http://code.google.com/p/mockito/issues/detail?id=207

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 :)

moc...@googlecode.com

unread,
Oct 17, 2010, 10:56:55 AM10/17/10
to mocki...@googlegroups.com
Updates:
Status: WontFix

Comment #7 on issue 207 by szczepiq: how to mock enum class.
http://code.google.com/p/mockito/issues/detail?id=207

Not possible...

moc...@googlecode.com

unread,
Mar 26, 2011, 1:14:49 PM3/26/11
to mocki...@googlegroups.com

Comment #8 on issue 207 by peter.de...@gmail.com: how to mock enum class.
http://code.google.com/p/mockito/issues/detail?id=207

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).

moc...@googlecode.com

unread,
Mar 27, 2011, 12:55:24 AM3/27/11
to mocki...@googlegroups.com

Comment #9 on issue 207 by szcze...@gmail.com: how to mock enum class.
http://code.google.com/p/mockito/issues/detail?id=207

Hello :)

In the link you provided I don't find any rationale behind mocking enums.
It just says for 'testing purposes' and it feels a bit weak 8-)

Example: we have a crappy code that in order to be testable requires a
bunch of new features in your test framework. Would you add those features
to the framework or rather simplified the code so that it is easy to test?

Forgive me my typical 'simple-code-is-easy-to-test' rant but that's they my
mind works. One thing you should know for sure: if enums weren't final
classes they would be mockable in Mockito and I wouldn't make any effort to
disable it. Even more: if you find a clean & bullet-proof way of mocking
enums I will the first one to pull your fork. Nevertheless, I still believe
that given right design (e.g. simple) you wouldn't need to mock enums. If
only we could pair program some time I would eradicate those ideas of
yours, trust me on that :-D

> 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)

Hmm, that seems to like a bug...

moc...@googlecode.com

unread,
Apr 27, 2011, 11:33:45 PM4/27/11
to mocki...@googlegroups.com

Comment #10 on issue 207 by basil...@gmail.com: how to mock enum class.
http://code.google.com/p/mockito/issues/detail?id=207

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. :-)

moc...@googlecode.com

unread,
Feb 18, 2013, 1:47:27 PM2/18/13
to mocki...@googlegroups.com

Comment #11 on issue 207 by jorac...@gmail.com: how to mock enum class.
http://code.google.com/p/mockito/issues/detail?id=207

Mocking might not be useful, but spying sure is.

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

moc...@googlecode.com

unread,
Mar 12, 2013, 9:17:20 AM3/12/13
to mocki...@googlegroups.com

Comment #12 on issue 207 by js.cedar...@gmail.com: how to mock enum class.
http://code.google.com/p/mockito/issues/detail?id=207

Spying is very useful for enums.
Don't forget: Enums are not just simple lists. They may instead implement
interfaces and provide a lot of own code.

And since Java 1.5 it is possible to force the usage of enums implementing
a given interface with generics. I have one case here: It greatly helps
with code completion.


Sooo, why isn't it possible to mock final classes? I am sure there should
be a work around for that?

moc...@googlecode.com

unread,
Mar 12, 2013, 10:42:03 AM3/12/13
to mocki...@googlegroups.com

Comment #13 on issue 207 by brice.du...@gmail.com: how to mock enum class.
http://code.google.com/p/mockito/issues/detail?id=207

> why isn't it possible to mock final classes?
Because in our current implementations we are limited by the rules the JVM
enforces when loading bytecode. and you can't extend final classes. Plus in
this case an Enum is a system class (that is extended by all enums), which
may complicate even more the matter.

Powermock allows you to work around that. It uses a trick where they reload
all the necessary classes (it could be a lot) in a child classloader where
they modify the bytecode of classes before reloading them. This technic,
while rather clever, has some drawback is slower and eats even more your
memory. Plus it forces you to change your test to say which class needs to
be reloaded, which class to ignore, which can be not that straightforward
in a some cases.

In my opinion if the enum has an interface, then just mock the interface,
if the enum has way too much code, then there might be a design issue
either in production code or in test code.


Maybe someday we will have a simple technic that allows to work around with
finals, but it will require a lot of work, and may even not work as good as
hoped.

moc...@googlecode.com

unread,
Mar 18, 2015, 3:34:26 PM3/18/15
to mocki...@googlegroups.com

Comment #14 on issue 207 by raspac...@gmail.com: how to mock enum class.
https://code.google.com/p/mockito/issues/detail?id=207

I don't agree with some of the comments saying that enums having too much
code is a design issue or that mocking enums is a code smell.

In Java, enums are more than a set of values, they are a special kind of
classes that allow you to have constructors and methods. That is a very
powerful feature that Java brought for being used, and when done properly
they are very useful in enterprise applications and simplify the logic. For
example for object factories, service and datasource defintions, etc.
Example: https://github.com/raspacorp/maker.
Reply all
Reply to author
Forward
0 new messages