You need to sign in to do that
Don't have an account?
Harmpie
I got a few questions on this:
Question on triggers and testmethods
See my trigger:
Code:
trigger copyCursusDataToOpp on Cursusinschrijving__c (after insert, after update) { if(System.Trigger.isUpdate) { for(Cursusinschrijving__c ci : Trigger.new) { if(ci.Via_opportunity__c!=null) { Opportunity Opp = [SELECT Id,Name FROM Opportunity WHERE Id = :ci.Via_opportunity__c]; Cursus C = [SELECT Id,Cursusdagen__c FROM Cursus__c WHERE Id = :ci.Cursus__c LIMIT 1]; Opp.Cursusdata__c = C.Cursusdagen__c; Opp.Cursus__c = ci.Cursus__c; update Opp; } } } else if(System.Trigger.isInsert) { for(Cursusinschrijving__c ci : Trigger.new) { if(ci.Via_opportunity__c!=null) { Opportunity Opp = [SELECT Id,Name FROM Opportunity WHERE Id = :ci.Via_opportunity__c]; Cursus C = [SELECT Id,Cursusdagen__c FROM Cursus__c WHERE Id = :ci.Cursus__c LIMIT 1]; Opp.Cursusdata__c = C.Cursusdagen__c; Opp.Cursus__c = ci.Cursus__c; update Opp; } } } static testMethod void testTrigger() { Cursus__c tc = new Cursus__c(); insert tc; Cursusinschrijving__c tci = new Cursusinschrijving__c(); Opportunity to = new Opportunity(); to.Name = 'testopp'; insert to; tci.Via_opportunity__c = to.Id; tci.Cursus__c = tc.id; insert tci; System.assertEquals(to.Cursus__c, tc.Id); } }
I got a few questions on this:
1) Why does ci.Cursus__r.Cursusdagen__c return null in the first parts, while the query to the related object (as in the example) DOES return a value?
2) Saving the code above gives an error: Compile Error: Only top-level class methods can be declared static at line 2 column 748. Can someone tell me how to write testmethods on triggers?
3) When trying to deploy the trigger (without any testcoverage at that time), selecting only the trigger to be deployed in the deploymentplan using Eclipse, I will receive an error, complaining about testcoverage for multiple APEX components (while only the trigger is selected in the deployment plan !!), because the trigger is untested, and my other APEX class has minimal testcoverage (78%), the average of coverage drops below 75%, making deployment impossible. Why does eclipse take unselected component's testcoverage into account ?
test methods must be in a class, as the message indicates, please create a class for this test method (not inside a trigger)
you must select a trigger and the class that tests it in the deploy plan.
I think I know what`s causing this, but I don`t see why my test would fail over it. In the system I am testing with, a workflow updates the Opportunity name at save or update time with an autonumber value. This seems to be happening in this case too.
so your custom field validation is fireing and preventing your test from creating an opportunity, i think
Message Edited by Harmpie on 03-06-2008 08:48 AM