unit testing - Mock an enum using Mockito in Java -


how can mock enum testing purposes using mockito? given sample enum:

public enum testenum {  yes,  no } 

and 1 method using enum:

public static boolean worktheenum(testenum theenum) { switch (theenum) {   case yes:      return true;   case no:      return false;   default:      // throws exception here  } } 

how can mock enum reach default branch of switch loop? this answer says mockito can't mock enums answer has been provided more year ago. can mock enum meanwhile or have let branch stay untested? other mocking frameworks can't used.

there 2 answers that:

a) turn powermock-like mocking framework. 2 cent(entences) there: don't that. powermock opens door land of pain; not want enter.

b) put interfaces on enums

seriously; nowadays think there 1 use case enums; , use them singletons provide service. , then, this:

public interface fooservice { void foo(); } class fooserviceimpl implements fooservice { @override void foo() ... enum fooserviceprovider implements fooservice {    instance;    private final fooservice impl  = new fooserviceimpl();    @override foo() { impl.foo() 

of course, doesn't when use enums do. thing is: shouldn't using enums way anyway. because using enums way leads shattered code - every place takes enum variable in need such switch statements; negative consequences when add / remove enum cases.

so, in case: consider turning true oo designs - have abstract base classes define methods; , use factories create subclasses (probably based on enum switches) give objects right thing.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -