• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

test enum

 
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic