0

This question already has an answer here:

I have this simple trigger.

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'];
                system.debug('This is number of asks ' + listAsks.size());
                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.');
                    }
                }
            }
        }  } }

In my test class, I am testing for if the Account is active, and I also want to test against when I have 1 Opportunity, and 1 Activity opened. For some reason when I run the test, my test class does not cover that line of the trigger, and I can not figure out why.

@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;

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


        Test.startTest();

        acc2.status__c='Inactive';
        update acc2;

        Test.stopTest();

     }

}

marked as duplicate by Eric, martin, crmprogdev, Himanshu, superfell Aug 7 '16 at 15:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

Try using the following test code. You should run the test class in such a user's context that its username has 'arcddcoe' and to cover more lines its profile should not be system administer.

@isTest
        private class TEST_inactiveIvo {

          //Query on profile with condition name != 'system administrator'
          //Create a user with profileId fetched from the query
       //create the data 
          //then use system.RunAs(user){
           //update the account
         }
        }

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