• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

test enum

 
Isaac Ferguson
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I need testing an enum, I have tries like this



but it doesn´t works because it doesn´t understand this :



The version of mockito is 1.9 -all and the version of jmock is 2.5

On the other hand I am trying with powerMock, I have been googling but Idon´t find sometrhing simple enougth for this

¿Someone knows how to mock an enum un java, please?

Regards
 
Tim Cooke
Sheriff
Pie
Posts: 2885
121
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand the question. Do you want to test an enum? Or do you want to test something else that has a dependency on an enum?
 
Isaac Ferguson
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to test something else that has a dependency on an enum I need mock it

 
Tim Cooke
Sheriff
Pie
Posts: 2885
121
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post a code example that demonstrates what you're attempting to test?

I cannot think of a scenario in which you'd need to mock out an enum so I'm curious to understand the context a little better. Perhaps there's another solution to the problem.
 
Isaac Ferguson
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the enum






The line

Throws an error like this:



I need mock it because need cal the method :



Any idea?

 
Tim Cooke
Sheriff
Pie
Posts: 2885
121
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you have some conditional logic in your production code, such as a switch on this enum value, and you want to test the default clause if you don't recognise any of the options?
For example:

In this scenario are you wanting to mock the Option enum so that you can force it to drop through to the default?

If so you might want to have a read through this thread. We had a similar discussion on this topic a short while back.

If this is not the scenario you are facing then please elaborate further.
 
Junilu Lacar
Bartender
Pie
Posts: 7306
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When something is difficult to test, sometimes it's because of the design approach. The flawed approach could be in the design of the test or in the design of the production code. In this case, it may be a little of both but I would start by re-evaluating the test design approach.

Should any of the existing enum values be treated as unexpected by the eSF.filter() method? Or are you simply trying to safeguard existing code from breakage in the event that someone changes the ImportT enum in the future? If this is your motivation, then here's my approach: Since an enum represents a set of well-known values, then write a test that will break if the number of enum values changes. Add a note in the test to tell the developer what to do if it breaks.


You would add this test to any test class that references the enum type so that the design of each class that is dependent on the enum type will have to be re-evaluated against the new set of enum values if someone changes it in the future.

In this case, the test is there not so much to verify functionality of the production code but to serve as an "alarm" that will go off if any of your current assumptions is invalidated by a change in the production code.

BTW, the enum values you have defined are very cryptic. Prefer enum values that are more descriptive.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic