You need to sign in to do that
Don't have an account?

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);
}
}
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);
}
}
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
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.
Best Regards,
Anthony McDougald
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