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
shan876shan876 

Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required?????

I have a class and a trigger associated with the class... Now, I want to deploy it and I tested it out with a couple of accounts in sandbox where i have the trigger and class... But i keep getting the error Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required... when i try to deploy it from eclipse...
What should I do? How do you test this?
Best Answer chosen by Admin (Salesforce Developers) 
shan876shan876
Well, yes I was able to do everything before the summer upgrade...
Anyhow, I wrote my trigger and then for my class i wrote the testmethod....
and it seem to have worked...
This is what I got from the SF developer... who was pretty cool
Code:
Trigger:

trigger PopulateTitle on Contact (after update) {   
     for (Contact c : trigger.new) {   
     Account a = [select id, type from Account where id = :c.AccountId];
     c.Title = a.Type;
}
}

Class:

public class TestPopulate  {
           private static testMethod void myShareTest(){
                Contact C = new Contact();
                C.FirstName = 'Darren';
                C.LastName = 'Kemp';
                insert C;
                C.AccountID = '0015000000LwRkD';        
        
                 try{
                    update C;
                    Account a = [select id, type from Account where id = :C.AccountID];
                    System.assertEquals(C.Title,a.Type);
            
        } catch (System.DmlException e){
            System.debug('we caught a dml exception: ' + e.getDmlMessage(0));    
        }
    }
}

 
thanks
shan
 

All Answers

jrotensteinjrotenstein
Have you created any TestMethod procedures to test the Trigger?If so, do they work in your sandbox?
MyTwoCentsMyTwoCents
I am having the same issue. Downloaded Eclipse and installed force.com IDE on June 23, 2008. My legacy trigger that is in production will not pass either. Any help appreciated.
 
Code:
trigger mktModel2PlanTrigger on MktModel2Plan__c (before insert, before update) {
  MktModel2Plan__c[] mm2p = Trigger.new;
  MktModel2Plan.setType(mm2p);
}

public class MktModel2Plan {
  public static void setType( MktModel2Plan__c[] mm2p) {
    for ( MktModel2Plan__c i:mm2p ) {
      ID key = i.Plan__c;
      // all variables in select must be preceeded with a :
      Plan__c p = [select id, Type__c from Plan__c where id = :key];
      i.Type__c = p.Type__c;      
    }
  }
  // 75% unit test coverage to deploy to production org
  static testMethod void testMktModel2Plan() {
     //create a new Market Model
     Mkt_Model__c mm = new Mkt_Model__c();
     mm.Year__c = '2008';
     mm.Industry__c = 'Test';
     mm.Application__c = 'Test';
     mm.Priority__c = 'Test';
     mm.Description__c = 'Test';
     mm.CurrencyIsoCode = 'USD';
     insert mm;
     
     //create a new Plan__c
     Plan__c p = new Plan__c();
     p.Type__c = 'Sales';
     insert p;
          
     //create a new MktModel2Plan__c
     MktModel2Plan__c mm2p = new MktModel2Plan__c();
     mm2p.Mkt_Model__c = mm.id;
     mm2p.Plan__c = p.id;
     insert mm2p;
     
     //assert
     ID key = mm2p.id;
         MktModel2Plan__c val = [select id, Type__c from MktModel2Plan__c where id = :key];
         System.assert(val.Type__c == 'Sales');
  }
}

 
shan876shan876

Thank you for replying back to me...

So I have two questions:

1. What is actually System.AssertEquals?

2. For my class here how would my testmthod look like?

CLASS:

public class account_site_update 
{
// This class updates the Account Site field on 
//account records that are
// passed to it.
public static void addacctsiteupdate(Account[] accs)
{
for (Account a:accs) 
{
  if (a.Account_Status_Description__c=='ON SERVICE' || a.Account_Wholesaler_Status_Description__c=='ON SERVICE')
    {
            a.Site = 'ON SERVICE';
    }
        else 
        {
            a.Site = 'OFF SERVICE';
        }
}
}
}
TRIGGER
trigger account_site_insert on Account (before insert, before update) {
 Account[] accs = Trigger.new;

 account_site_update.addacctsiteupdate(accs);

}
Thanks
Shan
MyTwoCentsMyTwoCents
I am not going to rule out my code but this may be an bug with the new Force.com IDE. I have an open ticket with Salesforce.com and I am hopeful I will recieve an answer soon. I was able to deploy my code to the production server with all tests passing. I only receive the error when when I choose 'run tests' from the class or trigger. Will post later what I find out from Salesforce.com.
SFDCRKSFDCRK
Hey, any update on this issue? I am getting the same error message, eventhough after executing the test methods I get 80%, when I try to deploy (using Eclipse) I get "Average test coverage across all Apex Classes and Triggers is 0%, at least 75% test coverage is required". Keep in mind I was able to install a Trigger/Class last week with out any issue. Don't know why. Any help is greatly appreciated...
--RK
shan876shan876
Well, yes I was able to do everything before the summer upgrade...
Anyhow, I wrote my trigger and then for my class i wrote the testmethod....
and it seem to have worked...
This is what I got from the SF developer... who was pretty cool
Code:
Trigger:

