function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
belabela 

please help me for the following test class

I have wrote a trigger that display error if rating ==hot,for this trigger I wrote testclass but I am getting testclass fail showing following error.
and it is also showing other trigger Trigger.updateAccount,why?can anyone help me 

System.AssertException: Assertion Failed: Expected: The Last Name "hot" is not allowed for DML, Actual: updateAccount: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.updateAccount: line 2, column 1

Trigger:
----------
trigger adderror on Account (before delete) {
 
 for(Account acc:trigger.old)
 {
   if(acc.Rating=='hot')
    {
     acc.addError('Account associtated. Cannot delete this record');

    }
 }


testclass
-------------
@isTest
public class TestAddError{

static testMethod void errorMethod(){
Account acccc=new Account(Rating='hot');
Database.saveResult sr= Database.insert(acccc,false);
System.assertEquals('The Last Name "'+acccc.rating+'" is not allowed for DML',sr.getErrors()[0].getMessage());

}

}
}
Best Answer chosen by bela
Amit Chaudhary 8Amit Chaudhary 8
"System.NullPointerException: Attempt to de-reference a null" was coming in your "updateAccount" because you was using the trigger.old in insert event.

NOTE:- Once you are doing insert in your test class then your "updateAccount" trigger is executing and when you are doing delete then "adderror" trigger is exexuting.

Please check below post for trigger context variable
1) http://amitsalesforce.blogspot.in/2015/10/trigger-context-variables.html

Trigger.old Returns a list of the old versions of the sObject records.Note that this sObject list is only available in update and delete triggers.
Issue is that you are trying to use Trigger.old in before insert

Please update your trigger like below
trigger updateAccount on Account (before insert) 
{
    for(Account b: Trigger.New)
	{
		system.debug('TRIGGER.New:AccountNAME..,,,,,'+b.name);
        system.debug('TRIGGER.New:Industry..,,,,,'+b.Industry);
    }
    Map<id,Account> NewMap=Trigger.NewMap;
    system.debug('TRIGGER.NewMAP:NAme..,,,,,'+Newmap); 
}

OR change like  below
trigger updateAccount on Account (before update) 
{
    for(Account a: Trigger.old){
        system.debug('TRIGGER.OLD:AccountNAME..,,,,,'+a.name);
                system.debug('TRIGGER.OLD:Industry..,,,,,'+a.Industry);
    }
  
    for(Account b: Trigger.New){
        system.debug('TRIGGER.New:AccountNAME..,,,,,'+b.name);
                system.debug('TRIGGER.New:Industry..,,,,,'+b.Industry);
    }
    Map<id,Account> oldMap=Trigger.oldMap;
    Map<id,Account> NewMap=Trigger.NewMap;
    system.debug('TRIGGER.oldMAP:NAme..,,,,,'+oldmap);
    system.debug('TRIGGER.NewMAP:NAme..,,,,,'+Newmap); 

}

Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Pleae try below test class.
@isTest
public class TestAddError{

	static testMethod void errorMethod()
	{
		Account acc=new Account();
		acc.name ='Test Acc';
		acc.Rating='hot';
		insert acc;
		
		try
		{
			Delete acc;
		}
		Catch(Exception ee)
		{
		}
	}

}

Let us know if this will help you

Thanks
Amit Chaudhary
belabela
Hi Amit,
thanks for the response.I have used your code but I am getting following  errors.I also want test for addError() amit,can you please help me.


I have updateaccount trigger also and it is active then i am getting following error.why?

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateAccount: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.updateAccount: line 2, column 1: []

If updateaccount trigger is deactive then I am getting following error.so I am getting following error
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]
Class.TestAddError.errorMethod: line 9, column 1

Thanks,
B.Mahanandeesh,
 
 
belabela
Hi Amit,
your code is working fine,but I want test for adderror(); and can you explain the following error ,why it is coming?and how to overcome this with making updateaccount deactive

I have updateaccount trigger also and it is active then i am getting following error.why?

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateAccount: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null objectTrigger.updateAccount: line 2, column 1: []
 
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest
public class TestAddError
{

	static testMethod void errorMethod()
	{
		Account acc=new Account();
		acc.name ='Test Acc';
		acc.Rating='hot';
		insert acc;
		
		try
		{
			Delete acc;
		}
		Catch(Exception ee)
		{
			Boolean expectedExceptionThrown =  ee.getMessage().contains('Account associtated. Cannot delete this record')  ? true : false ;
			System.AssertEquals(expectedExceptionThrown, true);
		}
	}
}

