Abhilasha Jain
Abhilasha Jain

Reputation: 3

Test code for enum

If I am declaring 2 enums inside my class this way:

    public class EnumerationExample {

    public enum Season {WINTER,SPRING,SUMMER,FALL}
    public enum Month {JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC}
    public List<Month> listMonths;
    public Month convert (String val) {

        for (Month mtObj : Month.values()) {

            if (mtObj.name() == val) {
                system.debug('The value passed is ' +mtObj);
            }
        }
    return null;
    }
    public List<Month> seasonMonths(Season s) {

        Season seasonObj = Season.SPRING;

        listMonths = new List<Month>();
        if(s==season.WINTER) {
            listMonths.add(Month.DEC);
            listMonths.add(Month.JAN);
            listMonths.add(Month.FEB);
        }
        else if(s==season.SPRING) {
            listMonths.add(Month.MAR);
            listMonths.add(Month.APR);
            listMonths.add(Month.MAY);
        }
        else if(s==season.SUMMER) {
            listMonths.add(Month.JUN);
            listMonths.add(Month.JUL);
            listMonths.add(Month.AUG);
        }
        else if(s==season.FALL) {
            listMonths.add(Month.SEP);
            listMonths.add(Month.OCT);
            listMonths.add(Month.NOV);
        }
    return listMonths;
    }
}

how do i write test code for this ??

i tried doing this way but it says season variable does not exist at line EnumerationExampleObj.seasonMonths(Season.WINTER);...

 @isTest
 public class TestEnumerationExample {
   public static testMethod void myUnitTest() {
    EnumerationExample EnumerationExampleObj = new EnumerationExample();
EnumerationExampleObj.convert('wintery');
  EnumerationExampleObj.seasonMonths(Season.WINTER);
    system.assertEquals(EnumerationExampleObj.listMonths.get(0) , Month.DEC );
}}

is there any problem with the access modifier or any specific annotations.?

Upvotes: 0

Views: 892

Answers (1)

Paul-Jan
Paul-Jan

Reputation: 17278

Your problem is not related to testing at all, but to C# basics like scope and syntax (your sample code is full of syntax errors).

To answer your specific question: if you define a public enum inside a class, you have to prefix it with the class name when used outside that class. Example:

var enumerationExampleObj = new EnumerationExample(); 
enumerationExampleObj.seasonMonths(EnumerationExample.Season.WINTER); 

Upvotes: 1

Related Questions