+ Start a Discussion
TheLearnerTheLearner 

code coverage

Hi experts,
Could anyone increase code coverage of my trigger its (70%) coming but i need to improve atleast 90%.Thanks in advance.

rigger tRIIO_Subcontractor_Client_updateContract_Scc on SCC_Client__c (after delete, after insert, after update) {

//CH02.End




set<Id> setSCCId = new set<Id>();

set<string> setClientName = new set<string>();

List<SCC__c> lstSCCUpdate = new List<SCC__c>();

//CH02.Start

List<SCC_Client__c > lstSub = new List<SCC_Client__c>();

//CH02.End

if(trigger.isInsert || trigger.isUpdate)

lstSub = trigger.new;

else

lstSub = trigger.old;

//CH02.Start

for(SCC_Client__c objSC : lstSub){

if(objSC.SCC__c != null)

setSCCId.add(objSC.SCC__c);

}

//CH02.End

map<Id,SCC__c> mapSCC = new map<Id,SCC__c>([select id, name, h_Subcontractor_Client_Name__c, Owning_Region__c,

(select id, name, Client_Names__r.Name from Subcontractor_Clients__r where Client_Names__r.Name = 'tRIIO' or Client_Names__r.Name = 'WPD') //CH01 remove - where Client_Names__r.Name = 'tRIIO' )

from SCC__c where Id =: setSCCId]);

map<Id,string> mapClientName = new map<Id,string>(); //CH01

List<SCC_Clients__c> lstClients = ([select id, Name from SCC_Clients__c where Name = 'tRIIO' or Name = 'WPD' ]); //CH01 added WPD condition

for(SCC_Clients__c objClient: lstClients){

setClientName.add(objClient.Id);

mapClientName.put(objClient.Id,objClient.Name); //CH01

}

if(trigger.isInsert || trigger.isUpdate)

//CH02.Start

for(SCC_Client__c objSub : trigger.new){

if(mapSCC.get(objSub.SCC__c).Owning_Region__c == 'Central'){

if(setClientName.Contains(objSub.Client_Names__c)){

SCC__c objSCC = mapSCC.get(objSub.SCC__c);

// system.debug('@@@ h client name: '+objSCC.h_Subcontractor_Client_Name__c);

//system.debug('@@@ Subcon client name: '+objSub.Client_Names__c);

if(objSCC.h_Subcontractor_Client_Name__c == null || objSCC.h_Subcontractor_Client_Name__c == '')

objSCC.h_Subcontractor_Client_Name__c = mapClientName.get(objSub.Client_Names__c); //CH01

else

if(!objSCC.h_Subcontractor_Client_Name__c.Contains(mapClientName.get(objSub.Client_Names__c)))

objSCC.h_Subcontractor_Client_Name__c = objSCC.h_Subcontractor_Client_Name__c + mapClientName.get(objSub.Client_Names__c); //CH01

//objSCC.h_Subcontractor_Client_Name__c = (!objSCC.h_Subcontractor_Client_Name__c.Contains(mapClientName.get(objSub.Client_Names__c))?objSCC.h_Subcontractor_Client_Name__c:'') + mapClientName.get(objSub.Client_Names__c); //CH01

system.debug('### h client name: '+objSCC.h_Subcontractor_Client_Name__c);

lstSCCUpdate.add(objSCC);

}

else if(mapSCC.get(objSub.SCC__c).Subcontractor_Clients__r.isEmpty()){

SCC__c objSCC = mapSCC.get(objSub.SCC__c);

objSCC.h_Subcontractor_Client_Name__c = '';

lstSCCUpdate.add(objSCC);

}

}

//CH02.End

}

if(trigger.isDelete)

//CH02.Start

for(SCC_Client__c objSub : trigger.old){

if(mapSCC.get(objSub.SCC__c).Owning_Region__c == 'Central')

if(mapSCC.get(objSub.SCC__c).Subcontractor_Clients__r.isEmpty()){

SCC__c objSCC = mapSCC.get(objSub.SCC__c);

objSCC.h_Subcontractor_Client_Name__c = '';

lstSCCUpdate.add(objSCC);

}

//CH02.Start

//CH01.start

else{

SCC__c objSCC = mapSCC.get(objSub.SCC__c);

boolean flgWPD = false;

boolean flgtRIIO = false;

//CH02.Start

for(SCC_Client__c obj :mapSCC.get(objSub.SCC__c).Subcontractor_Clients__r ){

if(obj.Client_Names__r.Name == 'WPD')

flgWPD = true;

if(obj.Client_Names__r.Name == 'tRIIO' )

flgtRIIO = true;

}

//CH02.End

if(mapClientName.get(objSub.Client_Names__c) == 'tRIIO' && objSCC.h_Subcontractor_Client_Name__c.contains('WPD')){

if(flgtRIIO)

objSCC.h_Subcontractor_Client_Name__c = 'WPDtRIIO';

else

objSCC.h_Subcontractor_Client_Name__c = 'WPD';

}

else if(mapClientName.get(objSub.Client_Names__c) == 'WPD' && objSCC.h_Subcontractor_Client_Name__c.contains('tRIIO')){

if(flgWPD)

objSCC.h_Subcontractor_Client_Name__c = 'WPDtRIIO';

else

objSCC.h_Subcontractor_Client_Name__c = 'tRIIO';

}

lstSCCUpdate.add(objSCC);

} //CH01.end

}

if(!lstSCCUpdate.isEmpty()){

List<SCC__c> lstSCC = new List<SCC__c>();

Set<SCC__c> setSCC = new Set<SCC__c>();

for(SCC__c obj:lstSCCUpdate){

if(!setSCC.contains(obj)) lstSCC.add(obj);

setSCC.add(obj);

}

//update lstSCCUpdate;

update lstSCC;

}




}




