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

Test Class for a simple trigger
Hello,
Despite creating S-Controls for our organization for several years we just started to create our first Apex Triggers and immediatly run into the "Test Classes"-problem. We created the following simple trigger which is working in the Sandbox but cannot transfer it to Production Environment without a Test Class.
When a user tries to set the Opportunity Stage to "Won", the trigger checks if there is a quotation file among opportunity attachments and if not, prevents saving the record with an error message. Here is the code:
trigger CheckOppAttachments on Opportunity (before update) { if (trigger.new[0].StageName == 'Closed Won') { Boolean pdf = false; Boolean pdfloop = false; Opportunity opp = trigger.new[0]; Attachment[] atts = [SELECT Name FROM Attachment WHERE ParentId =: opp.Id]; for (Integer i = 0; i < atts.size(); i++) { pdfloop = atts[i].Name.contains(opp.Quotation_No__c); if (pdfloop) { pdf=true; } } if (pdf) {} else { opp.addError('The Stage can not be set to "Won" as long Quotation is not attached'); } } }
Any example of a test class for such a trigger would be appreciated.
@isTest
private class Tmethod {
static testMethod void UnitTest(){
Opportunity lstOpp = new Opportunity(Name='testopp',stage='Open');
insert lstOpp;
lstOpp.Stage='Closed Won';
Update lstOpp;
}
}
This can be a starting point
All Answers
@isTest
private class Tmethod {
static testMethod void UnitTest(){
Opportunity lstOpp = new Opportunity(Name='testopp',stage='Open');
insert lstOpp;
lstOpp.Stage='Closed Won';
Update lstOpp;
}
}
This can be a starting point
Many thanks for your help, we just had to slightly extend your code (creation of account and opp with required values) and it worked immediatly. Unfortunately after executing the test the code coverage displayed is 68%, while at least 75% are necessary do deploy. The message displayed is
which is exaclty the desired error message the trigger should produce when the file is not attached. However this prevents the code from being deployment. Here is the test code after the necessary modifications
After some googling it seems the test has to cover both cases: when an opportunity has the files and the case when it hasn't. But it seems impossible to create files and attach them with APEX. Is it possible to update existing opportunities with a test class instead of creating new? I tried but it did not worked.
Is there an other possibility to rework the test class or the trigger code to reach 75%?
Thank you very much for your help, with your hint I managed to get 100% coverage with the following code.
However I still can't deploy it because despite 100% coverage the test run ends with a failure and the message
So the actually desired error message prevents the trigger from being deployed.
Is the method "addError('');" therefore unusable in Production environment?
Is there a way to circumvent it?
OK, the solution for the last problem was to use try/catch.
The following test class finally delivers 100% without a failure.
Many thanks to j020, without your help test classes would still be forbidden land for me.