trigger PopulateTitle on Contact (after update) {   
     for (Contact c : trigger.new) {   
     Account a = [select id, type from Account where id = :c.AccountId];
     c.Title = a.Type;
}
}

Class:

public class TestPopulate  {
           private static testMethod void myShareTest(){
                Contact C = new Contact();
                C.FirstName = 'Darren';
                C.LastName = 'Kemp';
                insert C;
                C.AccountID = '0015000000LwRkD';        
        
                 try{
                    update C;
                    Account a = [select id, type from Account where id = :C.AccountID];
                    System.assertEquals(C.Title,a.Type);
            
        } catch (System.DmlException e){
            System.debug('we caught a dml exception: ' + e.getDmlMessage(0));    
        }
    }
}

 
thanks
shan
 
This was selected as the best answer
MyTwoCentsMyTwoCents
Log into your sandbox or dev account. Try 'Run All Tests' from Develop | Apex Classes. Go back to Eclipse and 'Run Tests'.
 
I now cannot reproduce the "Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required" error. I did not change any code.
hassabhassab

I know this is an old thread, but anyone still having this problem?  I have run all tests, I'm at 87%, and when I run in Apex I get 0%.

ashwaksyedashwaksyed

i am getting same error in trigger , what can i do for that so kindly let me know the soution.

 trigger:

trigger Sampletest on Student__c (before insert) {
 Student__c[] studentsList = Trigger.new;     
    Set<String> emailSet = new Set<String>();    
       for(Student__c s : studentsList)      
         {       
               emailSet.add(s.Email__c);       
          }          //Get list of duplicate Students   
               List<Student__c> duplicateStudentList = [Select s.Name, s.Email__c From Student__c s where s.Email__c IN :emailSet];      
                   Set<String> duplicateEmailIds = new Set<String>();
                            for(Student__c s : duplicateStudentList)     
                               {       
                                     duplicateEmailIds.add(s.Email__c);      
                                       
                                   }         
                                    for(Student__c s : studentsList)     
                                       {          
                                              if(duplicateEmailIds.contains(s.Email__c))         
                                                  {             
                                                          s.Email__c.addError('Record already exist with same email Id');     
                                                    }                                                     
            }
}

 

class:

@isTest
private with sharing class TestTriggers {
    static testMethod void myUnitTest() {
        Student__c s = new Student__c();
        s.Name = 'Om Test';
        s.l_Name__c = 'LastName';
 
        Course__c c = new Course__c();
        c.Name = 'SFDC';
        c.Fees__c = 2000;
        
    //    insert c;
 
      //  s.Course__c = c.Id;
       // s.Installment_1__c = 2000;
        //s.Email__c = 'admin@shivasoft.in';
        try
        {
            insert s;
        }
        catch(System.DMLException e)
        {
            System.assert(e.getMessage().contains('Record already exist with same email Id'));
        }
    }
}

 

 

ashwaksyedashwaksyed

Hi,How to delete Trigger and apex Class in salesforce Enterprise edition using Eclipse and let me know the solution of this as soon as possible.

Anil DuttAnil Dutt

I m getting same error

 

"Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required"

Here is my trigger

 

trigger FlightRequestTrigger on Flight_Request__c (after insert) {

    List<Opportunity> listOpp = new List<Opportunity>();
    boolean flag = true;
    for (Flight_Request__c fr: trigger.new)
    {
        Opportunity oppNew = [SELECT Id,StageName FROM Opportunity WHERE Id =:fr.Opportunity__c];
        if(oppNew.StageName == 'To Be Searched')
        {
            oppNew.StageName = 'Search';
            listOpp.add(oppNew);
        }
    }
    if (listOpp != null && !listOpp .isEmpty())
    {
       Database.update(listOpp);
    }
}

 

 

And this is the test case

 

@isTest
public  class FlighRequestTestCase {
   private  static testMethod void myUnitTest() {
        Opportunity oppNew =  new Opportunity();
        oppNew.Name = 'Test Opp';
        oppNew.StageName = 'To Be Searched';
        oppNew.CloseDate = System.now().date();
        insert oppNew;
        
        Flight_Request__c fr = new Flight_Request__c();
        fr.From__c ='Ani';
        fr.To__c ='Ani';
        fr.Opportunity__c = oppNew.Id;
        insert fr;
          
           List<Opportunity> listOpp = new List<Opportunity>();
        oppNew =  [SELECT Id,StageName FROM Opportunity WHERE Id =:fr.Opportunity__c];
           if(oppNew.StageName == 'To Be Searched')
        {
            oppNew.StageName = 'Search';
            listOpp.add(oppNew);
        }
        
        if (listOpp != null && !listOpp .isEmpty())
           {
              Database.update(listOpp);
           }
    }
}

 

Please help me, what i m doing wrong here?