0

What I am missing I can not do the code coverage for the for loop.

public class myControllerClass{

public string aText{get;set;}

public string iText{get;set;}

    public myControllerClass () 
    {
        String userId;
        List<User> uList; 
        try
        {   

                userId=Userinfo.getUserId();


                if(userId!=null && userId!='')
                {
                    uList=[SELECT id,Business_Unit__c,Country__c, Language__c,LanguageLocaleKey,IsActive FROM User WHERE ID=:userId AND IsActive=True LIMIT 1];

                    if(uList[0].LanguageLocaleKey == 'en_US') {
                       uList[0].Language__c ='English';
                    }
                    System.Debug('Current User List' + uList);
                }
        }
        Catch(System.NullPointerException e)
        {

        }   


        List<Message__c> msgList =[SELECT
            SP.Name,
            SP.Region__c,
            SP.Country__c,
            SP.Language__c,
            SP.Description__c,
            SP.Context__c,
            SP.Key__c,
            SP.Active__c
            FROM Message__cSP
            WHERE SP.Region__c =: UList[0].Business_Unit__c
            AND SP.Country__c=:UList[0].Country__c
            AND SP.Language__c=:UList[0].Language__c
            AND SP.Context__c = 'Contracts'
            AND ((SP.Key__c = 'Asset_Register') OR (SP.Key__c = 'Invoice_Status') )
            AND SP.Active__c=:TRUE
            ];
         System.Debug('msgList:' + msgList);


         for(Message__citem : msgList){
           if(item.Key__c == 'Asset_Register) {
             assetDisplayText = item.Description__c;
           } else if(item.Key__c == 'Invoice_Status’) {
             invoiceDisplayText = item.Description__c;
           }
         }

    }   
    }
}

***************Test Class************

 @isTest
    public class ContractsHoverOverTest{

        private static User createUser(String region){
            String nameStr = ''+Datetime.now().getTime();
            String alias = (''+Datetime.now().getTime()).substring(8);

            Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 

            User u = new User(Alias = alias, Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName=nameStr, LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName=nameStr+'@testorg.com', 
            Business_Unit__c=region);
            return u;
        }

        private static List<CP_Messages__c> createMessage(){
            List<CP_Messages__c> msgList = new List<CP_Messages__c>();

            CP_Messages__c msgEng = new CP_Messages__c();

            msgEng.Region__c ='EM';
            msgEng.Country__c = 'United Kingdom';
            msgEng.Language__c = 'English';
            msgEng.Context__c = 'Contracts';
            msgEng.Key__c ='Invoice_Status_hoverover';
            msgEng.Active__c = true;
            msgEng.Description__c = 'This is United Kingdom Language';

            msgList.add(msgEng);

            CP_Messages__c msgGer = new CP_Messages__c();

            msgGer.Region__c ='EMEA';
            msgGer.Country__c = 'Germany';
            msgGer.Language__c = 'German';
            msgGer.Context__c = 'Contracts';
            msgGer.Key__c ='Asset_Register_hoverover';
            msgGer.Active__c = true;
            msgGer.Description__c = 'This is German Langaiage';

            msgList.add(msgGer);

            return msgList;        
        }   
        static testMethod void contractPageHoverOver(){

           User usr =  createUser('United Kingdom');
           insert usr;
           System.assertEquals(usr.LanguageLocaleKey, 'en_US');

           System.runAs(usr){
               List<CP_Messages__c> msg = createMessage();
               insert msg;

                System.assertEquals(msg.size(),2);

                PageReference pageRef = new PageReference('/apex/ContractsList');
                Test.setCurrentPageReference(pageRef);
               // ApexPages.currentPage().getParameters().put(msg);
                myControllerClass ctrl = new myControllerClass ();

           }       
        }
    }
0

First:

You need to set the message context__c in your test to equal Contracts because of this line in your class

AND SP.Context__c = 'Contracts

In addition make sure your user values are equal to what is expected

WHERE SP.Region__c =: UList[0].Business_Unit__c
        AND SP.Country__c=:UList[0].Country__c
        AND SP.Language__c=:UList[0].Language__c

Check that the user you are creating has the values

Business_unit__c = EMEA

Country__c = United Kingdom OR Germany

Language__c = English or German

0

You can try to refactor your controller to be easier to test. The loop can be its own method. The method name can serve as a comment. Ie

private void setAssetAndInvoiceDisplayText(List<Message__c> messages){
    for(Message__c item : messages){
        if(item.Key__c == 'Asset_Register') {
            assetDisplayText = item.Description__c;
            } else if(item.Key__c == 'Invoice_Status') {
                invoiceDisplayText = item.Description__c;
            }
        }
    }   
}

Now you can test this method with whatever input you want.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.