You need to sign in to do that
Don't have an account?
Ganeeesh
Need test class for this trigger
Can anybody help me i need test class for this trigger..
trigger testopp1 on Opportunity (before insert,before update) {
for(Opportunity opp : Trigger.new){
System.debug('****************************'+opp.recordtypeid);
String rectype=[select id,name from RecordType where name =: 'Rec Type1'].id;
System.debug('********convert id into sting************'+rectype);
if(opp.StageName =='Prospecting' && opp.Probability == 10 && opp.recordtypeid == rectype){
if(opp.Amount == null){
opp.Amount.addError('****enter amount*****');
}
}
}
}
@isTest (SeeAllData = true)
public class testopp1Test {
static testMethod void testopp1 () {
Opportunity opp1 = new Opportunity();
opp1.name = 'Test 1';
opp1.RecordType = [SELECT Id,Name FROM RecordType WHERE SObjectType = 'Opportunity' and Name = 'Rec Type1'];
opp1.Type = 'New Customer';
opp1.Amount = 10;
opp1.CloseDate = system.today();
opp1.StageName = 'Received';
Opp1.Amount=100;
INSERT opp1;
try
{
opp1.Amount=null;
update opp1;
//throw new MyException('An exception should have been thrown by the trigger but was not.'); // 1. If we get to this line it means an error was not added and the test class should throw an exception here. 2. MyException class extends Exception.
}
catch(Exception e)
{
Boolean expectedExceptionThrown = e.getMessage().contains('My Error Message') ? true : false;
System.AssertEquals(expectedExceptionThrown, true);
}
}
}
Here is an alternative solution:
I have also refactored the original trigger to move the SOQL query out of the for loop:
If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!
-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator
Hi Glyn it shows an Error.....
Tell me the error message, including line number, please.