1

I am pretty new to Apex, and was hoping someone could potentially point me to the right direction in my code.

Here is a trigger I wrote:

trigger inactivateAccountsRestrictions on Account (before update) {
if(!UserInfo.getUserName().contains('arcddcoe'))
{

    if(trigger.new[0].Status__c == 'Inactive')
    {
        Id profileId=userinfo.getProfileId();
        String sysAdminProfileId=[Select Id,Name from Profile where Name='System Administrator'].Id;

        if(profileId != sysAdminProfileId){

            List<OpportunityStage> closedStages = [Select MasterLabel, IsClosed from OpportunityStage where IsClosed = true];
            Set<String> closedStageNames = new Set<String>();
            For (OpportunityStage os:closedStages){
                closedStageNames.add(os.MasterLabel);
            }
            List<Opportunity> listAsks = [Select Id,Name,stageName from Opportunity where AccountId=:trigger.new[0].Id and stageName not in :ClosedStageNames];
            List<Task> listActivities =[Select Id,Status from Task where AccountId= :trigger.new[0].Id and Status<>'Completed'];

            if(listAsks.size()>0 && listActivities.size()>0)
            {
                if(listAsks.size() == 1 && listActivities.size() == 1)
                {
                     trigger.new[0].addError(' There is 1 open Ask and 1 open Activity on this Account. To inactivate this Account, you must first close the open Ask and Activity.');
                }
                else if(listAsks.size() == 1)
                {
                     trigger.new[0].addError(' There is 1 open Ask and '+listActivities.size()+' open Activities on this Account. To inactivate this Account, you must first close the open Ask and Activities.');
                }
                else if(listActivities.size() == 1)
                {
                     trigger.new[0].addError(' There are '+listAsks.size()+' open Asks and 1 open Activity on this Account. To inactivate this Account, you must first close the open Asks and Activity.');
                }
                else
                {
                    trigger.new[0].addError(' There are '+listAsks.size()+' open Asks and '+listActivities.size()+' open Activities on this Account. To inactivate this Account, you must first close the open Asks and Activities.');
                }
            }

            else if(listAsks.size()>0)
            {
                if(listAsks.size() == 1)
                {
                    trigger.new[0].addError(' There is 1 open Ask on this Account. To inactivate this Account, you must first close the open Ask.');
                }
                else
                {
                    trigger.new[0].addError(' There are '+listAsks.size()+' open Asks on this Account. To inactivate this Account, you must first close the open Asks.');
                }
            }

            else if(listActivities.size()>0)
            {
                if(listActivities.size() == 1)
                {
                    trigger.new[0].addError(' There is 1 open Activity on this Account. To inactivate this Account, you must first close the open Activity.');
                }
                else
                {
                    trigger.new[0].addError(' There are '+listActivities.size()+' open Activities on this Account. To inactivate this Account, you must first close the open Activities.');
                }
            }
        }
    }
 }
}

This is the Test Class I wrote. The only coverage I get is for if the Account is Active. The rest of my code I wrote is to check against if an Account has 2 Opportunities and 2 Tasks for an account that is marked as inactive. The issue is that when I run the test, the lines I wrote the test class against do not get covered.

Here is my sample test Class:

@isTest
private class TEST_inactiveIvo {

        //Testing when an account is changed to 'Inactive'

     static testMethod void TestInactiveAcc(){


        Account acc1 = new Account();
        acc1.Name = 'Test AccCon';
        acc1.Status__c='Active';
        insert acc1;

        Test.startTest();

        acc1.status__c='Inactive';
        update acc1;

        Test.stopTest();

    }
   //Testing with 1 Ask and 1 Task
     static testMethod void TestInactiveAccRestrictions(){

        Account acc2 = new Account();
        acc2.Name = 'Test AccCon';
        acc2.Status__c='Active';
        insert acc2;

        Opportunity ask1 = new Opportunity();
        ask1.Name='Test_Ask';
        ask1.stageName='Prospecting';
        ask1.AccountId=acc2.Id;
        ask1.CloseDate=date.Today();
        insert ask1; 

        Opportunity ask2 = new Opportunity();
        ask2.Name='Test_Ask';
        ask2.stageName='Prospecting';
        ask2.AccountId=acc2.Id;
        ask2.CloseDate=date.Today();
        insert ask2;            

        Task Activity1 = new Task();
        Activity1.WhatId=acc2.Id;
        Activity1.Status='Not Started';
        insert Activity1;

        Task Activity2 = new Task();
        Activity2.WhatId=acc2.Id;
        Activity2.Status='Not Started';
        insert Activity2;

        Test.startTest();

        acc2.status__c='Inactive';
        update acc2;            
        Test.stopTest();            
     }
}
1
  • this trigger is not bulkified and will fail whenever more than one account is updated (like via Data Loader) – cropredy Aug 5 '16 at 22:00
1

You have not created any user in your test class. So when you are running the test, it is considering your 'System Admin' profile to run this test.

That's why after this line if(profileId != sysAdminProfileId), code will not get covered.

So, in your test class create a user other than System Admin profile and run the test with System.runAs().

You should get coverage.

Recommend you to follow Testing Best Practices and How to Write Good Unit Tests

Also, refer Apex testing with RunAs

7
  • 2
    You should mention that it's very bad practice to completely omit assertions. – Adrian Larson Aug 5 '16 at 20:45
  • Thats true @AdrianLarson – Santanu Boral Aug 5 '16 at 20:49
  • Thank you very much Santanu. I really appreciate your feedback. Your suggestion is definitely on my mind that would need to get done. The item that I want to address on my test class is where I am testing 1 ask and 1 task. When I run the test class I would expect this code to receive coverage, but it is not. I am just not sure why? if(listAsks.size()>0 && listActivities.size()>0) { if(listAsks.size() == 1 && listActivities.size() == 1) – Ivo Dimov Aug 5 '16 at 20:53
  • @user35012 if it solved your issue then close this question. – Santanu Boral Aug 5 '16 at 20:56
  • Hi Santanu, sorry long day. I do see what you mean. I must set up the user first. Then the rest of my code will be applied towards the coverage, correct? – Ivo Dimov Aug 5 '16 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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