0

This test class cover 94% to its controller. It didn't cover this:

ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please select atleast one file!'));

Please help me to cover the message.

controller:

/*Transaction Out Controller*/
public with sharing class TOController {
    public List<TOWrapper> listTOWrapper {get; set;}
    public List<Transaction_Request__c> selectedTO {get;set;}

    public TOController(){
        listTOWrapper = new List<TOWrapper>();
        searchRecord();
    }

    public void searchRecord(){
        listTOWrapper.clear();
            for(Transaction_Request__c a: [SELECT Name, Contact__c, Type__c, Status__c, Amount_Out__c FROM Transaction_Request__c WHERE Status__c = 'Created']){
                listTOWrapper.add(new TOWrapper(a));
            }
    }

    public void processSelected(){
        selectedTO = new List<Transaction_Request__c>();
        selectedTO.clear();
        for(TOWrapper wrapToObj : listTOWrapper){
            if(wrapToObj.selected == TRUE){
                selectedTO.add(wrapToObj.to);
            }
        }
    }

    public void ActivateData(){
        if(selectedTO.size()>0){
            for(Transaction_Request__c eas: selectedTO){
                eas.Status__c = 'Approved';
            }
            update selectedTO;
            searchRecord();
        }
        else{
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please select atleast one file!'));
        }
    }

    public void DeActivateData(){
        if(selectedTO.size()>0){
            for(Transaction_Request__c eas: selectedTO){
                eas.Status__c = 'Rejected';
            }
            update selectedTO;
            searchRecord();
        }
        else{
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please select atleast one file!'));
        }
    }

    // This is our wrapper/container class. 
    public class TOWrapper
    {
        public Transaction_Request__c to {get; set;}
        public Boolean selected {get; set;}
        public TOWrapper(Transaction_Request__c a) 
        {
            to = a;
            selected = false;
        }
    }

}

test class:

@isTest
public class TOTets {
    static testMethod void queryTest(){
        Account acc = new Account(Name='Test Account 1');
        insert acc;
        System.assert(acc.id!=null); 

        Contact con1 = new Contact(AccountId=  acc.id, LastName = 'Test Contact 1');
        insert con1;
        System.assert(con1.id!=null);

        Escrow_Account__c ea = new Escrow_Account__c(Name='test',Is_Created__c = true, Created_Datetime__c=system.now(),Current_Balance__c=0,Contact__c=con1.id);
        insert ea;
        System.assert(ea.id!=null);

        con1.Escrow_Account__c = ea.id;
        update con1;

        Opportunity op = new Opportunity(AccountId=  acc.id, Contact__c=con1.id,Name = 'DBS Master', StageName = 'Signed Up',CloseDate = date.today());
        insert op;

        Transaction_Request__c to = new Transaction_Request__c(Contact__c=con1.id, Opportunity__c =op.id,Type__c = 'Out', Reason__c = 'Deposit', Amount_Out__c = 250000, Status__c = 'Created',Escrow_Account__c = ea.id);
        insert to;
        System.assert(to.id!=null);

        //con1.Transaction_Request__c = ea.id;
        //update con1;

        //Transaction_Request__c toc = new Transaction_Request__c(Contact__c=con1.id, Type__c = 'Out', Reason__c = 'Deposit', Amount_Out__c = 250000, Status__c = 'Created');
        //insert toc;
        //System.assert(toc.id!=null);

        TOController tocc = new TOController();
        PageReference pageRef = Page.TransactionOutRequestApproval;
        Test.setCurrentPage(pageRef);
        tocc.searchRecord();

        for(TOController.TOWrapper WD : tocc.listTOWrapper)  
            WD.selected = true;

        //System.assert(ApexPages.hasMessages(ApexPages.SEVERITY.FATAL));

        tocc.processSelected();
        tocc.ActivateData();
        tocc.DeActivateData();
        tocc.searchRecord();

    }
}
1

Your case is actually quite a bit simpler than the proposed duplicate, because you have no exception handling in your class. All you have to do to get those addError() methods to run is to call ActivateData() and DeActivateData() while you have no selected TOWrappers.

You will likely want to create a second test case for this, since your current test case relies upon the presence of selected items. It appears that test case 2 can be exactly the same as test case 1, except without

    for(TOController.TOWrapper WD : tocc.listTOWrapper)  
        WD.selected = true;

and calling only

    tocc.ActivateData();
    tocc.DeActivateData();

followed by an assertion against the expected PageMessages.

As a next step, you may wish to factor out your test data creation, which would be duplicated across your two unit test cases, into an @testSetup method. This does require that you query the created objects you need in each test case, but it helps keep them cleaner.

You'll also want to make sure all of your test cases have assertions to show that the behavior of your code is as expected. You may want to further factor your test cases so that each one tests a single method or tightly-coupled set of functionality, specifically so that you can make meaningful assertions.

  • Halo Reed! Thanks for helping me. I have tried to delete for(TOController.TOWrapper WD : tocc.listTOWrapper) WD.selected = true; But the covering decrease to 73%. – martina Jun 13 '18 at 6:40
  • I did not advise you to delete that code. I advised you to create a second test case. – David Reed Jun 13 '18 at 10:59

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