0

Given the following enum:

public enum SupportedLoanProcessor {
    PRE_AUTHORIZED,
    ACCURED_INTEREST
 }

And a switch working on a value if the type SupportedLoanProessor

switch(processorType){
      case SupportedLoanProcessor.PRE_AUTHORIZED:
        result = processPreAuthorized allLendingsWithALoan, date
      break
      case SupportedLoanProcessor.ACCURED_INTEREST:
        result = processAccuredInterest allLendingsWithALoan, date
      break
      default:
        throw new IllegalArgumentException("Unknow loan processor: $processorType")
    }

How could a do to test the default case. I'm using groovy and junit. I suppose modifying the enum at runtime could be possible. But i don't know how.

benzen
  • 6,204
  • 4
  • 25
  • 37

1 Answers1

0

It is not possible for the default case to be executed, because the enumeration has no other values than those covered by the switch.

If you are trying to future proof your application, for when there are more possible values, my approach is usually to add a None to the enumeration.

public enum SupportedLoanProcessor {
    None = 0,
    PRE_AUTHORIZED,
    ACCURED_INTEREST
}
Tim B
  • 2,340
  • 15
  • 21
  • And you add this value for tests only? How about separate tests and app code? – Mr. Cat Jun 11 '13 at 19:58
  • No, I permanently add it to the enumeration. This also has the property of being the default value for an enumeration in .NET when zero is used. I don't know how it works in Java. – Tim B Jun 11 '13 at 20:02
  • I think it's not very clean aproach. You mix test and business logic tests. – Mr. Cat Jun 11 '13 at 20:08
  • I didn't realize this was a Groovy/Java question when I answered. In .NET if you have a real value assigned to 0, this will be the default, which is often not desirable in the business logic. – Tim B Jun 11 '13 at 20:17