It look like some issue is coming in your trigger "updateAccount". Please deactivate "updateAccount" trigger and let us know if that will help
belabela
Hi Amit,

the above code with assert  is working fine.

thanks,
B.Mahanandeesh.

 
belabela
Hi Amit,
Can you please explain the following issue and doubts.

doubts
-------
1.For class if we write test class we will create instance for class in order to call methods and also to mention that this testclass is for the particular class,but for trigger how to mention that this testclass is for particular trigger.

issue:which i mentioned in above a code
-------
I have updateaccount trigger also and it is active then i am getting following error.why?

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateAccount: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null objectTrigger.updateAccount: line 2, column 1: []

thanks,
B.Mahanandeesh,


 
Amit Chaudhary 8Amit Chaudhary 8
If you are creating any test class for Apex class then you need to create the object of that class but for trigger test class you just need to perform the DML accourding to Trigger Event
For Example you create above trigger on Account Delete so for this trigger Test class  you need to perform the delete of account record inside your test class.

You are getting error in your updateaccount  because there should be some issue in your trigger code. Means Null check is not added in your trigger properly. Please share your trigger code i will try to fix the trigger code as well.


Please check below post to learn test class. i hope that will help you
1) http://amitsalesforce.blogspot.in/search/label/Test%20Class
2) http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Let us know if this will help you
belabela
Hi Amit,
Means we have to write only one trigger for an object,and in that we have to implement whatever DML we want ,is it right .but I wrote different events for each trigger .delete event for adderror trigger and before insert for updateAccount trigger,then why i am getting the following error .can you please explain?

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateAccount: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null objectTrigger.updateAccount: line 2, column 1: []

this is trigger where I am getting  above error 
-----------------------------------------------------------------------
trigger updateAccount on Account (before insert) {
    for(Account a: Trigger.old){
        system.debug('TRIGGER.OLD:AccountNAME..,,,,,'+a.name);
                system.debug('TRIGGER.OLD:Industry..,,,,,'+a.Industry);
    }
  
    for(Account b: Trigger.New){
        system.debug('TRIGGER.New:AccountNAME..,,,,,'+b.name);
                system.debug('TRIGGER.New:Industry..,,,,,'+b.Industry);
    }
    Map<id,Account> oldMap=Trigger.oldMap;
    Map<id,Account> NewMap=Trigger.NewMap;
    system.debug('TRIGGER.oldMAP:NAme..,,,,,'+oldmap);
    system.debug('TRIGGER.NewMAP:NAme..,,,,,'+Newmap); 

}

thanks,
B.Mahanandeesh.
Amit Chaudhary 8Amit Chaudhary 8
"System.NullPointerException: Attempt to de-reference a null" was coming in your "updateAccount" because you was using the trigger.old in insert event.

NOTE:- Once you are doing insert in your test class then your "updateAccount" trigger is executing and when you are doing delete then "adderror" trigger is exexuting.

Please check below post for trigger context variable
1) http://amitsalesforce.blogspot.in/2015/10/trigger-context-variables.html

Trigger.old Returns a list of the old versions of the sObject records.Note that this sObject list is only available in update and delete triggers.
Issue is that you are trying to use Trigger.old in before insert

Please update your trigger like below
trigger updateAccount on Account (before insert) 
{
    for(Account b: Trigger.New)
	{
		system.debug('TRIGGER.New:AccountNAME..,,,,,'+b.name);
        system.debug('TRIGGER.New:Industry..,,,,,'+b.Industry);
    }
    Map<id,Account> NewMap=Trigger.NewMap;
    system.debug('TRIGGER.NewMAP:NAme..,,,,,'+Newmap); 
}

OR change like  below
trigger updateAccount on Account (before update) 
{
    for(Account a: Trigger.old){
        system.debug('TRIGGER.OLD:AccountNAME..,,,,,'+a.name);
                system.debug('TRIGGER.OLD:Industry..,,,,,'+a.Industry);
    }
  
    for(Account b: Trigger.New){
        system.debug('TRIGGER.New:AccountNAME..,,,,,'+b.name);
                system.debug('TRIGGER.New:Industry..,,,,,'+b.Industry);
    }
    Map<id,Account> oldMap=Trigger.oldMap;
    Map<id,Account> NewMap=Trigger.NewMap;
    system.debug('TRIGGER.oldMAP:NAme..,,,,,'+oldmap);
    system.debug('TRIGGER.NewMAP:NAme..,,,,,'+Newmap); 

}

Let us know if this will help you
 
This was selected as the best answer
belabela
Hi Amit,

Thanks for providing your valuable time and making me to understand testclasses.

Thanks,
B.Mahanandeesh.