Test class




@isTest

private class tRIIO_Subcont_Client_updateCont_Scc_test {

static testMethod void myUnitTest(){

SCC_Clients__c testSclient = new SCC_Clients__c(Name = 'tRIIO', Owning_Region__c = 'Central');

insert testSclient;

SCC_Clients__c testSclient1 = new SCC_Clients__c(Name = 'test scc client', Owning_Region__c = 'Central');

insert testSclient1;

SCC__c testScc = new SCC__c(Name = 'test SCC', Contact_Name__c = 'test Contact name',Location__c = 'test location',

Street__c = 'test street', Town__c = 'test town', Number_of_Employees__c = 'Less than 5 employees',

Area_Covered__c = 'Central', Owning_Region__c = 'Central');

insert testScc;

//CH01.Start

SCC_Client__c testSubClient = new SCC_Client__c(Name = 'test Subcontractor Client',

Client_Names__c = testSclient.Id , SCC__c = testScc.Id);

insert testSubClient;

testSubClient.Client_Names__c = testSclient1.Id;

update testSubClient;

delete testSubClient;

//CH01.End

}

}
Always ThinkinAlways Thinkin
Hi, do you think you could post a screenshot of the current coverage so we can see which lines are not covered yet? You can see that coverage in the Developer Console by running the unit test then opening the trigger and selecting the unit test from the Code Coverage drop-down in the upper right corner. That will help me see what aspects of your trigger need coverage.
Always ThinkinAlways Thinkin
You should also add System Asserts. These won't increase your code coverage but they're very important for ensuring that your trigger does what it's expected to do!
 
Always ThinkinAlways Thinkin
My first guess is that it's some of your if-else clauses that need extra coverage such as if(mapClientName.get(objSub.Client_Names__c) == 'WPD' for which you would need to insert an additional Client Names record with that value so that it attempts that path through your code. You might want to create a couple test methods that attempt the various potential paths through your if-else clauses.
TheLearnerTheLearner

HI,
Thnanks for the quick reply,i striked the lines which are not covered.



