+ Start a Discussion
ChiyanChiyan 

write a test class for below trigger


trigger OpportunityTrigger on Opportunity (before update) {
    if(Trigger.isBefore && Trigger.isUpdate){
        for(Opportunity op:Trigger.new){
            if((Trigger.oldMap.get(op.id).Amount!=Trigger.newMap.get(op.Id).Amount) && op.StageName=='Closed Won'){
                op.addError('you cannot change Amount on Won stage');
            }
        }
    }
}
Khan AnasKhan Anas (Salesforce Developers) 
Hi Chiyan,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
@isTest
public class Test_TriggerError {
    
    @isTest
    static void TestResellerCustomer(){
        
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        opp.Amount = 100;
        opp.CloseDate = System.today() + 5;
        opp.StageName = 'Closed Won';
        INSERT opp;
        
        Test.startTest();
        
        opp.Amount = 200;
        try{
        	UPDATE opp;
        }
        catch (DmlException ex) {}
        Test.StopTest();  
        
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
GovindarajGovindaraj
Hi Chiyan,

Below code helps, if you have an Opportunity associated with Account,
@isTest
public class OptyTestClass {
    @isTest
    public static void test_method_one() {
		Account accObj = new Account();
		accObj.Name = 'Test Account';
		//Fill the required fields (based on your org validations)
		insert accObj;
		list<Account> accRec = [SELECT Id FROM Account WHERE Id = :accObj.Id];
		System.assertEquals(accRec.size(),1);

		Opportunity optyObj = new Opportunity();
		optyObj.AccountId = accObj.Id;
		optyObj.Name = 'Test Opty';
		optyObj.Amount = 100;
        optyObj.CloseDate = System.today() + 5;
        optyObj.StageName = 'Closed Won';
		//Fill the required fields (based on your org validations)
		insert optyObj;
		list<Opportunity> OptyRec = [SELECT Id FROM Opportunity WHERE Id = :optyObj.Id];
		System.assertEquals(OptyRec.size(),1);
		
		Test.startTest();
		opp.Amount = 200;
		update optyObj;
		Test.stopTest();
        list<Opportunity> OptyRec = [SELECT Id, Amount FROM Opportunity WHERE Id = :optyObj.Id];
        System.assertEquals(OptyRec.Amount,200);
	}
}
Thanks,
Govindaraj.S
 
Raj VakatiRaj Vakati
Use this 
@isTest
public class OptyTestClass {
    @isTest
    public static void test_method_one() {
		Account accObj = new Account();
		accObj.Name = 'Test Account';
		//Fill the required fields (based on your org validations)
		insert accObj;
		list<Account> accRec = [SELECT Id FROM Account WHERE Id = :accObj.Id];
		System.assertEquals(accRec.size(),1);

		Opportunity optyObj = new Opportunity();
		optyObj.AccountId = accObj.Id;
		optyObj.Name = 'Test Opty';
		optyObj.Amount = 100;
        optyObj.CloseDate = System.today() + 5;
        optyObj.StageName = 'Closed Won';
		//Fill the required fields (based on your org validations)
		insert optyObj;
		list<Opportunity> OptyRec = [SELECT Id FROM Opportunity WHERE Id = :optyObj.Id];
		System.assertEquals(OptyRec.size(),1);
		
		Test.startTest();
try{
		opp.Amount = 200;
		update optyObj;
}catch(Exception e){
}
		Test.stopTest();
        list<Opportunity> OptyRec = [SELECT Id, Amount FROM Opportunity WHERE Id = :optyObj.Id];
        System.assertEquals(OptyRec.Amount,200);
	}
}