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
Anil DuttAnil Dutt 

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

I m getting following 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?

Best Answer chosen by Admin (Salesforce Developers) 
Rajesh SriramuluRajesh Sriramulu

Hi

 

try this it will getting 100%.

@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;
          
         }   
}


All Answers

Rajesh SriramuluRajesh Sriramulu

Hi

 

try this it will getting 100%.

@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;
          
         }   
}


This was selected as the best answer
ClintLeeClintLee

Try making your test class private.  I also updated the test method.  Let me know if this works for you.

 

@isTest

private class FlightRequestTestCase {

 

              static testmethod void myUnitTest() {

                         Opportunity oppNew = new Opportunity( Name            = 'Test Opp'

                                                                                                   StageName = 'To Be Searched'

                                                                                                   CloseDate    = System.today() );

                         insert oppNew;

 

                         Flight_Request__c fr = new Flight_Request__c( From__c             = 'Ani'

                                                                                                                 To__c                 = 'Ani'

                                                                                                                 Opportunity__c = oppNew.Id );

                         insert fr;

 

                         //  Assert that the trigger actually worked.

                         Opportunity opp = [ select StageName from Opportunity where Id = :oppNew.Id limit 1 ];

                         System.assertEquals( 'Search', opp.StageName );                    

              }

}

 

Hope that helps!

 

~ Clint

Anil DuttAnil Dutt

 @SRS8

 

System.assertEquals() is not required to check output of test case?

 

 

Rajesh SriramuluRajesh Sriramulu

Hi

 

if u getting 100% code here then no need to have we can directly insert those values.

ofcourse it is one of the best practice to use in test classes.

 

 

Let me know any issue and if it solves ur problem plz accept it solution.

Anil DuttAnil Dutt

@SRS8, Yes its works, but giving error for following trigger

 

Please suggest Test case for following trigger also

 

trigger FFTrigger on FF__c (after insert) {
    List<Opportunity> listOpp = new List<Opportunity>();
    boolean flag = true;
    for (FF__c ff: trigger.new)
    {
        if (ff.Account_Type__c == 'ANA' && ff.DOB__c == null)
        {
            ff.DOB__c.addError('DOB is required');
            flag = false;
        }
        if (ff.Account_Type__c == 'DL' && ff.Last_Name__c == null)
        {
            ff.Last_Name__c.addError('Last Name is required');
            flag = false;
        }
        if(flag)
        {    
            //Opportunity oppNew = new Opportunity(Id = ff.Opportunity__c);
            Opportunity oppNew = [SELECT Id,StageName FROM Opportunity WHERE Id =:ff.Opportunity__c];
            if(oppNew.StageName == 'Ticketing')
            {
                oppNew.StageName = 'FF Account Assigned';
                listOpp.add(oppNew);
            }
        }
    }
    if (listOpp != null && !listOpp .isEmpty())
    {
       Database.update(listOpp);
    }
}

 

Test case is

 

@isTest
private class FFTestCase {
    private static testMethod void myUnitTest() {
        Opportunity oppNew =  new Opportunity();
        oppNew.Name = 'Test Opp';
        oppNew.StageName = 'Ticketing';
        oppNew.CloseDate = System.now().date();
        insert oppNew;
        
        FF__c ff = new FF__c();
        ff.Account_Number__c ='1234456';
        ff.Account_Type__c = 'ANA';
        ff.DOB__c = System.now().date();
        ff.Opportunity__c = oppNew.Id;
        ff.Last_Name__c = 'Dutt';
        insert ff;
          
          List<FF__c> ffList = new List<FF__c>();
         ffList.add(ff);
         
           List<Opportunity> listOpp = new List<Opportunity>();
            for(FF__c f : ffList)
        {
            oppNew =  [SELECT Id,StageName FROM Opportunity WHERE Id =:f.Opportunity__c];
            try
            {
                boolean flag = true;
                if (ff.Account_Type__c == 'ANA' && ff.DOB__c == null)
                {
                    ff.DOB__c.addError('DOB is required');
                    flag = false;
                }
                if (ff.Account_Type__c == 'DL' && ff.Last_Name__c == null)
                {
                    ff.Last_Name__c.addError('Last Name is required');
                    flag = false;
                }
            
                if(flag)
                {    
                    if(oppNew.StageName == 'Ticketing')
                    {
                        oppNew.StageName = 'FF Account Assigned';
                        listOpp.add(oppNew);
                    }
                }
                
                if (listOpp != null && !listOpp .isEmpty())
                {
                   Database.update(listOpp);
                }
                System.assertEquals('FF Account Assigned',oppNew.StageName);
               }
               catch (System.DmlException e){
                System.debug('we caught a dml exception: ' + e.getDmlMessage(0));    
            }
        }
    }
}