trigger tRIIO_Subcontractor_Client_updateContract_Scc on SCC_Client__c (after delete, after insert, after update) {

//CH02.End




set<Id> setSCCId = new set<Id>();

set<string> setClientName = new set<string>();

List<SCC__c> lstSCCUpdate = new List<SCC__c>();

//CH02.Start

List<SCC_Client__c > lstSub = new List<SCC_Client__c>();

//CH02.End

if(trigger.isInsert || trigger.isUpdate)

lstSub = trigger.new;

else

lstSub = trigger.old;

//CH02.Start

for(SCC_Client__c objSC : lstSub){

if(objSC.SCC__c != null)

setSCCId.add(objSC.SCC__c);

}

//CH02.End

map<Id,SCC__c> mapSCC = new map<Id,SCC__c>([select id, name, h_Subcontractor_Client_Name__c, Owning_Region__c,

(select id, name, Client_Names__r.Name from Subcontractor_Clients__r where Client_Names__r.Name = 'tRIIO' or Client_Names__r.Name = 'WPD') //CH01 remove - where Client_Names__r.Name = 'tRIIO' )

from SCC__c where Id =: setSCCId]);

map<Id,string> mapClientName = new map<Id,string>(); //CH01

List<SCC_Clients__c> lstClients = ([select id, Name from SCC_Clients__c where Name = 'tRIIO' or Name = 'WPD' ]); //CH01 added WPD condition

for(SCC_Clients__c objClient: lstClients){

setClientName.add(objClient.Id);

mapClientName.put(objClient.Id,objClient.Name); //CH01

}

if(trigger.isInsert || trigger.isUpdate)

//CH02.Start

for(SCC_Client__c objSub : trigger.new){

if(mapSCC.get(objSub.SCC__c).Owning_Region__c == 'Central'){

if(setClientName.Contains(objSub.Client_Names__c)){

SCC__c objSCC = mapSCC.get(objSub.SCC__c);

// system.debug('@@@ h client name: '+objSCC.h_Subcontractor_Client_Name__c);

//system.debug('@@@ Subcon client name: '+objSub.Client_Names__c);

if(objSCC.h_Subcontractor_Client_Name__c == null || objSCC.h_Subcontractor_Client_Name__c == '')

objSCC.h_Subcontractor_Client_Name__c = mapClientName.get(objSub.Client_Names__c); //CH01

else

if(!objSCC.h_Subcontractor_Client_Name__c.Contains(mapClientName.get(objSub.Client_Names__c)))

objSCC.h_Subcontractor_Client_Name__c = objSCC.h_Subcontractor_Client_Name__c + mapClientName.get(objSub.Client_Names__c); //CH01

//objSCC.h_Subcontractor_Client_Name__c = (!objSCC.h_Subcontractor_Client_Name__c.Contains(mapClientName.get(objSub.Client_Names__c))?objSCC.h_Subcontractor_Client_Name__c:'') + mapClientName.get(objSub.Client_Names__c); //CH01

system.debug('### h client name: '+objSCC.h_Subcontractor_Client_Name__c);

lstSCCUpdate.add(objSCC);

}

else if(mapSCC.get(objSub.SCC__c).Subcontractor_Clients__r.isEmpty()){

SCC__c objSCC = mapSCC.get(objSub.SCC__c);

objSCC.h_Subcontractor_Client_Name__c = '';

lstSCCUpdate.add(objSCC);

}

}

//CH02.End

}

if(trigger.isDelete)

//CH02.Start

for(SCC_Client__c objSub : trigger.old){

if(mapSCC.get(objSub.SCC__c).Owning_Region__c == 'Central')

if(mapSCC.get(objSub.SCC__c).Subcontractor_Clients__r.isEmpty()){

SCC__c objSCC = mapSCC.get(objSub.SCC__c);

objSCC.h_Subcontractor_Client_Name__c = '';

lstSCCUpdate.add(objSCC);

}

//CH02.Start

//CH01.start

else{

SCC__c objSCC = mapSCC.get(objSub.SCC__c);

boolean flgWPD = false;

boolean flgtRIIO = false;


//CH02.Start

for(SCC_Client__c obj :mapSCC.get(objSub.SCC__c).Subcontractor_Clients__r ){

if(obj.Client_Names__r.Name == 'WPD')

flgWPD = true;

if(obj.Client_Names__r.Name == 'tRIIO' )

flgtRIIO = true;


}

//CH02.End

if(mapClientName.get(objSub.Client_Names__c) == 'tRIIO' && objSCC.h_Subcontractor_Client_Name__c.contains('WPD')){

if(flgtRIIO)

objSCC.h_Subcontractor_Client_Name__c = 'WPDtRIIO';


else

objSCC.h_Subcontractor_Client_Name__c = 'WPD';

}

else if(mapClientName.get(objSub.Client_Names__c) == 'WPD' && objSCC.h_Subcontractor_Client_Name__c.contains('tRIIO')){

if(flgWPD)

objSCC.h_Subcontractor_Client_Name__c = 'WPDtRIIO';


else

objSCC.h_Subcontractor_Client_Name__c = 'tRIIO';

}

lstSCCUpdate.add(objSCC);

} //CH01.end

}

if(!lstSCCUpdate.isEmpty()){

List<SCC__c> lstSCC = new List<SCC__c>();

Set<SCC__c> setSCC = new Set<SCC__c>();

for(SCC__c obj:lstSCCUpdate){

if(!setSCC.contains(obj)) lstSCC.add(obj);

setSCC.add(obj);

}

//update lstSCCUpdate;

update lstSCC;

}




}

 
Always ThinkinAlways Thinkin
For the first line that isn't covered:
if(objSCC.h_Subcontractor_Client_Name__c == null || objSCC.h_Subcontractor_Client_Name__c == ''

are you inserting a record in which h_Subcontractor_Client_Name__c == null? It looks like no part of the test covers that condition, so the evaluation is always false, thus the line is never touched by the test scenario.
Always ThinkinAlways Thinkin
For the next block, between 
//CH02.Start //CH01.start and //CH02.End

it looks like you are not inserting any Subcontractor_Clients__c records, so the list is always empty and the else{} clause is never invoked.
Always ThinkinAlways Thinkin
And the last block is not invoked because there is no SCC__c record inserted with Subcontractor_Client_Name__c == 'WPD' so neither the if() nor the else if() blocks ever evaluate to true
TheLearnerTheLearner
Hi,

Thanks for the reply could you able to write code for this please
Always ThinkinAlways Thinkin
Sorry, I can't help with that, got my own boss to please.