How to cover method return statement in Apex Class? The Next CEO of Stack Overflow2019 Community Moderator ElectionIssues with calling 2 setTest methods in apex test classComparison fails when converting Opportunity from/to JSONSimple Apex class to return a list of stringsCompilation error with a unit testschema.getglobaldescribe needs test classTrouble creating test to cover codeHow to cover afterUndelete method when you can't UndeleteCannot Return a List of Strings - Void method must not return a valueHow to cover global class and method in test classHello i am not able to get the result from this test class

Can you replace a racial trait cantrip when leveling up?

Unreliable Magic - Is it worth it?

square root of the periodic function need to be periodic?

Can we say or write : "No, it'sn't"?

The exact meaning of 'Mom made me a sandwich'

Interfacing a button to a microcontroller (and PC) with a 50 m long cable

Arranging cats and dogs - what is wrong with my approach

Inappropriate reference requests from Journal reviewers

How did the Bene Gesserit know how to make a Kwisatz Haderach?

If a black hole is created from light, can this black hole then move at the speed of light?

What happens if you roll doubles 3 times then land on "Go to jail?"

Why do professional authors make "consistency" mistakes? And how to avoid them?

Would a completely good Muggle be able to use a wand?

Is it possible to replace duplicates of a character with one character using tr

Is 'diverse range' a pleonastic phrase?

What does "Its cash flow is deeply negative" mean?

WOW air has ceased operation, can I get my tickets refunded?

Why has the US not been more assertive in confronting Russia in recent years?

If/When UK leaves the EU, can a future goverment conduct a referendum to join the EU?

Is it professional to write unrelated content in an almost-empty email?

What is the purpose of the Evocation wizard's Potent Cantrip feature?

Example of a Mathematician/Physicist whose Other Publications during their PhD eclipsed their PhD Thesis

How to get from Geneva Airport to Metabief?

Is it possible to search for a directory/file combination?



How to cover method return statement in Apex Class?



The Next CEO of Stack Overflow
2019 Community Moderator ElectionIssues with calling 2 setTest methods in apex test classComparison fails when converting Opportunity from/to JSONSimple Apex class to return a list of stringsCompilation error with a unit testschema.getglobaldescribe needs test classTrouble creating test to cover codeHow to cover afterUndelete method when you can't UndeleteCannot Return a List of Strings - Void method must not return a valueHow to cover global class and method in test classHello i am not able to get the result from this test class










5















I'm writing a test for a schedulable class and so far it's working fine but I can't get the return statement to be covered by the test class. I'm missing something here and can't see it.



The final returns on getMonthName (return Name) and getQuarter(Return Quarter) methods are not covered by the test class.



Here the Schedulable Class:



global class MRD_LATAM_LeaseCorrection implements Schedulable 



global void execute(SchedulableContext SC)

ListofLeases();



global static void ListOfLeases ()

List<Margin_Report_Data__c> Corrections = new List<Margin_Report_Data__c>();

//Loop for year

Integer m=date.today().month();
for (Integer y=date.today().year(); y <= 2026 ; y++ )

//Loop for month



while (m <=12)


Margin_Report_Data__c mrd = new Margin_Report_Data__c();
mrd.Account__c='001f000001Fd5sIAAR';
mrd.Customer__c = 'LATAM AIRLINES GROUP S.A.';
mrd.Transaction_Date__c = date.newInstance(y, m, 1);
mrd.Transaction_Year__c = string.valueOf(y);
mrd.Transaction_Month__c = getMonthName(m);
mrd.Transaction_Quarter__c = getQuarter(m);
mrd.Program_Revenue__c = 'No';
mrd.Sale_Category__c='Leases';
mrd.Region__c='LA';
mrd.Total_Sales__c = 194800;
mrd.Total_Gross_Profit__c = 194800;
mrd.Post_Status__c = 'Posted';
mrd.Transaction_Description__c = 'Sale Income Adjustment for LATAM Lease';

Corrections.add(mrd);
m++;



m=1;


for (Margin_Report_Data__c mr : Corrections)



insert Corrections;


//Converting month number to month name
global static String getMonthName (Integer Month)
String Name;
Switch on Month
when 1
Name='January';
return Name;

when 2
Name='February';
return Name;

when 3
Name='March';
return Name;

when 4
Name='April';
return Name;

when 5
Name='May';
return Name;

when 6
Name='June';
return Name;

when 7
Name='July';
return Name;

when 8
Name='August';
return Name;

when 9
Name='September';
return Name;

when 10
Name='October';
return Name;

when 11
Name='November';
return Name;

when 12
Name='December';
return Name;


return Name; //<-- This is not covered


//Converting month number to year quarter
global static String getQuarter (Integer Month)
String Quarter;
Switch on Month
when 1
Quarter = 'Q1';
return Quarter;

when 2
Quarter = 'Q1';
return Quarter;

when 3
Quarter = 'Q1';
return Quarter;

when 4
Quarter = 'Q2';
return Quarter;

when 5
Quarter = 'Q2';
return Quarter;

when 6
Quarter = 'Q2';
return Quarter;

when 7
Quarter = 'Q3';
return Quarter;

when 8
Quarter = 'Q3';
return Quarter;

when 9
Quarter = 'Q3';
return Quarter;

when 10
Quarter = 'Q4';
return Quarter;

when 11
Quarter = 'Q4';
return Quarter;

when 12
Quarter = 'Q4';
return Quarter;


return Quarter; //<-- This is not covered






Here's the test class:



 @IsTest
public class MRD_LATAM_LeaseCorrection_Test

@IsTest static void TestDeleteData()


//Setup Margin Report
Margin_Report_Data__c MarginReport = new Margin_Report_Data__c (Transaction_Month__c='January', Transaction_Year__c='2018', Document_Number__c='10', Sales_Order__c='12345');


Test.startTest();

MRD_LATAM_LeaseCorrection mrdc = new MRD_LATAM_LeaseCorrection();
String sch = '0 59 12 * * ?';
system.schedule('Test MR Lease Correction', sch, mrdc);
mrdc.execute(null);

Test.stoptest();

List <Margin_Report_Data__c> resultmr = [SELECT id FROM Margin_Report_Data__c WHERE Customer__c = 'LATAM AIRLINES GROUP S.A.' AND Sale_Category__c = 'Leases'];


System.debug('MRD With Leases?:'+ resultmr.size());
System.assertEquals(true, resultmr.size()>0);













