0

My trigger working well.My problem on test class.Test class result is 76 lines not tested,%0 covered. How i can fix it. Pls help me. Here my trigger :

trigger CreatingOrderLineItem on OpportunityLineItem (after insert,after update,before delete) {

    id opportunityuser=userinfo.getProfileId();
    if(opportunityuser=='00eb0000000xMDF'){
    if(trigger.isAfter && trigger.isInsert){ 
    Order_Line_Item__c neworderlineitem=new Order_Line_Item__c();
    List <Order_Line_Item__c> orderlineitemlist=new List<Order_Line_Item__c>();
    List<ID> pricebookID=new List<ID>();
    List<ID> opportunityID=new List<ID>();
    List<OpportunityLineItem> newopportunitylineitemlist=trigger.new;
    for(OpportunityLineItem opportunitylistfield:newopportunitylineitemlist){
    neworderlineitem.ServiceDate__c =opportunitylistfield.ServiceDate;
    neworderlineitem.LineDescription__c=opportunitylistfield.Description;
    neworderlineitem.Discount__c=opportunitylistfield.Discount;
    neworderlineitem.Quantity__c=opportunitylistfield.Quantity;
    neworderlineitem.UnitPrice__c=opportunitylistfield.UnitPrice;
    neworderlineitem.Subtotal__c=opportunitylistfield.Subtotal;
    pricebookID.add(opportunitylistfield.PriceBookEntryId);
    opportunityID.add(opportunitylistfield.OpportunityId);
    orderlineitemlist.add(neworderlineitem);
}
    List<Order__c> orderlist= new List<Order__c>([Select Id,Opportunity__c From Order__c Where Opportunity__c IN:opportunityID]);
    List<PricebookEntry> pricebookentrylist = new List<PricebookEntry>([Select Id,Product2Id From PricebookEntry Where Id IN:pricebookID]);
    for (integer i=0;i<orderlineitemlist.size();i++){
    for (integer j=0;j<orderlist.size();j++){
     if(opportunityID[i]==(orderlist[j].Opportunity__c)){
       orderlineitemlist[i].Order__c=orderlist[j].Id;
       }
       }
      }
    for (integer k=0;k<orderlineitemlist.size();k++){
    for (integer l=0;l<pricebookentrylist.size();l++){
     if(pricebookId[k]==(pricebookentrylist[l].Id)){
       orderlineitemlist[k].Product2__c=pricebookentrylist[l].Product2Id;
       }
       }
       }
    insert(orderlineitemlist);
       }
    if(trigger.isAfter && trigger.isUpdate){
    Set<ID> pricebookID=new Set<ID>();
    Set<ID> opportunityID=new Set<ID>();
    List<OpportunityLineItem> updatelist=trigger.new;
    for(OpportunityLineItem opportunitylineitemlist:updatelist){
    pricebookID.add(opportunitylineitemlist.PricebookEntryId);
    opportunityID.add(opportunitylineitemlist.OpportunityId);
       }
    List<Order__c> orderlist=new List<Order__c>([Select Id, Opportunity__c From Order__c Where Opportunity__c IN:opportunityID]);
    List<PricebookEntry> pricebookentrylist=new List<PricebookEntry>([Select Product2Id, ID From PricebookEntry Where Id IN:pricebookID]);
    Set<ID> orderlistid= new Set<ID>();
    Set<ID> pricebooklistid=new Set <ID>();
    for (integer m=0;m<orderlist.size();m++){
    orderlistid.add(orderlist[m].Id);
       }
    for (integer n=0;n<pricebookentrylist.size();n++){
    pricebooklistid.add(pricebookentrylist[n].Product2Id);
       }
    List<Order_Line_Item__c> orderlineitemlist = new List<Order_Line_Item__c>([Select ServiceDate__c,LineDescription__c,TotalPrice__c,Discount__c,Quantity__c,UnitPrice__c,Subtotal__c,Product2__c,Order__c From Order_Line_Item__c Where Product2__c IN:pricebooklistid OR Order__c IN:orderlistid]);
    for(integer a=0;a<orderlineitemlist.size();a++){
    for(integer b=0;b<orderlist.size();b++){
    if(orderlineitemlist[a].Order__c==orderlist[b].Id){
    for(integer c=0;c<pricebooklistid.size();c++){
    if(orderlineitemlist[a].Product2__c==pricebookentrylist[c].Product2Id){
    for (integer d=0;d<updatelist.size();d++){
    if(updatelist[d].OpportunityId==orderlist[b].Opportunity__c && updatelist[d].PricebookEntryId==pricebookentrylist[c].Id){
    orderlineitemlist[a].ServiceDate__c=updatelist[d].ServiceDate;
    orderlineitemlist[a].UnitPrice__c=updatelist[d].UnitPrice;
    orderlineitemlist[a].LineDescription__c=updatelist[d].Description;
    orderlineitemlist[a].Discount__c=updatelist[d].Discount;
    orderlineitemlist[a].Quantity__c=updatelist[d].Quantity;
    orderlineitemlist[a].SubTotal__c=updatelist[d].SubTotal;
                 }
                 }
                 }
                 }
                 }
                 }
                 }
    update(orderlineitemlist);                   
              } 

    if(Trigger.isDelete && Trigger.isBefore){
    Set<ID> pricebookID=new Set<ID>();
    Set<ID> opportunityID=new Set<ID>();
    List<OpportunityLineItem> deletelist=trigger.old;
    for (OpportunityLineItem opportunitylineitemlist:deletelist){
    pricebookID.add(opportunitylineitemlist.PricebookEntryId);
    opportunityID.add(opportunitylineitemlist.OpportunityId);
       } 
    List<Order__c> orderlist= new List<Order__c>([Select Id,Opportunity__c From Order__c Where Opportunity__c IN:opportunityID]); 
    List<PricebookEntry> pricebooklist= new List<PricebookEntry>([Select Id,Product2Id From PricebookEntry Where Id IN:pricebookID]);
    Set<ID> orderlistid=new Set<ID>();
    Set<ID> pricebooklistid=new Set<ID>();
    for(integer e=0;e<orderlist.size();e++){
    orderlistid.add(orderlist[e].Id);
        }
    for(integer f=0;f<pricebooklist.size();f++){
    pricebooklistid.add(pricebooklist[f].Product2Id);
        }
    List<Order_Line_Item__c> orderlineitemlist=new List <Order_Line_Item__c>([Select Id From Order_Line_Item__c Where Product2__c IN:pricebooklistid AND Order__c IN:orderlistid]);
    delete(orderlineitemlist);                        
       }
       }
       }

