This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Testing and the fly likes test enum Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "test enum" Watch "test enum" New topic
Author

test enum

Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 331
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
Bartender

Joined: Mar 28, 2008
Posts: 800
    
  38

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?


Tim Driven Development
Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 331
I want to test something else that has a dependency on an enum I need mock it

Tim Cooke
Bartender

Joined: Mar 28, 2008
Posts: 800
    
  38

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

Joined: Jun 22, 2012
Posts: 331
This is the enum






The line

Throws an error like this:



I need mock it because need cal the method :



Any idea?

Tim Cooke
Bartender

Joined: Mar 28, 2008
Posts: 800
    
  38

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

Joined: Feb 26, 2001
Posts: 4442
    
    5

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
 
 
subject: test enum
 
Similar Threads
Autenticate from java into Windows Active Directory
enum extending enum?
Using Enumerations
CommandLink not firing for the first time
Enum inside an Enum