share|improve this question




























    5















    I'm writing a test for a schedulable class and so far it's working fine but I can't get the return statement to be covered by the test class. I'm missing something here and can't see it.



    The final returns on getMonthName (return Name) and getQuarter(Return Quarter) methods are not covered by the test class.



    Here the Schedulable Class:



    global class MRD_LATAM_LeaseCorrection implements Schedulable 



    global void execute(SchedulableContext SC)

    ListofLeases();



    global static void ListOfLeases ()

    List<Margin_Report_Data__c> Corrections = new List<Margin_Report_Data__c>();

    //Loop for year

    Integer m=date.today().month();
    for (Integer y=date.today().year(); y <= 2026 ; y++ )

    //Loop for month



    while (m <=12)


    Margin_Report_Data__c mrd = new Margin_Report_Data__c();
    mrd.Account__c='001f000001Fd5sIAAR';
    mrd.Customer__c = 'LATAM AIRLINES GROUP S.A.';
    mrd.Transaction_Date__c = date.newInstance(y, m, 1);
    mrd.Transaction_Year__c = string.valueOf(y);
    mrd.Transaction_Month__c = getMonthName(m);
    mrd.Transaction_Quarter__c = getQuarter(m);
    mrd.Program_Revenue__c = 'No';
    mrd.Sale_Category__c='Leases';
    mrd.Region__c='LA';
    mrd.Total_Sales__c = 194800;
    mrd.Total_Gross_Profit__c = 194800;
    mrd.Post_Status__c = 'Posted';
    mrd.Transaction_Description__c = 'Sale Income Adjustment for LATAM Lease';

    Corrections.add(mrd);
    m++;



    m=1;


    for (Margin_Report_Data__c mr : Corrections)



    insert Corrections;


    //Converting month number to month name
    global static String getMonthName (Integer Month)
    String Name;
    Switch on Month
    when 1
    Name='January';
    return Name;

    when 2
    Name='February';
    return Name;

    when 3
    Name='March';
    return Name;

    when 4
    Name='April';
    return Name;

    when 5
    Name='May';
    return Name;

    when 6
    Name='June';
    return Name;

    when 7
    Name='July';
    return Name;

    when 8
    Name='August';
    return Name;

    when 9
    Name='September';
    return Name;

    when 10
    Name='October';
    return Name;

    when 11
    Name='November';
    return Name;

    when 12
    Name='December';
    return Name;


    return Name; //<-- This is not covered


    //Converting month number to year quarter
    global static String getQuarter (Integer Month)
    String Quarter;
    Switch on Month
    when 1
    Quarter = 'Q1';
    return Quarter;

    when 2
    Quarter = 'Q1';
    return Quarter;

    when 3
    Quarter = 'Q1';
    return Quarter;

    when 4
    Quarter = 'Q2';
    return Quarter;

    when 5
    Quarter = 'Q2';
    return Quarter;

    when 6
    Quarter = 'Q2';
    return Quarter;

    when 7
    Quarter = 'Q3';
    return Quarter;

    when 8
    Quarter = 'Q3';
    return Quarter;

    when 9
    Quarter = 'Q3';
    return Quarter;

    when 10
    Quarter = 'Q4';
    return Quarter;

    when 11
    Quarter = 'Q4';
    return Quarter;

    when 12
    Quarter = 'Q4';
    return Quarter;


    return Quarter; //<-- This is not covered






    Here's the test class:



     @IsTest
    public class MRD_LATAM_LeaseCorrection_Test

    @IsTest static void TestDeleteData()


    //Setup Margin Report
    Margin_Report_Data__c MarginReport = new Margin_Report_Data__c (Transaction_Month__c='January', Transaction_Year__c='2018', Document_Number__c='10', Sales_Order__c='12345');


    Test.startTest();

    MRD_LATAM_LeaseCorrection mrdc = new MRD_LATAM_LeaseCorrection();
    String sch = '0 59 12 * * ?';
    system.schedule('Test MR Lease Correction', sch, mrdc);
    mrdc.execute(null);

    Test.stoptest();

    List <Margin_Report_Data__c> resultmr = [SELECT id FROM Margin_Report_Data__c WHERE Customer__c = 'LATAM AIRLINES GROUP S.A.' AND Sale_Category__c = 'Leases'];


    System.debug('MRD With Leases?:'+ resultmr.size());
    System.assertEquals(true, resultmr.size()>0);













    share|improve this question


























      5












      5








      5








      I'm writing a test for a schedulable class and so far it's working fine but I can't get the return statement to be covered by the test class. I'm missing something here and can't see it.



      The final returns on getMonthName (return Name) and getQuarter(Return Quarter) methods are not covered by the test class.



      Here the Schedulable Class:



      global class MRD_LATAM_LeaseCorrection implements Schedulable 



      global void execute(SchedulableContext SC)

      ListofLeases();



      global static void ListOfLeases ()

      List<Margin_Report_Data__c> Corrections = new List<Margin_Report_Data__c>();

      //Loop for year

      Integer m=date.today().month();
      for (Integer y=date.today().year(); y <= 2026 ; y++ )

      //Loop for month



      while (m <=12)


      Margin_Report_Data__c mrd = new Margin_Report_Data__c();
      mrd.Account__c='001f000001Fd5sIAAR';
      mrd.Customer__c = 'LATAM AIRLINES GROUP S.A.';
      mrd.Transaction_Date__c = date.newInstance(y, m, 1);
      mrd.Transaction_Year__c = string.valueOf(y);
      mrd.Transaction_Month__c = getMonthName(m);
      mrd.Transaction_Quarter__c = getQuarter(m);
      mrd.Program_Revenue__c = 'No';
      mrd.Sale_Category__c='Leases';
      mrd.Region__c='LA';
      mrd.Total_Sales__c = 194800;
      mrd.Total_Gross_Profit__c = 194800;
      mrd.Post_Status__c = 'Posted';
      mrd.Transaction_Description__c = 'Sale Income Adjustment for LATAM Lease';

      Corrections.add(mrd);
      m++;



      m=1;


      for (Margin_Report_Data__c mr : Corrections)



      insert Corrections;


      //Converting month number to month name
      global static String getMonthName (Integer Month)
      String Name;
      Switch on Month
      when 1
      Name='January';
      return Name;

      when 2
      Name='February';
      return Name;

      when 3
      Name='March';
      return Name;

      when 4
      Name='April';
      return Name;

      when 5
      Name='May';
      return Name;

      when 6
      Name='June';
      return Name;

      when 7
      Name='July';
      return Name;

      when 8
      Name='August';
      return Name;

      when 9
      Name='September';
      return Name;

      when 10
      Name='October';
      return Name;

      when 11
      Name='November';
      return Name;

      when 12
      Name='December';
      return Name;


      return Name; //<-- This is not covered


      //Converting month number to year quarter
      global static String getQuarter (Integer Month)
      String Quarter;
      Switch on Month
      when 1
      Quarter = 'Q1';
      return Quarter;

      when 2
      Quarter = 'Q1';
      return Quarter;

      when 3
      Quarter = 'Q1';
      return Quarter;

      when 4
      Quarter = 'Q2';
      return Quarter;

      when 5
      Quarter = 'Q2';
      return Quarter;

      when 6
      Quarter = 'Q2';
      return Quarter;

      when 7
      Quarter = 'Q3';
      return Quarter;

      when 8
      Quarter = 'Q3';
      return Quarter;

      when 9
      Quarter = 'Q3';
      return Quarter;

      when 10
      Quarter = 'Q4';
      return Quarter;

      when 11
      Quarter = 'Q4';
      return Quarter;

      when 12
      Quarter = 'Q4';
      return Quarter;


      return Quarter; //<-- This is not covered






      Here's the test class:



       @IsTest
      public class MRD_LATAM_LeaseCorrection_Test

      @IsTest static void TestDeleteData()


      //Setup Margin Report
      Margin_Report_Data__c MarginReport = new Margin_Report_Data__c (Transaction_Month__c='January', Transaction_Year__c='2018', Document_Number__c='10', Sales_Order__c='12345');


      Test.startTest();

      MRD_LATAM_LeaseCorrection mrdc = new MRD_LATAM_LeaseCorrection();
      String sch = '0 59 12 * * ?';
      system.schedule('Test MR Lease Correction', sch, mrdc);
      mrdc.execute(null);

      Test.stoptest();

      List <Margin_Report_Data__c> resultmr = [SELECT id FROM Margin_Report_Data__c WHERE Customer__c = 'LATAM AIRLINES GROUP S.A.' AND Sale_Category__c = 'Leases'];


      System.debug('MRD With Leases?:'+ resultmr.size());
      System.assertEquals(true, resultmr.size()>0);













      share|improve this question
















      I'm writing a test for a schedulable class and so far it's working fine but I can't get the return statement to be covered by the test class. I'm missing something here and can't see it.



      The final returns on getMonthName (return Name) and getQuarter(Return Quarter) methods are not covered by the test class.



      Here the Schedulable Class:



      global class MRD_LATAM_LeaseCorrection implements Schedulable 



      global void execute(SchedulableContext SC)

      ListofLeases();



      global static void ListOfLeases ()

      List<Margin_Report_Data__c> Corrections = new List<Margin_Report_Data__c>();

      //Loop for year

      Integer m=date.today().month();
      for (Integer y=date.today().year(); y <= 2026 ; y++ )

      //Loop for month



      while (m <=12)


      Margin_Report_Data__c mrd = new Margin_Report_Data__c();
      mrd.Account__c='001f000001Fd5sIAAR';
      mrd.Customer__c = 'LATAM AIRLINES GROUP S.A.';
      mrd.Transaction_Date__c = date.newInstance(y, m, 1);
      mrd.Transaction_Year__c = string.valueOf(y);
      mrd.Transaction_Month__c = getMonthName(m);
      mrd.Transaction_Quarter__c = getQuarter(m);
      mrd.Program_Revenue__c = 'No';
      mrd.Sale_Category__c='Leases';
      mrd.Region__c='LA';
      mrd.Total_Sales__c = 194800;
      mrd.Total_Gross_Profit__c = 194800;
      mrd.Post_Status__c = 'Posted';
      mrd.Transaction_Description__c = 'Sale Income Adjustment for LATAM Lease';

      Corrections.add(mrd);
      m++;



      m=1;


      for (Margin_Report_Data__c mr : Corrections)



      insert Corrections;


      //Converting month number to month name
      global static String getMonthName (Integer Month)
      String Name;
      Switch on Month
      when 1
      Name='January';
      return Name;

      when 2
      Name='February';
      return Name;

      when 3
      Name='March';
      return Name;

      when 4
      Name='April';
      return Name;

      when 5
      Name='May';
      return Name;

      when 6
      Name='June';
      return Name;

      when 7
      Name='July';
      return Name;

      when 8
      Name='August';
      return Name;

      when 9
      Name='September';
      return Name;

      when 10
      Name='October';
      return Name;

      when 11
      Name='November';
      return Name;

      when 12
      Name='December';
      return Name;


      return Name; //<-- This is not covered


      //Converting month number to year quarter
      global static String getQuarter (Integer Month)
      String Quarter;
      Switch on Month
      when 1
      Quarter = 'Q1';
      return Quarter;

      when 2
      Quarter = 'Q1';
      return Quarter;

      when 3
      Quarter = 'Q1';
      return Quarter;

      when 4
      Quarter = 'Q2';
      return Quarter;

      when 5
      Quarter = 'Q2';
      return Quarter;

      when 6
      Quarter = 'Q2';
      return Quarter;

      when 7
      Quarter = 'Q3';
      return Quarter;

      when 8
      Quarter = 'Q3';
      return Quarter;

      when 9
      Quarter = 'Q3';
      return Quarter;

      when 10
      Quarter = 'Q4';
      return Quarter;

      when 11
      Quarter = 'Q4';
      return Quarter;

      when 12
      Quarter = 'Q4';
      return Quarter;


      return Quarter; //<-- This is not covered






      Here's the test class:



       @IsTest
      public class MRD_LATAM_LeaseCorrection_Test

      @IsTest static void TestDeleteData()


      //Setup Margin Report
      Margin_Report_Data__c MarginReport = new Margin_Report_Data__c (Transaction_Month__c='January', Transaction_Year__c='2018', Document_Number__c='10', Sales_Order__c='12345');


      Test.startTest();

      MRD_LATAM_LeaseCorrection mrdc = new MRD_LATAM_LeaseCorrection();
      String sch = '0 59 12 * * ?';
      system.schedule('Test MR Lease Correction', sch, mrdc);
      mrdc.execute(null);

      Test.stoptest();

      List <Margin_Report_Data__c> resultmr = [SELECT id FROM Margin_Report_Data__c WHERE Customer__c = 'LATAM AIRLINES GROUP S.A.' AND Sale_Category__c = 'Leases'];


      System.debug('MRD With Leases?:'+ resultmr.size());
      System.assertEquals(true, resultmr.size()>0);










      apex unit-test code-coverage






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 22 at 14:39









      Adrian Larson

      109k19117249




      109k19117249










      asked Mar 22 at 14:37









      Alejandro FloresAlejandro Flores

      455




      455




















          3 Answers
          3






          active

          oldest

          votes


















          5














          You have unreachable statements, and your code is overly complicated. You should remove your use of switch statements entirely.



          I recommend you simplify the logic for getting quarter to something like the below:



          public Integer getQuarterNumber(Integer month)

          return 1 + (Integer)Math.floor((month-1)/3);

          public String getQuarterName(Integer month)

          return 'Q' + getQuarterNumber(month);



          This code should be very easy to cover.




          As for getting the name of the month, I would use standard Datetime formatting and keep this logic out of your own code. Something like below would be very easy to use and cover, and should update based on the running user's language:



          String getMonthName(Integer month)

          return Datetime.newInstance(2000, month, 15, 0, 0, 0).format('MMMM');



          If you want to guarantee the month name is in English, a more classic approach will give you what you want without forcing you to hit every single execution path in your test. Use a Map.



          static final Map<Integer, String> monthNames = new Map<Integer, String>

          1 => 'January',
          2 => 'February',
          // etc
          ;
          public static String getMonthName(Integer month)

          return monthNames.get(month);



          One advantage to using a map over a list here is you do not have to worry about bound checking or exceptions based on invalid input. You will simply get a name of null if you pass in 15 as your month number, for example.






          share|improve this answer

























          • I guess they are not unreachable, but I thought switch statements wouldn't compile without a when else.

            – Adrian Larson
            Mar 22 at 14:52











          • Got it. Thanks for your explanation. Should I use Switch for more complex logic evaluations then?

            – Alejandro Flores
            Mar 22 at 15:28











          • Also, I need the month names to be always in english in the format of the code because that is being used for other triggers and other logics. Can that be done with your approach?

            – Alejandro Flores
            Mar 22 at 15:30











          • @AlejandroFlores I would only use switch statement when you can't find an alternative. I'll add a way you can guarantee the month names are in English, but if that is the language on all users in your system it may be moot anyway.

            – Adrian Larson
            Mar 22 at 15:33











          • Can you edit the your answer? I'm trying to correct how to get the values of the mapping in your code but for some reason I can't do it, maybe I don't have enough reputation. Initially it was return MonthNames(); but this was not compiling so I changed it to return monthNames.get(month) and works fine. Thanks again.

            – Alejandro Flores
            Mar 22 at 17:46



















          4














          The golden rule of unit testing is that you only gain coverage for code that is executed as part of a unit test.



          Looking at your switch statements in both of your helper methods, if your input matches one of the when criteria, you set a value and then immediately return. Those return lines don't just pop you out of the switch, it pops you out of the entire method.



          Thus, if your tests are only providing input that will match one of the when criteria (the "happy path"), you'll never reach those final return lines in your two helper methods.



          No reach = no execution = no coverage.



          Honestly though, all of those return lines inside of your when blocks are only hurting you here. The behavior of switch in Apex is to execute either one or zero blocks inside the switch, and then move on to the next statement.



          If you were to remove the return; line in each of your when blocks, you would reach the final return; at the end of both of your helper methods and you'd get your coverage for that line.



          If you do follow that advice, then you should also add some more error checking (what if month is null? what if it's less than 1? what if it's greater than 12?) as well as test methods to stress those situations.



          Looking at the other answers though, I agree that switch isn't really the best tool for the job here.






          share|improve this answer























          • The reason why I have that final return is because otherwise it wasn't compiling but my understanding was that when a switch block executes the return doesn't pop you out of the entire method but I guess I have to many returns in there. I did this because I saw an example of the Switch and they were doing this but I guess that was a bad practice.

            – Alejandro Flores
            Mar 22 at 15:51











          • @AlejandroFlores Can you share the resource that you used as the basis for your code? You may have misinterpreted something, or maybe the resource you found is low-quality and should be avoided.

            – Derek F
            Mar 22 at 16:25











          • @AlejandroFlores The reason why Salesforce was complaining was because there were certain paths that could be taken (null integer, or a value outside of [1,12]) where you would not encounter a return statement. If you had added a when else block with a return statement, Salesforce would've been just fine with it. That said, I still think that a single return statement (if there weren't a better alternative to using switch outside of the switch is better practice.

            – Derek F
            Mar 22 at 16:33











          • I lost the source between computers, sorry. I'll owe you that one.

            – Alejandro Flores
            Mar 22 at 17:16


















          3














          You would need to pass in a null value to the method to reach the "default" value. Since that's not possible, you might consider optimizing your code to return a value in default, which should negate the need for a default return.



          global static String getMonthName (Integer Month)
          Switch on Month
          when 1 return 'January';
          when 2 return 'February';
          when 3 return 'March';
          when 4 return 'April';
          when 5 return 'May';
          when 6 return 'June';
          when 7 return 'July';
          when 8 return 'August';
          when 9 return 'September';
          when 10 return 'October';
          when 11 return 'November';
          when else return 'December';





          Switch statements can do what you're doing, but this would be just as efficient:



          static String[] monthNames = new String[] 'January','February','March','April','May','June','July','August','September','October','November','December' ;
          global static string getMonthName(Integer month)
          return monthNames[month-1];






          share|improve this answer























            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "459"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f254931%2fhow-to-cover-method-return-statement-in-apex-class%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            5














            You have unreachable statements, and your code is overly complicated. You should remove your use of switch statements entirely.



            I recommend you simplify the logic for getting quarter to something like the below:



            public Integer getQuarterNumber(Integer month)

            return 1 + (Integer)Math.floor((month-1)/3);

            public String getQuarterName(Integer month)

            return 'Q' + getQuarterNumber(month);



            This code should be very easy to cover.




            As for getting the name of the month, I would use standard Datetime formatting and keep this logic out of your own code. Something like below would be very easy to use and cover, and should update based on the running user's language:



            String getMonthName(Integer month)

            return Datetime.newInstance(2000, month, 15, 0, 0, 0).format('MMMM');



            If you want to guarantee the month name is in English, a more classic approach will give you what you want without forcing you to hit every single execution path in your test. Use a Map.



            static final Map<Integer, String> monthNames = new Map<Integer, String>

            1 => 'January',
            2 => 'February',
            // etc
            ;
            public static String getMonthName(Integer month)

            return monthNames.get(month);



            One advantage to using a map over a list here is you do not have to worry about bound checking or exceptions based on invalid input. You will simply get a name of null if you pass in 15 as your month number, for example.






            share|improve this answer

























            • I guess they are not unreachable, but I thought switch statements wouldn't compile without a when else.

              – Adrian Larson
              Mar 22 at 14:52











            • Got it. Thanks for your explanation. Should I use Switch for more complex logic evaluations then?

              – Alejandro Flores
              Mar 22 at 15:28











            • Also, I need the month names to be always in english in the format of the code because that is being used for other triggers and other logics. Can that be done with your approach?

              – Alejandro Flores
              Mar 22 at 15:30











            • @AlejandroFlores I would only use switch statement when you can't find an alternative. I'll add a way you can guarantee the month names are in English, but if that is the language on all users in your system it may be moot anyway.

              – Adrian Larson
              Mar 22 at 15:33











            • Can you edit the your answer? I'm trying to correct how to get the values of the mapping in your code but for some reason I can't do it, maybe I don't have enough reputation. Initially it was return MonthNames(); but this was not compiling so I changed it to return monthNames.get(month) and works fine. Thanks again.

              – Alejandro Flores
              Mar 22 at 17:46
















            5














            You have unreachable statements, and your code is overly complicated. You should remove your use of switch statements entirely.



            I recommend you simplify the logic for getting quarter to something like the below:



            public Integer getQuarterNumber(Integer month)

            return 1 + (Integer)Math.floor((month-1)/3);

            public String getQuarterName(Integer month)

            return 'Q' + getQuarterNumber(month);



            This code should be very easy to cover.




            As for getting the name of the month, I would use standard Datetime formatting and keep this logic out of your own code. Something like below would be very easy to use and cover, and should update based on the running user's language:



            String getMonthName(Integer month)

            return Datetime.newInstance(2000, month, 15, 0, 0, 0).format('MMMM');



            If you want to guarantee the month name is in English, a more classic approach will give you what you want without forcing you to hit every single execution path in your test. Use a Map.



            static final Map<Integer, String> monthNames = new Map<Integer, String>

            1 => 'January',
            2 => 'February',
            // etc
            ;
            public static String getMonthName(Integer month)

            return monthNames.get(month);



            One advantage to using a map over a list here is you do not have to worry about bound checking or exceptions based on invalid input. You will simply get a name of null if you pass in 15 as your month number, for example.






            share|improve this answer

























            • I guess they are not unreachable, but I thought switch statements wouldn't compile without a when else.

              – Adrian Larson
              Mar 22 at 14:52











            • Got it. Thanks for your explanation. Should I use Switch for more complex logic evaluations then?

              – Alejandro Flores
              Mar 22 at 15:28











            • Also, I need the month names to be always in english in the format of the code because that is being used for other triggers and other logics. Can that be done with your approach?

              – Alejandro Flores
              Mar 22 at 15:30











            • @AlejandroFlores I would only use switch statement when you can't find an alternative. I'll add a way you can guarantee the month names are in English, but if that is the language on all users in your system it may be moot anyway.

              – Adrian Larson
              Mar 22 at 15:33











            • Can you edit the your answer? I'm trying to correct how to get the values of the mapping in your code but for some reason I can't do it, maybe I don't have enough reputation. Initially it was return MonthNames(); but this was not compiling so I changed it to return monthNames.get(month) and works fine. Thanks again.

              – Alejandro Flores
              Mar 22 at 17:46














            5












            5








            5







            You have unreachable statements, and your code is overly complicated. You should remove your use of switch statements entirely.



            I recommend you simplify the logic for getting quarter to something like the below:



            public Integer getQuarterNumber(Integer month)

            return 1 + (Integer)Math.floor((month-1)/3);

            public String getQuarterName(Integer month)

            return 'Q' + getQuarterNumber(month);



            This code should be very easy to cover.




            As for getting the name of the month, I would use standard Datetime formatting and keep this logic out of your own code. Something like below would be very easy to use and cover, and should update based on the running user's language:



            String getMonthName(Integer month)

            return Datetime.newInstance(2000, month, 15, 0, 0, 0).format('MMMM');



            If you want to guarantee the month name is in English, a more classic approach will give you what you want without forcing you to hit every single execution path in your test. Use a Map.



            static final Map<Integer, String> monthNames = new Map<Integer, String>

            1 => 'January',
            2 => 'February',
            // etc
            ;
            public static String getMonthName(Integer month)

            return monthNames.get(month);



            One advantage to using a map over a list here is you do not have to worry about bound checking or exceptions based on invalid input. You will simply get a name of null if you pass in 15 as your month number, for example.






            share|improve this answer















            You have unreachable statements, and your code is overly complicated. You should remove your use of switch statements entirely.



            I recommend you simplify the logic for getting quarter to something like the below:



            public Integer getQuarterNumber(Integer month)

            return 1 + (Integer)Math.floor((month-1)/3);

            public String getQuarterName(Integer month)

            return 'Q' + getQuarterNumber(month);



            This code should be very easy to cover.




            As for getting the name of the month, I would use standard Datetime formatting and keep this logic out of your own code. Something like below would be very easy to use and cover, and should update based on the running user's language:



            String getMonthName(Integer month)

            return Datetime.newInstance(2000, month, 15, 0, 0, 0).format('MMMM');



            If you want to guarantee the month name is in English, a more classic approach will give you what you want without forcing you to hit every single execution path in your test. Use a Map.



            static final Map<Integer, String> monthNames = new Map<Integer, String>

            1 => 'January',
            2 => 'February',
            // etc
            ;
            public static String getMonthName(Integer month)

            return monthNames.get(month);



            One advantage to using a map over a list here is you do not have to worry about bound checking or exceptions based on invalid input. You will simply get a name of null if you pass in 15 as your month number, for example.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 22 at 17:48

























            answered Mar 22 at 14:46









            Adrian LarsonAdrian Larson

            109k19117249




            109k19117249












            • I guess they are not unreachable, but I thought switch statements wouldn't compile without a when else.

              – Adrian Larson
              Mar 22 at 14:52











            • Got it. Thanks for your explanation. Should I use Switch for more complex logic evaluations then?

              – Alejandro Flores
              Mar 22 at 15:28











            • Also, I need the month names to be always in english in the format of the code because that is being used for other triggers and other logics. Can that be done with your approach?

              – Alejandro Flores
              Mar 22 at 15:30











            • @AlejandroFlores I would only use switch statement when you can't find an alternative. I'll add a way you can guarantee the month names are in English, but if that is the language on all users in your system it may be moot anyway.

              – Adrian Larson
              Mar 22 at 15:33











            • Can you edit the your answer? I'm trying to correct how to get the values of the mapping in your code but for some reason I can't do it, maybe I don't have enough reputation. Initially it was return MonthNames(); but this was not compiling so I changed it to return monthNames.get(month) and works fine. Thanks again.

              – Alejandro Flores
              Mar 22 at 17:46


















            • I guess they are not unreachable, but I thought switch statements wouldn't compile without a when else.

              – Adrian Larson
              Mar 22 at 14:52











            • Got it. Thanks for your explanation. Should I use Switch for more complex logic evaluations then?

              – Alejandro Flores
              Mar 22 at 15:28











            • Also, I need the month names to be always in english in the format of the code because that is being used for other triggers and other logics. Can that be done with your approach?

              – Alejandro Flores
              Mar 22 at 15:30











            • @AlejandroFlores I would only use switch statement when you can't find an alternative. I'll add a way you can guarantee the month names are in English, but if that is the language on all users in your system it may be moot anyway.

              – Adrian Larson
              Mar 22 at 15:33











            • Can you edit the your answer? I'm trying to correct how to get the values of the mapping in your code but for some reason I can't do it, maybe I don't have enough reputation. Initially it was return MonthNames(); but this was not compiling so I changed it to return monthNames.get(month) and works fine. Thanks again.

              – Alejandro Flores
              Mar 22 at 17:46

















            I guess they are not unreachable, but I thought switch statements wouldn't compile without a when else.

            – Adrian Larson
            Mar 22 at 14:52





            I guess they are not unreachable, but I thought switch statements wouldn't compile without a when else.

            – Adrian Larson
            Mar 22 at 14:52













            Got it. Thanks for your explanation. Should I use Switch for more complex logic evaluations then?

            – Alejandro Flores
            Mar 22 at 15:28





            Got it. Thanks for your explanation. Should I use Switch for more complex logic evaluations then?

            – Alejandro Flores
            Mar 22 at 15:28













            Also, I need the month names to be always in english in the format of the code because that is being used for other triggers and other logics. Can that be done with your approach?

            – Alejandro Flores
            Mar 22 at 15:30





            Also, I need the month names to be always in english in the format of the code because that is being used for other triggers and other logics. Can that be done with your approach?

            – Alejandro Flores
            Mar 22 at 15:30













            @AlejandroFlores I would only use switch statement when you can't find an alternative. I'll add a way you can guarantee the month names are in English, but if that is the language on all users in your system it may be moot anyway.

            – Adrian Larson
            Mar 22 at 15:33





            @AlejandroFlores I would only use switch statement when you can't find an alternative. I'll add a way you can guarantee the month names are in English, but if that is the language on all users in your system it may be moot anyway.

            – Adrian Larson
            Mar 22 at 15:33













            Can you edit the your answer? I'm trying to correct how to get the values of the mapping in your code but for some reason I can't do it, maybe I don't have enough reputation. Initially it was return MonthNames(); but this was not compiling so I changed it to return monthNames.get(month) and works fine. Thanks again.

            – Alejandro Flores
            Mar 22 at 17:46






            Can you edit the your answer? I'm trying to correct how to get the values of the mapping in your code but for some reason I can't do it, maybe I don't have enough reputation. Initially it was return MonthNames(); but this was not compiling so I changed it to return monthNames.get(month) and works fine. Thanks again.

            – Alejandro Flores
            Mar 22 at 17:46














            4














            The golden rule of unit testing is that you only gain coverage for code that is executed as part of a unit test.



            Looking at your switch statements in both of your helper methods, if your input matches one of the when criteria, you set a value and then immediately return. Those return lines don't just pop you out of the switch, it pops you out of the entire method.



            Thus, if your tests are only providing input that will match one of the when criteria (the "happy path"), you'll never reach those final return lines in your two helper methods.



            No reach = no execution = no coverage.



            Honestly though, all of those return lines inside of your when blocks are only hurting you here. The behavior of switch in Apex is to execute either one or zero blocks inside the switch, and then move on to the next statement.



            If you were to remove the return; line in each of your when blocks, you would reach the final return; at the end of both of your helper methods and you'd get your coverage for that line.



            If you do follow that advice, then you should also add some more error checking (what if month is null? what if it's less than 1? what if it's greater than 12?) as well as test methods to stress those situations.



            Looking at the other answers though, I agree that switch isn't really the best tool for the job here.






            share|improve this answer























            • The reason why I have that final return is because otherwise it wasn't compiling but my understanding was that when a switch block executes the return doesn't pop you out of the entire method but I guess I have to many returns in there. I did this because I saw an example of the Switch and they were doing this but I guess that was a bad practice.

              – Alejandro Flores
              Mar 22 at 15:51











            • @AlejandroFlores Can you share the resource that you used as the basis for your code? You may have misinterpreted something, or maybe the resource you found is low-quality and should be avoided.

              – Derek F
              Mar 22 at 16:25











            • @AlejandroFlores The reason why Salesforce was complaining was because there were certain paths that could be taken (null integer, or a value outside of [1,12]) where you would not encounter a return statement. If you had added a when else block with a return statement, Salesforce would've been just fine with it. That said, I still think that a single return statement (if there weren't a better alternative to using switch outside of the switch is better practice.

              – Derek F
              Mar 22 at 16:33











            • I lost the source between computers, sorry. I'll owe you that one.

              – Alejandro Flores
              Mar 22 at 17:16















            4














            The golden rule of unit testing is that you only gain coverage for code that is executed as part of a unit test.



            Looking at your switch statements in both of your helper methods, if your input matches one of the when criteria, you set a value and then immediately return. Those return lines don't just pop you out of the switch, it pops you out of the entire method.



            Thus, if your tests are only providing input that will match one of the when criteria (the "happy path"), you'll never reach those final return lines in your two helper methods.



            No reach = no execution = no coverage.



            Honestly though, all of those return lines inside of your when blocks are only hurting you here. The behavior of switch in Apex is to execute either one or zero blocks inside the switch, and then move on to the next statement.



            If you were to remove the return; line in each of your when blocks, you would reach the final return; at the end of both of your helper methods and you'd get your coverage for that line.



            If you do follow that advice, then you should also add some more error checking (what if month is null? what if it's less than 1? what if it's greater than 12?) as well as test methods to stress those situations.



            Looking at the other answers though, I agree that switch isn't really the best tool for the job here.






            share|improve this answer























            • The reason why I have that final return is because otherwise it wasn't compiling but my understanding was that when a switch block executes the return doesn't pop you out of the entire method but I guess I have to many returns in there. I did this because I saw an example of the Switch and they were doing this but I guess that was a bad practice.

              – Alejandro Flores
              Mar 22 at 15:51











            • @AlejandroFlores Can you share the resource that you used as the basis for your code? You may have misinterpreted something, or maybe the resource you found is low-quality and should be avoided.

              – Derek F
              Mar 22 at 16:25











            • @AlejandroFlores The reason why Salesforce was complaining was because there were certain paths that could be taken (null integer, or a value outside of [1,12]) where you would not encounter a return statement. If you had added a when else block with a return statement, Salesforce would've been just fine with it. That said, I still think that a single return statement (if there weren't a better alternative to using switch outside of the switch is better practice.

              – Derek F
              Mar 22 at 16:33











            • I lost the source between computers, sorry. I'll owe you that one.

              – Alejandro Flores
              Mar 22 at 17:16













            4












            4








            4







            The golden rule of unit testing is that you only gain coverage for code that is executed as part of a unit test.



            Looking at your switch statements in both of your helper methods, if your input matches one of the when criteria, you set a value and then immediately return. Those return lines don't just pop you out of the switch, it pops you out of the entire method.



            Thus, if your tests are only providing input that will match one of the when criteria (the "happy path"), you'll never reach those final return lines in your two helper methods.



            No reach = no execution = no coverage.



            Honestly though, all of those return lines inside of your when blocks are only hurting you here. The behavior of switch in Apex is to execute either one or zero blocks inside the switch, and then move on to the next statement.



            If you were to remove the return; line in each of your when blocks, you would reach the final return; at the end of both of your helper methods and you'd get your coverage for that line.



            If you do follow that advice, then you should also add some more error checking (what if month is null? what if it's less than 1? what if it's greater than 12?) as well as test methods to stress those situations.



            Looking at the other answers though, I agree that switch isn't really the best tool for the job here.






            share|improve this answer













            The golden rule of unit testing is that you only gain coverage for code that is executed as part of a unit test.



            Looking at your switch statements in both of your helper methods, if your input matches one of the when criteria, you set a value and then immediately return. Those return lines don't just pop you out of the switch, it pops you out of the entire method.



            Thus, if your tests are only providing input that will match one of the when criteria (the "happy path"), you'll never reach those final return lines in your two helper methods.



            No reach = no execution = no coverage.



            Honestly though, all of those return lines inside of your when blocks are only hurting you here. The behavior of switch in Apex is to execute either one or zero blocks inside the switch, and then move on to the next statement.



            If you were to remove the return; line in each of your when blocks, you would reach the final return; at the end of both of your helper methods and you'd get your coverage for that line.



            If you do follow that advice, then you should also add some more error checking (what if month is null? what if it's less than 1? what if it's greater than 12?) as well as test methods to stress those situations.



            Looking at the other answers though, I agree that switch isn't really the best tool for the job here.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 22 at 14:53









            Derek FDerek F

            20.7k52353




            20.7k52353












            • The reason why I have that final return is because otherwise it wasn't compiling but my understanding was that when a switch block executes the return doesn't pop you out of the entire method but I guess I have to many returns in there. I did this because I saw an example of the Switch and they were doing this but I guess that was a bad practice.

              – Alejandro Flores
              Mar 22 at 15:51











            • @AlejandroFlores Can you share the resource that you used as the basis for your code? You may have misinterpreted something, or maybe the resource you found is low-quality and should be avoided.

              – Derek F
              Mar 22 at 16:25











            • @AlejandroFlores The reason why Salesforce was complaining was because there were certain paths that could be taken (null integer, or a value outside of [1,12]) where you would not encounter a return statement. If you had added a when else block with a return statement, Salesforce would've been just fine with it. That said, I still think that a single return statement (if there weren't a better alternative to using switch outside of the switch is better practice.

              – Derek F
              Mar 22 at 16:33











            • I lost the source between computers, sorry. I'll owe you that one.

              – Alejandro Flores
              Mar 22 at 17:16

















            • The reason why I have that final return is because otherwise it wasn't compiling but my understanding was that when a switch block executes the return doesn't pop you out of the entire method but I guess I have to many returns in there. I did this because I saw an example of the Switch and they were doing this but I guess that was a bad practice.

              – Alejandro Flores
              Mar 22 at 15:51











            • @AlejandroFlores Can you share the resource that you used as the basis for your code? You may have misinterpreted something, or maybe the resource you found is low-quality and should be avoided.

              – Derek F
              Mar 22 at 16:25











            • @AlejandroFlores The reason why Salesforce was complaining was because there were certain paths that could be taken (null integer, or a value outside of [1,12]) where you would not encounter a return statement. If you had added a when else block with a return statement, Salesforce would've been just fine with it. That said, I still think that a single return statement (if there weren't a better alternative to using switch outside of the switch is better practice.

              – Derek F
              Mar 22 at 16:33











            • I lost the source between computers, sorry. I'll owe you that one.

              – Alejandro Flores
              Mar 22 at 17:16
















            The reason why I have that final return is because otherwise it wasn't compiling but my understanding was that when a switch block executes the return doesn't pop you out of the entire method but I guess I have to many returns in there. I did this because I saw an example of the Switch and they were doing this but I guess that was a bad practice.

            – Alejandro Flores
            Mar 22 at 15:51





            The reason why I have that final return is because otherwise it wasn't compiling but my understanding was that when a switch block executes the return doesn't pop you out of the entire method but I guess I have to many returns in there. I did this because I saw an example of the Switch and they were doing this but I guess that was a bad practice.

            – Alejandro Flores
            Mar 22 at 15:51













            @AlejandroFlores Can you share the resource that you used as the basis for your code? You may have misinterpreted something, or maybe the resource you found is low-quality and should be avoided.

            – Derek F
            Mar 22 at 16:25





            @AlejandroFlores Can you share the resource that you used as the basis for your code? You may have misinterpreted something, or maybe the resource you found is low-quality and should be avoided.

            – Derek F
            Mar 22 at 16:25













            @AlejandroFlores The reason why Salesforce was complaining was because there were certain paths that could be taken (null integer, or a value outside of [1,12]) where you would not encounter a return statement. If you had added a when else block with a return statement, Salesforce would've been just fine with it. That said, I still think that a single return statement (if there weren't a better alternative to using switch outside of the switch is better practice.

            – Derek F
            Mar 22 at 16:33





            @AlejandroFlores The reason why Salesforce was complaining was because there were certain paths that could be taken (null integer, or a value outside of [1,12]) where you would not encounter a return statement. If you had added a when else block with a return statement, Salesforce would've been just fine with it. That said, I still think that a single return statement (if there weren't a better alternative to using switch outside of the switch is better practice.

            – Derek F
            Mar 22 at 16:33













            I lost the source between computers, sorry. I'll owe you that one.

            – Alejandro Flores
            Mar 22 at 17:16





            I lost the source between computers, sorry. I'll owe you that one.

            – Alejandro Flores
            Mar 22 at 17:16











            3














            You would need to pass in a null value to the method to reach the "default" value. Since that's not possible, you might consider optimizing your code to return a value in default, which should negate the need for a default return.



            global static String getMonthName (Integer Month)
            Switch on Month
            when 1 return 'January';
            when 2 return 'February';
            when 3 return 'March';
            when 4 return 'April';
            when 5 return 'May';
            when 6 return 'June';
            when 7 return 'July';
            when 8 return 'August';
            when 9 return 'September';
            when 10 return 'October';
            when 11 return 'November';
            when else return 'December';





            Switch statements can do what you're doing, but this would be just as efficient:



            static String[] monthNames = new String[] 'January','February','March','April','May','June','July','August','September','October','November','December' ;
            global static string getMonthName(Integer month)
            return monthNames[month-1];






            share|improve this answer



























              3














              You would need to pass in a null value to the method to reach the "default" value. Since that's not possible, you might consider optimizing your code to return a value in default, which should negate the need for a default return.



              global static String getMonthName (Integer Month)
              Switch on Month
              when 1 return 'January';
              when 2 return 'February';
              when 3 return 'March';
              when 4 return 'April';
              when 5 return 'May';
              when 6 return 'June';
              when 7 return 'July';
              when 8 return 'August';
              when 9 return 'September';
              when 10 return 'October';
              when 11 return 'November';
              when else return 'December';





              Switch statements can do what you're doing, but this would be just as efficient:



              static String[] monthNames = new String[] 'January','February','March','April','May','June','July','August','September','October','November','December' ;
              global static string getMonthName(Integer month)
              return monthNames[month-1];






              share|improve this answer

























                3












                3








                3







                You would need to pass in a null value to the method to reach the "default" value. Since that's not possible, you might consider optimizing your code to return a value in default, which should negate the need for a default return.



                global static String getMonthName (Integer Month)
                Switch on Month
                when 1 return 'January';
                when 2 return 'February';
                when 3 return 'March';
                when 4 return 'April';
                when 5 return 'May';
                when 6 return 'June';
                when 7 return 'July';
                when 8 return 'August';
                when 9 return 'September';
                when 10 return 'October';
                when 11 return 'November';
                when else return 'December';





                Switch statements can do what you're doing, but this would be just as efficient:



                static String[] monthNames = new String[] 'January','February','March','April','May','June','July','August','September','October','November','December' ;
                global static string getMonthName(Integer month)
                return monthNames[month-1];






                share|improve this answer













                You would need to pass in a null value to the method to reach the "default" value. Since that's not possible, you might consider optimizing your code to return a value in default, which should negate the need for a default return.



                global static String getMonthName (Integer Month)
                Switch on Month
                when 1 return 'January';
                when 2 return 'February';
                when 3 return 'March';
                when 4 return 'April';
                when 5 return 'May';
                when 6 return 'June';
                when 7 return 'July';
                when 8 return 'August';
                when 9 return 'September';
                when 10 return 'October';
                when 11 return 'November';
                when else return 'December';





                Switch statements can do what you're doing, but this would be just as efficient:



                static String[] monthNames = new String[] 'January','February','March','April','May','June','July','August','September','October','November','December' ;
                global static string getMonthName(Integer month)
                return monthNames[month-1];







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 22 at 14:46









                sfdcfoxsfdcfox

                262k12209454




                262k12209454



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Salesforce Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f254931%2fhow-to-cover-method-return-statement-in-apex-class%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Is flight data recorder erased after every flight?When are black boxes used?What protects the location beacon (pinger) of a flight data recorder?Is there anywhere I can pick up raw flight data recorder information?Who legally owns the Flight Data Recorder?Constructing flight recorder dataWhy are FDRs and CVRs still two separate physical devices?What are the data elements shown on the GE235 flight data recorder (FDR) plot?Are CVR and FDR reset after every flight?What is the format of data stored by a Flight Data Recorder?How much data is stored in the flight data recorder per hour in a typical flight of an A380?Is a smart flight data recorder possible?

                    Which is better: GPT or RelGAN for text generation?2019 Community Moderator ElectionWhat is the difference between TextGAN and LM for text generation?GANs (generative adversarial networks) possible for text as well?Generator loss not decreasing- text to image synthesisChoosing a right algorithm for template-based text generationHow should I format input and output for text generation with LSTMsGumbel Softmax vs Vanilla Softmax for GAN trainingWhich neural network to choose for classification from text/speech?NLP text autoencoder that generates text in poetic meterWhat is the interpretation of the expectation notation in the GAN formulation?What is the difference between TextGAN and LM for text generation?How to prepare the data for text generation task

                    Is there a general name for the setup in which payoffs are not known exactly but players try to influence each other's perception of the payoffs?Osborne, Nash equilibria and the correctness of beliefsIs there a name for this family of games (Binomial games?)?Perfect Bayesian EquilibriumCalculating mixed strategy equilibrium in battle of sexesPure Strategy SPNEIs there a commitment mechanism which allows players to achieve pareto optimal solutions?Extensive Form GamesAn $n$-player prisoner's dilemma where a coalition of 2 players is better off defectingTit-For-Stat Strategy Best RepliesPotential solutions of the $n$-player Prisoner's Dilemma