ssacher-tgm / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

how to mock enum class. #207

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
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.

Original issue reported on code.google.com by kedarb...@gmail.com on 27 Jul 2010 at 2:53

GoogleCodeExporter commented 8 years ago
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?

Original comment by szcze...@gmail.com on 27 Jul 2010 at 3:39

GoogleCodeExporter commented 8 years ago
BTW. enums classes are implicitly final so Mockito will not be able to mock 
them.

Original comment by szcze...@gmail.com on 27 Jul 2010 at 3:42

GoogleCodeExporter commented 8 years ago
is power mockito support it..?

Original comment by kedarb...@gmail.com on 27 Jul 2010 at 5:56

GoogleCodeExporter commented 8 years ago
I don't know but you really should not be mocking enums. This is a code smell.

Original comment by szcze...@gmail.com on 27 Jul 2010 at 6:43

GoogleCodeExporter commented 8 years ago
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.

Original comment by chengt on 23 Sep 2010 at 11:59

GoogleCodeExporter commented 8 years ago
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 :)

Original comment by szcze...@gmail.com on 24 Sep 2010 at 8:52

GoogleCodeExporter commented 8 years ago
Not possible...

Original comment by szcze...@gmail.com on 17 Oct 2010 at 2:56

GoogleCodeExporter commented 8 years ago
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).

Original comment by peter.de...@gmail.com on 26 Mar 2011 at 5:13

GoogleCodeExporter commented 8 years ago
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...  

Original comment by szcze...@gmail.com on 27 Mar 2011 at 4:54

GoogleCodeExporter commented 8 years ago
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. :-)

Original comment by basil...@gmail.com on 28 Apr 2011 at 3:32

GoogleCodeExporter commented 8 years ago
Mocking might not be useful, but spying sure is.

Original comment by jorac...@gmail.com on 18 Feb 2013 at 6:47

GoogleCodeExporter commented 8 years ago
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?

Original comment by js.cedar...@gmail.com on 12 Mar 2013 at 1:17

GoogleCodeExporter commented 8 years ago
> 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.

Original comment by brice.du...@gmail.com on 12 Mar 2013 at 2:41

GoogleCodeExporter commented 8 years ago
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.

Original comment by raspac...@gmail.com on 18 Mar 2015 at 7:34