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
Chad RitchieChad Ritchie 

Simple Test Class

Hey guys, 

Looking to write a test class for the following: 

trigger OpportunityAmountTrigger on Opportunity (Before Insert, Before Update) {
    for(Opportunity o : Trigger.New){
    if(trigger.isinsert && o.amount < 5000)
    o.addError('Amount can not be less than 5000');
    else if (Trigger.isUpdate && o.amount < 3000)
    o.adderror('Amount can not be updated to less than 3000');
 
    }
}

Here is what I have so far, but not sure where to go from here:

@isTest
public class OpportunityAmountTriggerTest {
    
    @isTest static void testOpportunity()
    {
        Integer Opportunity= Opportunity.amount(4000);
        System.assertEquals('Amount can not be less than 5000', Opportunity);
    }
}
Best Answer chosen by Chad Ritchie
Maharajan CMaharajan C
Hi Chad,

Please write the test class like below:

@isTest
public class OpportunityAmountTriggerTest {
    static testmethod void testOpportunity(){
        Opportunity opp = new Opportunity(
            Name= 'Test',
            Amount = 4000);
        Database.SaveResult result = Database.insert(opp, false);
        System.assertEquals('Amount can not be less than 5000',result.getErrors()[0].getMessage());
    }
    
    static testmethod void testOpportunityupdate(){
        Opportunity opp = new Opportunity(
            Name = 'Test 2',
            Amount = 6000,stageName='Prospecting',CloseDate=system.today());   
        insert opp;
        opp.Amount = 2000;
        Database.SaveResult result = Database.update(opp, false);
        System.assertEquals('Amount can not be updated to less than 3000',result.getErrors()[0].getMessage());
    }
}

Thanks,
Maharajan.C

All Answers

Anthony McDougaldAnthony McDougald
Hello Chad,
Hope that your day is off to an amazing start. We've created a test class that should give you all the code coverage you need. Hope this helps and may God bless you abundantly this year.
@isTest
public class OpportunityAmountTriggerTest {
    testmethod static void testOpportunity(){
        Opportunity opp = new Opportunity(
        Name= 'Test',
        Amount = 4000);
        Opportunity opp2 = new Opportunity(
        Name = 'Test 2',
        Amount = 6000);
        test.startTest();
        insert opp;
        insert opp2;
        opp2.Amount = 2000;
        update opp2;
        test.stopTest();
    }
}


Best Regards,
Anthony McDougald
Maharajan CMaharajan C
Hi Chad,

Please write the test class like below:

@isTest
public class OpportunityAmountTriggerTest {
    static testmethod void testOpportunity(){
        Opportunity opp = new Opportunity(
            Name= 'Test',
            Amount = 4000);
        Database.SaveResult result = Database.insert(opp, false);
        System.assertEquals('Amount can not be less than 5000',result.getErrors()[0].getMessage());
    }
    
    static testmethod void testOpportunityupdate(){
        Opportunity opp = new Opportunity(
            Name = 'Test 2',
            Amount = 6000,stageName='Prospecting',CloseDate=system.today());   
        insert opp;
        opp.Amount = 2000;
        Database.SaveResult result = Database.update(opp, false);
        System.assertEquals('Amount can not be updated to less than 3000',result.getErrors()[0].getMessage());
    }
}

Thanks,
Maharajan.C
This was selected as the best answer