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
sfdclearnsfdclearn 

Test class has only 60% code coverage

Hi,

 

  I have written the following trigger.

 

 

trigger OpportunityStageToCaseStatus on Opportunity (after update) {
/////////////////////////////////
// This Trigger updates the Case status of the related opportunity//
/////////////////////////////////

if(trigger.isAfter && trigger.isUpdate) {

// Create List of Cases that relate to this Opportunity
List<Case> lstCasesToCheck = [SELECT Opportunity__c, Status FROM Case WHERE Opportunity__c IN :trigger.new];
List<Case> lstCasesToUpdate = new List<Case>();

for(Opportunity o : trigger.new) {

// Check to see if the StageName has changed

{
if(trigger.newMap.get(o.Id).StageName != trigger.oldMap.get(o.Id).StageName)
// Loop through Cases
for(Case c : lstCasesToCheck) {
if(o.StageName == 'Closed/Won') {
c.Status= 'Resolved';
lstCasesToUpdate.add(c);
}
}

}

}

// Bulk DML
if(lstCasesToUpdate.size() > 0) { update lstCasesToUpdate; }

}
}

 

 

and wrote the following test class.

 

@isTest
private class OpportunityStageToCaseStatustest {

static testMethod void myUnitTest() {

case c=new case(Status='New',Origin='Email',Opportunity__c='006V0000002fHfU');
insert c;
opportunity o= new opportunity(id='006V0000002fHfU',StageName = 'Closed/Won');
update o;
c.status='Resolved';
update c;


}
}

 

But it shows only 60% code coverage. so i am not able to push the code to production. Any advise on this please.

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
sfdclearnsfdclearn

Thanks a lot! the test code passed and the code coverage is 100%.

 

All Answers

AtestAtest

Please try to figure out which of the lines are out of code coverage. that  will help you to narrow down the problem.

Also I saw u tried to assign existing ID to opportunity. Instead try to create the opportunity here then update it.

 

Like :

 

opportunity o = new opportunity (give opp-details)

insert o;

 

change o-data;

update o;

 

use o.Id inside case.

 

give it a try.............

sfdclearnsfdclearn

Thanks for the response. I tried to look at the code coverage. The only place it didn't cover was the whole for loop.

 

I also tried this one. But the test case fails.

@isTest
private class OpportunityStageToCaseStatustest {

static testMethod void myUnitTest() {

opportunity o= new opportunity(Name='test',StageName = 'Contract Signed');
insert o;
case c=new case(Status='New',Origin='Email',Opportunity__c='test');

insert c;
o.stageName='Closed/Won';
update o;
update c;

//case c=new case(Status='New',Origin='Email',Opportunity__c='006V0000002fHfU');
//insert c;
//opportunity o= new opportunity(id='006V0000002fHfU',StageName = 'Closed/Won');
//update o;

//c.status='Resolved';
// update c;


}
}

Naidu PothiniNaidu Pothini
@isTest
private class OpportunityStageToCaseStatustest 
{
	static testMethod void myUnitTest() 
	{
		test.startTest();
		
		opportunity o= new opportunity(Name='test',StageName = 'Contract Signed');
		insert o;
		
		case c=new case(Status='New',Origin='Email',Opportunity__c=o.Id);
		insert c;
		
		o.stageName='Closed/Won';
		update o;
		
		test.stopTest();
	}
}

 

Try this. 

sfdclearnsfdclearn

test class fails now.

Naidu PothiniNaidu Pothini

Is it throwing any error?

sfdclearnsfdclearn

No.when i run the test it shows the following.

 

Test Class OpportunityStageToCaseStatustest
Tests Run 1
Test Failures 1
Code Coverage Total % 0
Total Time (ms) 166.0
Debug Log Download

 

Thanks!

sanjaypatidarsanjaypatidar

can you post in your test class and failure details ?

sanjaypatidarsanjaypatidar

what is the error ? please list the error code. you can find it in failures section

sfdclearnsfdclearn

@isTest
private class OpportunityStageToCaseStatustest
{
static testMethod void myUnitTest()
{
test.startTest();

opportunity o= new opportunity(Name='test',StageName = 'Contract Signed');
insert o;

case c=new case(Status='New',Origin='Email',Opportunity__c=o.Id);
insert c;

o.stageName='Closed/Won';
update o;

test.stopTest();
}
}

this is the test class i am trying.

 

 

Test Class OpportunityStageToCaseStatustest
Tests Run 1
Test Failures 1
Code Coverage Total % 0
Total Time (ms) 108.0
Debug Log Download

sfdclearnsfdclearn

oh...yeah

 

this is the error msg

 

Method Name
Total Time (ms)
Message
Stack Trace
OpportunityStageToCaseStatustest.myUnitTest 108.0 System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [CloseDate]: [CloseDate] Class.OpportunityStageToCaseStatustest.myUnitTest: line 7, column 1

sanjaypatidarsanjaypatidar

SFDC Learn,  we need to see the error/failure which is coming ? When you run the test class you would have the error listed below, under the failure sections. please paste the same.

Naidu PothiniNaidu Pothini
@isTest
private class OpportunityStageToCaseStatustest 
{
	static testMethod void myUnitTest() 
	{
		test.startTest();
		
		opportunity o= new opportunity(Name='test',StageName = 'Contract Signed', closeDate=System.today());
		insert o;
		
		case c=new case(Status='New',Origin='Email',Opportunity__c=o.Id);
		insert c;
		
		o.stageName='Closed/Won';
		update o;
		
		test.stopTest();
	}
}

 try this.

sfdclearnsfdclearn

Thanks a lot! the test code passed and the code coverage is 100%.

 

This was selected as the best answer
AtestAtest

while creating opportunity u missed the required field CloseDate thats why ur getting error,.....

sfdclearnsfdclearn

Now the code coverage is 100% and i am trying to deploy that to production. I am using package to push this trigger and the test class to production. When i try to install the package in the production is shows package installation error saying that

 

"Duplicate RelationshipNameCustom Field DefinitionsThe relationship name "Cases__r" is already used by custom field Case.Opportunity__c. Please rename existing relationship name."

 

When i try to put the trigger and test class in the package this custom field automatically added to this package. Not sure why.

 

please advise.


sanjaypatidarsanjaypatidar

is the opportunity__c field on the Case already there ? or you are creating new ?

Naidu PothiniNaidu Pothini

Did you try deploying using Force.com IDE deployment wizard or the Force.com Migration Tool ?

 

 

Check this link,

 

http://success.salesforce.com/questionDetail?qid=a1X30000000KpGuEAK

sfdclearnsfdclearn

I tried the deployment connection as i did not want to rename the custom field. And it worked Perfect.