0
@istest(SeeAllData=false)
public class Testclassforaddingpli {

    //public list<Proposal_Line_Item__c> prolist = new  list<Proposal_Line_Item__c>();

    @istest 
    public static void Testclassforaddingpli(){

        chapter__C chp = new chapter__C();
        chp.name ='test1';
        chp.TotalLinePrice__c =1000;
        chp.TotalTrucks__c =1000;
        insert chp;

        Proposal_Line_Item__c pli = new Proposal_Line_Item__c ();
        pli.Name=  'raj' ;
        pli.chapters__c = chp.Id ;
        pli.LineNumber__c=1000;
        pli.NumberofTrucks__c=1000;
        insert pli;  
        update pli;
        delete pli;
    }

    @istest
    private static void Testclassforaddingpli2(){

        chapter__C chp = new chapter__C();
        chp.name ='test1';
        chp.TotalLinePrice__c =100;
        chp.TotalTrucks__c =100;
        insert chp;

        Proposal_Line_Item__c pli = new Proposal_Line_Item__c ();
        pli.Name=  'raj' ;
        pli.chapters__c = chp.Id ;
        pli.LineNumber__c = 50;
        pli.NumberofTrucks__c = 50 ;
        insert pli;  
        update pli;
        delete pli;

        Proposal_Line_Item__c pli2 = new Proposal_Line_Item__c ();
        pli2.Name=  'raj' ;
        pli2.chapters__c = chp.Id ;
        pli.LineNumber__c =  50 ;
        pli.NumberofTrucks__c = 50 ;
        insert pli2;  
        update pli2;
        delete pli2;
    }

    @istest
    private static void Testclassforaddingpli3(){

        chapter__C chp = new chapter__C();
        chp.name ='test1';
        chp.TotalLinePrice__c = 500;
        chp.TotalTrucks__c = 500;
        insert chp;
        update chp;

        Proposal_Line_Item__c pli = new Proposal_Line_Item__c ();
        pli.Name=  'raj' ;
        pli.chapters__c = chp.Id ;
        pli.LineNumber__c = 450;
        pli.NumberofTrucks__c =50 ;
        insert pli;  
        update pli;
        delete pli;

        Proposal_Line_Item__c pli3 = new Proposal_Line_Item__c ();
        pli3.Name=  'raj' ;
        pli3.chapters__c = chp.Id ;
        pli3.LineNumber__c = 50;
        pli3.NumberofTrucks__c = 450  ;

        insert pli3;  
        update pli3;
        delete pli3;

        chapter__C chp1 = new chapter__C();
        chp1.name ='test1';
        chp1.TotalLinePrice__c = pli.LinePrice__c + pli3.LinePrice__c;
        chp1.TotalTrucks__c = pli3.NumberofTrucks__c + pli.NumberofTrucks__c ;
        insert chp1;
    }
} 
1

You can use test setup methods to create test records once and then access them in every test method in the test class. Test setup methods can be time-saving when you need to create reference or prerequisite data for all test methods, or a common set of records that all test methods operate on.

So you need to create another method with the annotation @testSetup and write the code to create common test data, and the functionality which is being tested (and additional test data which is not common) would be in non-testSetup methods. So before executing each test method you will get those records which you have created in @testSetup method. In order to get/access those records in non @testSetup methods, you need to query for the records, and no need to call the @testSetup method explicitly.

Apart from that, as Salesforce recommends, you should surround your code part with Test.startTest() and Test.stopTest() and include assertions as well. There are a lot of documentations on web regarding this where you can read, like you can give a look here: When to use Test.startTest?
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_start_stop_test.htm

-2
@testSetup static void setup() {   
    List<chapter__C> chapterList = new List<chapter__C>();
    chapter__C chp = new chapter__C();
    chp.name ='test1';
    chp.TotalLinePrice__c =1000;
    chp.TotalTrucks__c =1000;
    chapterList.add(chp);
    insert chapterList;

    List<Proposal_Line_Item__c> pliList = new List<Proposal_Line_Item__c>();
    Proposal_Line_Item__c pli = new Proposal_Line_Item__c ();
    pli.Name=  'raj' ;
    pli.chapters__c = chp.Id ;
    pli.LineNumber__c=1000;
    pli.NumberofTrucks__c=1000;
    pliList.add(pli);
    insert pliList; 
}


@istest 
    public static void Testclassforaddingpli(){
      List<chapter__C> chapterList = [SELECT Name, TotalLinePrice__c, TotalTrucks__c FROM chapter__C];
      List<Proposal_Line_Item__c> pliList = [SELECT Name, chapters__c, LineNumber__c, NumberofTrucks__c FROM Proposal_Line_Item__c];

      //Perform dml like update, delete here.
    }
  • Thank you so much @JD – raj blp Mar 11 at 12:18
  • do you wanna add a description? code only answer are considered as low quality – Pranay Jaiswal Mar 11 at 12:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.