and my test class is:

@isTest

public with sharing class CreatingOrderLineItemTest {

    static testMethod void myUnitTestInsert(){

Profile profiles=[Select Id From Profile Where Name='System Administrator'];

User newuser=new User(
profileid=profiles.Id,
email='[email protected]',
username='[email protected]',
lastname='deneme',
alias='deneme1',
languagelocalekey='en_US',
emailencodingkey='UTF-8',
localesidkey='en_US',
timezonesidkey='America/Los_Angeles');

insert newuser;

String pricebookid = '';
List<Pricebook2> pb2 = [select Id from Pricebook2 where isStandard=true];
for(Pricebook2 pbb:pb2){
pricebookid = pbb.Id;

Opportunity testOpportunity=new Opportunity(
Name = 'Test 1',
StageName ='Closed Won',
CloseDate= System.today(),
Amount=50000);
insert testOpportunity;

Product2 p2 = new Product2(Name='Test Product',isActive=true);
insert p2;

PricebookEntry pbe = new PricebookEntry(Pricebook2Id=pricebookid, Product2Id=p2.Id, UnitPrice=99, isActive=true);
insert pbe;

OpportunityLineItem oli = new OpportunityLineItem(PriceBookEntryId=pbe.Id, OpportunityId=testOpportunity.Id, Quantity=1, TotalPrice=99);
List <OpportunityLineItem> olilist=new List<OpportunityLineItem>{oli};
insert (olilist);

Order_Line_Item__c neworderlineitem=new Order_Line_Item__c();
List<Order_Line_Item__c> ordlist=new List <Order_Line_Item__c>();

for(OpportunityLineItem oppor:olilist){
    neworderlineitem.Quantity__c=oppor.Quantity;
    //neworderlineitem.TotalPrice__c=oppor.TotalPrice;
    neworderlineitem.Order__c=oppor.OpportunityId;
    ordlist.add(neworderlineitem);
}
Test.startTest();

System.runAs(newuser){
insert(ordlist);

List<Order_Line_Item__c> ordlists=[Select Quantity__c,TotalPrice__c,Order__c From Order_Line_Item__c Where id=:oli.Id];
for (Order_Line_Item__c ord:ordlists){
system.assertEquals(ord.Quantity__c,1);
system.assertEquals(ord.TotalPrice__c,99);
}

}
Test.stopTest();

}

}
}
9
  • 1
    I believe that to access the standard Pricebook in a testmethod, you need @isTest(SeeAllData=true) Look at the debug log for the test run and look for successful execution of the soql and dml statements
    – cropredy
    Jan 14, 2014 at 20:32
  • i looked debug log.Status is success and it passed. But lines not covered. its %0
    – user
    Jan 14, 2014 at 21:07
  • Can you look in your log and see what the last line number in the Test class is hit before it exits? It might start the test and exit out when it runs into an issue, giving you the illusion of passing without ever hitting the trigger. You might also want an assert at the end to ensure that the size of your ordlists is expected (right now a size of 0 results in no asserts). Jan 14, 2014 at 21:20
  • Have you looked through the debug log to make sure that when the test runs the CreatingOrderLineItem trigger is actually fired? if your OpportunityLineItem is inserted and this is not fired i would guess that the trigger may be flagged as Inactive Edit - List<Order_Line_Item__c> ordlists=[Select Quantity__c,TotalPrice__c,Order__c From Order_Line_Item__c Where id=:oli.Id]; The query shown here will fail as it is trying to use the OpportunityLineItem Id to find an Order_Line_Item__c by Id. Jan 14, 2014 at 21:20
  • 2
    As a side note, I'd strongly recommend writing your test method to cover several hundred records at once; I wouldn't be surprised if your code timed out because of the loop-loop-loop-loop you have in the code.
    – sfdcfox
    Jan 14, 2014 at 21:31

1 Answer 1

1

So this might be a simple answer.. Where are you viewing code coverage?

After winter 13 code coverage in the class list page in the UI always shows 0% code coverage. You need to view your code coverage in the developer console or in the ide.

3
  • i am using eclipse. after i click runtest it shows me code coverage and failures.
    – user
    Jan 14, 2014 at 23:46
  • If your test coverage is failing, it makes sence that you have 0% code coverage. You need to fix the errors you are getting from your testrun first. Also, if your testrun fails, I would be surprised that your trigger does what it should do.
    – KoenVM
    Jan 15, 2014 at 7:27
  • I agree with KoenVM! Jan 16, 2014 at 0:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .