+ Start a Discussion

Trigger Test Class woes.

Still cutting my teeth on a test method of a reasonably simple trigger I've brought up in the past.

The trigger works fine in sandbox, but doesn't seem to work during the test method, I'm at the end of my wits with it so was hoping somebody with more insight might be willing to check it out:

public class TestTriggers {
  static testMethod void myTest() {
   Date futureDate = date.newInstance(2050, 6, 6);
    Account account = new Account(name='testAccount', OfficeOwningAccount__c='VIC', Industry='Defence');
    insert account;
    account = [select name from Account where name=:account.name];
    Opportunity opp = new Opportunity(name='testOpp', accountID=account.ID, StageName='1.0 - Territory', CloseDate = futureDate);
    insert opp;
    opp = [select ID, name, StageName, Primary_Contact__c, PricingMethod__c from Opportunity where ID=:opp.ID];
    Contact contact = new Contact(lastname='testContact', accountID=account.ID);
    insert contact;
    contact = [select name from Contact where LastName=:contact.lastname];
    OpportunityContactRole oppContact = new OpportunityContactRole(IsPrimary = true, ContactID=contact.ID, OpportunityID=opp.ID);
    insert oppContact;
    oppContact = [select Id, isPrimary from OpportunityContactRole where OpportunityID=:oppContact.OpportunityID and ContactID=:oppContact.ContactID];
    Practice_Service_Offering__c practiceService = new Practice_Service_Offering__c(Opportunity__c = opp.ID, of_Sale__c = 100);
    insert practiceService;
    practiceService = [select Id from Practice_Service_Offering__c where Opportunity__c = :opp.ID];
    update opp;
    opp.name = 'testOppUpdate';
    opp.StageName = '7.0 - Win';
    opp.ENGAGEMENTSTARTDATE__C = futureDate;
    opp.ENGAGEMENTCLOSEDATE__C = futureDate;
    opp.HOURS_PER_DAY__C = 8;
    opp.PRICINGMETHOD__C = 'Fixed-Price';
    opp.AMOUNT = 20000;
//This info is all required fields for something at Stage 7
//When something goes to stage 7, IsClosed switches to true
    update opp;
    opp = [select ID, name, IsClosed, StageName, Primary_Contact__c, PricingMethod__c from Opportunity where ID=:opp.ID];
//Assertions go here:
    System.assert(account != null);
    System.assert(contact != null);
    System.assert(oppContact != null);
    System.assert(practiceService != null);
    System.assert(opp.PricingMethod__c == 'Fixed-Price');
    System.assert(opp.name == 'testOppUpdate');
    System.assert(opp.Primary_Contact__c != null);
     try {
       delete opp;
       delete account;
       delete oppContact;
       delete contact;
       delete practiceService;
     catch (DmlException e) {

The trigger in question is here
trigger SetPrimaryContact on Opportunity (before update) {

    for (Opportunity o : Trigger.new) {
     //innitialise variables
     OpportunityContactRole contactRole;
     //Check that the opportunity is being closed out
      if (o.IsClosed && !Trigger.oldMap.get(o.id).IsClosed) {
     contactRole = [select ContactID from OpportunityContactRole where IsPrimary = true and OpportunityId = :o.id];
      //if something is returned from the list:
      //get the primary contact associated with opp and assign the id to primary contact field
    if(contactRole != null){
    o.Primary_Contact__c = contactRole.ContactID;

Idea is simple, if an opportunity is closed out, then it pulls the information from related list into the opportunity field. This is a work around due to us having mail merges going out which should contain info from the related list section.

There are 3 failed assertions in the above:
System.assert(false); //from catch statement
System.assert(opp==null); //from just after catch statement (obviously it seems to be having some problems deleting opp)
//The big one
System.assert(opp.Primary_Contact__c != null);

I lightly considered commenting out the assertions and hoping for the best, but something tells me, our Best Practice Rep. would try and choke me out for that.

may I also use this opportunity (no pun intended) to point out that
:manvery-happy: and :womanmad: are phenominally creepy.


Nonick -

Are you doing the final delete to clear out the data that the test method added?  You know you don't have to do this - any data manipulation done in a test method is not committed to the database.

Also, I don't see where you are setting the Primary_Contact__c on your Opportunity record, so why shouldn't it be null?

Hope this helps.
The trigger fires on opp update, which should have the following happen.
o.Primary_Contact__c = contactRole.ContactID;
This trigger DOES work in Dev environment.
Free Image Hosting at www.ImageShack.us
Changes to Free Image Hosting at www.ImageShack.us

Also, I 'm aware that stuff in test method doesn't commit to the db,
but I'd like to since I'll need to do it in other production methods,
and would like to confirm that it works.
(names deleted to defend the shy)

IT WORKED, apparently the test coverage was 100% when I removed those 3 assertions causing problems, unfortunately discovered a flaw, in the trigger logic since I don't want it updating for stages . So my test percentage and problem is still unresolved. Atleast I know the trigger works.

Message Edited by Nonick on 11-19-2008 06:53 PM

Message Edited by Nonick on 11-19-2008 07:16 PM
So all things indicate that one of the main problems is that I'm trying to upload to  the production environment, I think that I can't upload the test class because it's running the test on the current trigger there, which doesn't complete the task tested.

I can't upload the trigger because it's been tested against  the existing test method doesn't satisfactorally test the new trigger.

Might this be the issue?

I'm basing this on the test number being unresponsive to me modifying the trigger, and the running test not reporting on system.debug calls made in the trigger.


Hmm...interesting, seems like a discrepancy between eclipse and the server. Something goes through the save to server process in eclipse, but didn't update on the page until 5 min later.

Message Edited by Nonick on 11-19-2008 11:04 PM

Hi There,

It looks like I'm having a similar issue - were you able to resolve this by first removing the old test classes and then doing the deploy of the classes and triggers?     I get 100% coverage in the sandbox on all my tests.  However on the deployCodeCheckOnly - it returns a failure listing the names of only the two previous test classes in production.   I had changed the triggers and found it necessary to expand on the original test classes so now they are at different version levels.   Wouldn't the deploy override and always use the test class from the most recent version.  

So can I am assume I can remove the test classes in production and it will then allow me to replace them.

Please let me know if i am on the right track?

Thanks Jackie