You need to sign in to do that
Don't have an account?
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!
Thanks a lot! the test code passed and the code coverage is 100%.
All Answers
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.............
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;
}
}
Try this.
test class fails now.
Is it throwing any error?
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!
can you post in your test class and failure details ?
what is the error ? please list the error code. you can find it in failures section
@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
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
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.
try this.
Thanks a lot! the test code passed and the code coverage is 100%.
while creating opportunity u missed the required field CloseDate thats why ur getting error,.....
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.
is the opportunity__c field on the Case already there ? or you are creating new ?
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
I tried the deployment connection as i did not want to rename the custom field. And it worked Perfect.