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
SampathSampath 

Apex Test Coverage problem

Hi,
     Am new to salesforce apex code.
 I wrote a trigger in sandbox login it was working fine. But when i put in the production it is throwing error like "Test coverage of selected Apex Class and Trigger is 0%, at least 75% test coverage is required"
 
 My trigger is a before insert trigger.
 
 Its logic is to fetch substring of Account name, billstreet and postal code and add them and put it in the new custom field.
 
Can anyone of help me to find what is the problem in my apex trigger and class.
 
 My trigger is as follows.

trigger Dummy on Account (before insert)

{

for (Account a : Trigger.new)

{

String Card = '';

String tempAccount = a.Name;

String tempBllStreet = a.BillingStreet;

String tempPstlCode = a.BillingPostalCode;

If(tempAccount != null)

{

tempAccount = tempAccount.toUpperCase();

 tempAccount = tempAccount.replaceAll('[\\x00-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\x7f\\x80-\\xfe]','');

String AccountName = tempAccount;

if(tempAccount.length() > 5)

AccountName = tempAccount.substring(0,5); Card = AccountName;

}

If(tempBllStreet != null)

{

tempBllStreet = tempBllStreet.toUpperCase();

 tempBllStreet = tempBllStreet.replaceAll('[\\x00-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\x7f\\x80-\\xfe]','');

String BillingStreet = tempBllStreet;

if(tempBllStreet.length() > 4)

BillingStreet = tempBllStreet.substring(0,4);

Card = Card + BillingStreet;

}

If(tempPstlCode != null)

{

tempPstlCode = tempPstlCode.toUpperCase();

tempPstlCode = tempPstlCode.replaceAll('[\\x00-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\x7f\\x80-\\xfe]','');

String PostalCode = tempPstlCode;

if(tempPstlCode.length() > 6)

PostalCode = tempPstlCode.substring(0,6);

Card = Card + PostalCode;

}

If(Card != '')

a.CardCode__c = Card;

else

a.CardCode__c = a.Name;

}

}

 

and my apex class for test coverage is as follows

public class AccountTest

{

static testMethod void myTest()

{

Account[] a = new Account[]{new Account(Name = 'Dave',BillingStreet = 'MyStreet1',BillingPostalCode = '123456'),

new Account(Name = null,BillingStreet = 'MyStreet2',BillingPostalCode = '123457'),

new Account(Name = 'Dave',BillingStreet = null,BillingPostalCode = '123456'),

new Account(Name = 'Dave',BillingStreet = null,BillingPostalCode = null)};

insert a;

}

}

can any one of pls help me.



Message Edited by Sampath on 04-22-2008 10:29 PM
JonPJonP
You can write any Apex classes triggers in a Developer Edition or Sandbox org, but there are stringent requirements on production environments.  In order to deploy this trigger, you'll need to create an Apex class with one or more TestMethods that execute your code and test that its outputs are correct.  Code Coverage measures how much of your code is exercises by your test methods.

To execute your Account trigger, you'll need to insert an Account within a test method.  (DML statements executed inside test methods are not committed, so your production data will not be altered.)

Once you've written suitable tests to meet or exceed the code coverage minimums, you will want to deploy your trigger and the class(es) containing its test methods all at once.

See the Apex Code documentation for details.
SampathSampath
Hi jonP,
            Thanks for your reponse.I tried in various ways even though am getting the same error.
 
I want to know how to write the test cases with full coverage. And also i want to know is there any thing specific to "BEFORE INSERT " trigger i want to add in my testcases.
 
Can anyone help in giving the test class framework for my trigger stated above....
 
Thanks in advance...
 
            
JonPJonP
To get full coverage, you must supply test data that exercises all your conditional logic.  For example, for every if/else in your trigger, you must insert records to test both cases.  However, if your trigger code coverage is still showing 0% when you run your tests, it means your test method isn't causing your trigger to be called at all.  Make sure that's working before worrying about testing every line of trigger code.
justinpauldavis10justinpauldavis10

I saw your response to this posting, and I'm hoping you might be able to help me, I'm new to Apex.

 

I have a very simple lookup-field trigger that places a record ID in that field, on a custom "Accounts" object, not the standard object:

 

trigger SetAccountField on Account__c (before insert, before update) {
    for (Account__c Account__c : Trigger.new) {
        a.Scoring__c = 'a00A0000003MnLw';
    }
 }


How can I create a test class, for something like this? Can you give me an example of the best way to test something this simple?