Rajesh SriramuluRajesh Sriramulu

Hi

 

Try this code

 

@isTest
private class FFTestCase {
    private static testMethod void myUnitTest() {
        Opportunity oppNew =  new Opportunity();
        oppNew.Name = 'Test Opp';
        oppNew.StageName = 'Ticketing';
        oppNew.CloseDate = System.now().date();
        insert oppNew;
        
        FF__c ff = new FF__c();
        ff.Account_Number__c ='1234456';
        ff.Account_Type__c = 'ANA';
        ff.DOB__c = null;
        ff.Opportunity__c = oppNew.Id;
        ff.Last_Name__c = 'Dutt';
        insert ff;
          
           FF__c ff1 = new FF__c();
        ff1.Account_Number__c ='1234456';
        ff1.Account_Type__c = 'DL';
        ff1.DOB__c = System.now().date();
        ff1.Opportunity__c = oppNew.Id;
        ff1.Last_Name__c =null;
        insert ff1;
          Boolean flag= true;
        }} 

Regards,

S Rajesh.

Anil DuttAnil Dutt

@SRS8, test case fail

 

error is "Custom validation exception DOB is required"

 

and Code Covrage is 56%

 

Rajesh SriramuluRajesh Sriramulu

Hi

 

what i sent to u plz replace system.today() on DOB and can u post the line in trigger which r not covered.

 

Regards,

S Rajesh.

 

Anil DuttAnil Dutt

@SRS8

 

Thanks aloft for your help

 

I set DOB = system.today() and LastName = 'D'

then test run successfully

 

But

 

When i run FlighRequestTestCase it says

 

FFTrigger -- 16 lines not tested, 0% covered

FlightRequestTrigger -- 100% covered

FlighRequestTestCase -- 100% covered

 

AND

When i run FFTestCase it says

 

FlightRequestTrigger -- 9 lines not tested, 0% covered

FFTrigger -- 9 lines not tested, 75% covered

FFTestCase -- 100% covered

 

 

Any idea?

 

AmitSahuAmitSahu

try 

@isTest (seeAlldata=true)

Anil DuttAnil Dutt

@j020

 

sorry , it didn't work

Rajesh SriramuluRajesh Sriramulu

Hi

 

there are 2 triggers right? and both having code coverage 100%?

 

can i have the screen shot plz.

 

 

Anil DuttAnil Dutt

@SRS8

 

yes , there are 2 triggers with two test cases

 

When i run FlighRequestTestCase it says

 

FFTrigger -- 16 lines not tested, 0% covered

FlightRequestTrigger -- 100% covered

FlighRequestTestCase -- 100% covered

 

AND

When i run FFTestCase it says

 

FlightRequestTrigger -- 9 lines not tested, 0% covered

FFTrigger -- 9 lines not tested, 75% covered

FFTestCase -- 100% covered

 

 

 

Not sure how to actach image

 

 

 

 

 

Rajesh SriramuluRajesh Sriramulu

Hi

 

Send ur gmail id and i will add in google+.

Anil DuttAnil Dutt

anil@swiftsetup.com

Rajesh SriramuluRajesh Sriramulu

Hi

 

run the each test class seperately in  User Interface and i think u r running in eclipse so it will show like that no problem ur both trigger having 100% u can deploy to PD.

Anil DuttAnil Dutt

@SRS8

 

yes you ar right, i m using eclippse , triggers and test deployed to Force.com with 100% and 75% coverage

 

So it is eclipsse behaviour which was creating problem

 

Thanks alot for you help

Rajesh SriramuluRajesh Sriramulu

Hi

 

Good to hear  :)

 

 

 

Make it as solution so that some one will get use of it.