function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DatajunkieDatajunkie 

Need help with test code

First off, I confess to being a complete newbie when it comes to Apex development. So I set up a code consult at Dreamforce, and was thrilled to get the help of two wonderful guys, who were able to show me how to write a really simple, basic trigger that we badly need. They also told me that before I could deploy it, I had to have a test class first. I already had a test class in my Sandbox (and in production, for that matter) written by Astadia for a previous project. It appeared to work fine as a test, so they told me all I needed to do was go home and deploy both the new trigger and the test class. Unfortunately, when I tried to do that, the deployment failed - with four errors. Two were new validation rules that weren't in place the last time my Sandbox had been refreshed (and that only applied to the functions called by the test class, not those in my new trigger), and two others were errors I don't understand, but seem to be telling me that my test code isn't sufficient. Namely:

 

*OpportunityAfterUpdate - Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

*Deploy Error - Average test coverage across all Apex Classes and Triggers is 20% at least 75% test coverage is required

 

BTW - I'm trying to deploy the code from the Sandbox using the new Deploy Change Sets function. Maybe the problem is that I have to use a different tool? Honestly, I'm kind of clueless.

 

So it looks like I need new test code, but I'm at a total loss for where to even start. My trigger is really pretty small and only calls a few functions, so it seems like my test code should only need to be simple too, but like I said - I'm at a total loss for where to even start. I'm hoping one of you can help me out!

 

My trigger simply creates a new record in a related list whenever a new Opportunity is created with a certain record type. We have 3 record types, and the only one that this should *not* fire for is called "ADM Opportunity", so we used that as a "not equals" instead of specifying two types for "equals".  The related object is called shared_sale__c, and only 3 fields need to be filled in with the new record. The opportunity_ss__c field (which is the master-detail relationship), the rep__c field (lookup to user table, populated with the owner of the opportunity) and the percent_share__c field (set to 100). I've copied the full trigger below, if that helps.

 

What all should I put in the test code? The one created by Astadia actually creates a new account, then a new related opportunity, and then populates a bunch of fields. And apparently that's not sufficient to test my new trigger.

 

Any help, advice, tips - anything at all - would be welcome!

 

trigger OpportunitySplitTrigger on Opportunity (after insert) {

list <recordtype> soqlret = [select id from recordtype where name = 'ADM Opportunity'];

id nosplitid = null;
if (soqlret.size()>0){
nosplitid = soqlret[0].id;
}

list<shared_sale__C> listOpportunitysplits = new list<shared_sale__C>();

for (Opportunity o :trigger.new){

if (o.recordtypeid != nosplitid){

shared_sale__C ss = new shared_sale__C();
ss.percent_share__c =100;
ss.rep__c = o.ownerid;
ss.opportunity_ss__c = o.id;

listOpportunitysplits.add(ss);
}
}
insert listOpportunitysplits;

}
bob_buzzardbob_buzzard

Your two additional errors are related.  The first indicates that your test class doesn't carry out an insert of an opportunity, so your trigger doesn't fire under test conditions, resulting in 0% coverage.

 

The second is that you can't deploy a trigger to production without some unit test coverage, and due to the message above, you have 0% coverage.

 

You have a choice between extending your existing test class or creating a new one.

 

It looks to me like you'll need to insert an opportunity that has a record type != 'ADM Opportunity', and then check that a Shared_Sale__c object has been created.

 

Something like the following should do it:

 

 

private static testMethod void testOpp()
{
  list <recordtype> oppRTs = [select id from recordtype where name =! 'ADM Opportunity' and SobjectType='Opportunity'];
  if (list.size()>0)
  {
     Opportunity opp=new Opportunity (Name='Unit Test', RecordTypeId=oppRTs[0], CloseDate=System.today());
     insert opp;

     List<shared_sale__C> ss=[select id, opportunity_ss__c from shared_sale__c where opportunity_ss__c=:opp.id];
     System.assertEquals(1, ss.size());
  }
}