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
NatureGodNatureGod 

Test class only gets 66% coverage.for 2 Triggers.

Test class only gets 66% coverage.for 2 Triggers.

 

Greetings,

I have written (Enterprise Edition ) 2 Triggers:

1 for a custom object which sums points from the Standard Soltution to  a custom object:

--------------------------

trigger CalculateTotalAmount on Consumer__c (before update)
{

Integer i = 0;

Consumer__c parObj = Trigger.new[i];

// Current parent opportunity ID
String intTest = Trigger.new[i].Id;

//Step 2. Create a list of opportunities who are children of this parent opportunity record.
List<Solution> opp = [Select Solution.Id, Solution.Points__c From Solution where Solution.Consumer__c =: intTest];

Double j = 0;
Double jj = 0;


// Loop through the filtered Consumers and sum up their amounts.
for(Solution op : opp)
{
If
(
op.Points__c != Null
)


{
j += op.Points__c ;
jj += op.Points__c ;

}
}
parObj.Parallel_Points__c = j;
// parObj.Points__c = jj;

}

--------------------------------------------

2nd Trigger , Similar, this time standard is the Accounts:

 

trigger CalculateTotalAmountAccount on Account (before update)
{

Integer i = 0;

Account parObj = Trigger.new[i];

// Current parent opportunity ID
String intTest = Trigger.new[i].Id;

//Step 2. Create a list of Consumer__c who are children of this parent opportunity record.
List<Consumer__c> opp = [Select Consumer__c.Id, Consumer__c.Basket__c From Consumer__c where Consumer__c.Ophelia__c =: intTest];

Double j = 0;

// Loop through the filtered Consumers and sum up their amounts.
for(Consumer__c op : opp)
{
If
(
op.Basket__c != Null
)

{
j += op.Basket__c;
}
}
parObj.Basket__c = j;

}

------------------------------

Both Triggers work fine.

 

I have written the following Test Class for both which only gets 66%:

@istest
public class TesttrgOnArticulo{
Private Static testmethod void TesttrgOnArticulo(){

//Insert Participacion__c Record
Consumer__c Con = new Consumer__c ();
Con.Mobile__c = '1234';
Test.startTest();
insert Con;

Con.Mobile__c = '5678';
update Con;

//Insert Articulo__c Record
for(Integer i = 0; i < 50; i++){

Solution Sol = new Solution();
Sol.SolutionName = 'Test';
Sol.Status= 'Reviewed';
Sol.PointConvertion__c = 'Προσθήκη Πόντων';
Sol.Total_Amount__c = 100;
Sol.PointConvertion__c = '10';
Sol.Consumer__c = Con.id;
Sol.Points__c = 0;
Sol.Points__c = Sol.Points__c +1;
insert Sol;
update Sol;

}

//Insert Participacion__c Record
Account Acc = new Account();
Acc.Name= 'TEST';
insert Acc ;

Test.stopTest();


List<Solution> opp = [Select Solution.Id, Solution.Points__c From Solution where Solution.Consumer__c =: Con.id];

}
}

----------------------------------------------

 

 

Any Help to get at least 75% would  be Appreciated.

Many Thanks,

Stefanos

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

hi,

Try to use below updated Test Class.

 

Test Class:

@istest
public class TesttrgOnArticulo{
	Private Static testmethod void TesttrgOnArticulo(){
		//Insert Participacion__c Record
		Consumer__c Con = new Consumer__c ();
		Con.Mobile__c = '1234';
		Test.startTest();
		insert Con;

		

		//Insert Articulo__c Record
		for(Integer i = 0; i < 50; i++){
			Solution Sol = new Solution();
			Sol.SolutionName = 'Test';
			Sol.Status= 'Reviewed';
			Sol.PointConvertion__c = 'Προσθήκη Πόντων';
			Sol.Total_Amount__c = 100;
			Sol.PointConvertion__c = '10';
			Sol.Consumer__c = Con.id;
			Sol.Points__c = 0;
			Sol.Points__c = Sol.Points__c +1;
			insert Sol;
			update Sol;
		}
		
		Con.Mobile__c = '5678';
		update Con;
		
		
		
		//Insert Participacion__c Record
		Account Acc = new Account();
		Acc.Name= 'TEST';
		insert Acc ;
		
		Con.Consumer__c.Ophelia__c = Acc.id;
		Con.Basket__c = 'Test';
		update Con;
		
		Acc.Name= 'TEST Update';
		update Acc;
		
		Test.stopTest();
		List<Solution> opp = [Select Solution.Id, Solution.Points__c From Solution where Solution.Consumer__c =: Con.id];
	}
}

 

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

 

All Answers

hitesh90hitesh90

hi,

Try to use below updated Test Class.

 

Test Class:

@istest
public class TesttrgOnArticulo{
	Private Static testmethod void TesttrgOnArticulo(){
		//Insert Participacion__c Record
		Consumer__c Con = new Consumer__c ();
		Con.Mobile__c = '1234';
		Test.startTest();
		insert Con;

		

		//Insert Articulo__c Record
		for(Integer i = 0; i < 50; i++){
			Solution Sol = new Solution();
			Sol.SolutionName = 'Test';
			Sol.Status= 'Reviewed';
			Sol.PointConvertion__c = 'Προσθήκη Πόντων';
			Sol.Total_Amount__c = 100;
			Sol.PointConvertion__c = '10';
			Sol.Consumer__c = Con.id;
			Sol.Points__c = 0;
			Sol.Points__c = Sol.Points__c +1;
			insert Sol;
			update Sol;
		}
		
		Con.Mobile__c = '5678';
		update Con;
		
		
		
		//Insert Participacion__c Record
		Account Acc = new Account();
		Acc.Name= 'TEST';
		insert Acc ;
		
		Con.Consumer__c.Ophelia__c = Acc.id;
		Con.Basket__c = 'Test';
		update Con;
		
		Acc.Name= 'TEST Update';
		update Acc;
		
		Test.stopTest();
		List<Solution> opp = [Select Solution.Id, Solution.Points__c From Solution where Solution.Consumer__c =: Con.id];
	}
}

 

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

 

This was selected as the best answer
NatureGodNatureGod

Many Thanks Hitesh.

 

That worked and gave 100% on both Triggers.

 

I really appreciate and aslo posted this as a Solution.

 

Best Regards,

 

Stefanos.

NatureGodNatureGod

Hi Hitesh ,

 

Although it got both 100% coverage, funny enough, in the prod it wil not validate since it requires as it says 100% coverage!!!

 

Any suggestions?

 

P.S. My best Regards to Chenai teams, TCS. Cognizant as well.. I worked with guys from both companies, truly great!

 

Stefanos