You need to sign in to do that
Don't have an account?
Samadhan Sakhale 3
Test Class Code coverage
hi all,
i have written trigger but it gives only 44% code coverage so what can do forn increase it....?
My trigger is
trigger UpdateprdctonAcc on Subscription__c (after insert)
{
//Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'User Group Membership'].Id;
Id recordTypeId = Schema.SObjectType.Subscription__c.getRecordTypeInfosByName().get('User Group Membership').getRecordTypeId();
List<Subscription__c> subscList = new List<Subscription__c>();
for (Subscription__c s : Trigger.new){
if(s.RecordTypeId == recordTypeId)
subscList.add(s);
}
String str;
Map<ID, Account> Acc = new Map<ID, Account>(); //Making it a map instead of list for easier lookup
List<Id> listIds = new List<Id>();
set<ID> cObjectID = new set<ID>(); //Making a set of Product ID's
Map<ID, Account> updateMap = new Map<ID, Account>();
for (Subscription__c s : subscList)
{
listIds.add(s.Company_Name__c);
if(s.Membership_Type__c!= null)
{
cObjectID.add(s.Membership_Type__c);//takes the Lookup Record & Add that ID's in cObjectID set
}
}
if(!cObjectID.isEmpty())
{
Map<ID,Product2> cObjectMap = new Map<ID,Product2>([select Id,Name from Product2 where Id IN: cObjectID]);
Acc = new Map<Id, Account>([SELECT id,Product__c,(SELECT ID,Membership_Type__c FROM Subscriptions__r) FROM Account WHERE ID IN :listIds]);
for(Subscription__c s : subscList)
{
if(cObjectMap.get(s.Membership_Type__c).Name != Null)
{
// fill the country name on Opportunity with Country Name on Country_Object__c
String pro= cObjectMap.get(s.Membership_Type__c).Name;
Account myacc = acc.get(s.Company_Name__c);
if(myacc != null){ //always check for nulls to avoid null pointer exceptions
myacc.Product__c=pro;
updateMap.put(myacc.Id,myacc);
}
}
}
update updateMap.values();
}
}
and test class for this is....
@istest
public class TestPrdctonacc
{
static testMethod void verifyProductUpdation()
{
// Set up the Account record.
Account a = new Account(Name='Test Account' ,SLA__c='Gold', SLAExpirationDate__c=Date.today(),SLASerialNumber__c='jhbfs');
insert a;
Product2 p=new Product2(Name='Product Name');
insert p;
test.startTest();
// Verify that the initial state is as expected.
a = [SELECT Name,Product__c FROM Account WHERE Id = :a.Id];
System.assertEquals(null,a.Product__c);
// Set up the Opportunity record.
String company=a.Id;
Subscription__c sub = new Subscription__c(Name='ABC',Company_Name__c=company,Membership_Type__c=p.Id);
// Cause the Trigger to execute.
insert sub;
// Verify that the results are as expected.
a = [SELECT Name, Product__c FROM Account WHERE Id = :a.Id];
System.assertEquals(null, a.Product__c);
test.stopTest();
}
}
please tell me answer in brief
Thanks,
Sam
i have written trigger but it gives only 44% code coverage so what can do forn increase it....?
My trigger is
trigger UpdateprdctonAcc on Subscription__c (after insert)
{
//Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'User Group Membership'].Id;
Id recordTypeId = Schema.SObjectType.Subscription__c.getRecordTypeInfosByName().get('User Group Membership').getRecordTypeId();
List<Subscription__c> subscList = new List<Subscription__c>();
for (Subscription__c s : Trigger.new){
if(s.RecordTypeId == recordTypeId)
subscList.add(s);
}
String str;
Map<ID, Account> Acc = new Map<ID, Account>(); //Making it a map instead of list for easier lookup
List<Id> listIds = new List<Id>();
set<ID> cObjectID = new set<ID>(); //Making a set of Product ID's
Map<ID, Account> updateMap = new Map<ID, Account>();
for (Subscription__c s : subscList)
{
listIds.add(s.Company_Name__c);
if(s.Membership_Type__c!= null)
{
cObjectID.add(s.Membership_Type__c);//takes the Lookup Record & Add that ID's in cObjectID set
}
}
if(!cObjectID.isEmpty())
{
Map<ID,Product2> cObjectMap = new Map<ID,Product2>([select Id,Name from Product2 where Id IN: cObjectID]);
Acc = new Map<Id, Account>([SELECT id,Product__c,(SELECT ID,Membership_Type__c FROM Subscriptions__r) FROM Account WHERE ID IN :listIds]);
for(Subscription__c s : subscList)
{
if(cObjectMap.get(s.Membership_Type__c).Name != Null)
{
// fill the country name on Opportunity with Country Name on Country_Object__c
String pro= cObjectMap.get(s.Membership_Type__c).Name;
Account myacc = acc.get(s.Company_Name__c);
if(myacc != null){ //always check for nulls to avoid null pointer exceptions
myacc.Product__c=pro;
updateMap.put(myacc.Id,myacc);
}
}
}
update updateMap.values();
}
}
and test class for this is....
@istest
public class TestPrdctonacc
{
static testMethod void verifyProductUpdation()
{
// Set up the Account record.
Account a = new Account(Name='Test Account' ,SLA__c='Gold', SLAExpirationDate__c=Date.today(),SLASerialNumber__c='jhbfs');
insert a;
Product2 p=new Product2(Name='Product Name');
insert p;
test.startTest();
// Verify that the initial state is as expected.
a = [SELECT Name,Product__c FROM Account WHERE Id = :a.Id];
System.assertEquals(null,a.Product__c);
// Set up the Opportunity record.
String company=a.Id;
Subscription__c sub = new Subscription__c(Name='ABC',Company_Name__c=company,Membership_Type__c=p.Id);
// Cause the Trigger to execute.
insert sub;
// Verify that the results are as expected.
a = [SELECT Name, Product__c FROM Account WHERE Id = :a.Id];
System.assertEquals(null, a.Product__c);
test.stopTest();
}
}
please tell me answer in brief
Thanks,
Sam
Thanks for your responce but i get answer in another way i write class as,
@istest
public class TestPrdctonacc
{
static testMethod void verifyProductUpdation()
{
// Set up the Account record.
Product2 p=new Product2(Name='Product Name');
insert p;
Account a = new Account(Name='Test Account' ,SLA__c='Gold', SLAExpirationDate__c=Date.today(),SLASerialNumber__c='jhbfs');
insert a;
test.startTest();
// Verify that the initial state is as expected.
a = [SELECT Name,Product__c FROM Account WHERE Id = :a.Id];
System.assertEquals(null,a.Product__c);
// Set up the Opportunity record.
String company=a.Id;
Id recordTypeId = Schema.SObjectType.Subscription__c.getRecordTypeInfosByName().get('User Group Membership').getRecordTypeId();
RecordType rt = [select id,Name from RecordType where SobjectType='Subscription__c' and Name='User Group Membership' Limit 1];
Subscription__c sub = new Subscription__c(Name='ABC',Company_Name__c=company,Membership_Type__c=p.Id,RecordTypeId=rt.Id);
// Cause the Trigger to execute.
insert sub;
if(sub.RecordTypeId==recordTypeID)
{
a.Product__c=sub.Membership_Type__c;
update a;
}
// Verify that the results are as expected.
a = [SELECT Name, Product__c FROM Account WHERE Id = :a.Id];
System.assertEquals(sub.Membership_Type__c, a.Product__c);
test.stopTest();
}
}
thanks,
Sam
All Answers
Looks like you were checking the record type of Subscription,
*** if(s.RecordTypeId == recordTypeId) ***
and in the test class, for subscription test data you havent mentioned any recordtype id. Try giving the subscription test data your desired record type and run it.
Thanks,
balaji
Thanks for your responce but i get answer in another way i write class as,
@istest
public class TestPrdctonacc
{
static testMethod void verifyProductUpdation()
{
// Set up the Account record.
Product2 p=new Product2(Name='Product Name');
insert p;
Account a = new Account(Name='Test Account' ,SLA__c='Gold', SLAExpirationDate__c=Date.today(),SLASerialNumber__c='jhbfs');
insert a;
test.startTest();
// Verify that the initial state is as expected.
a = [SELECT Name,Product__c FROM Account WHERE Id = :a.Id];
System.assertEquals(null,a.Product__c);
// Set up the Opportunity record.
String company=a.Id;
Id recordTypeId = Schema.SObjectType.Subscription__c.getRecordTypeInfosByName().get('User Group Membership').getRecordTypeId();
RecordType rt = [select id,Name from RecordType where SobjectType='Subscription__c' and Name='User Group Membership' Limit 1];
Subscription__c sub = new Subscription__c(Name='ABC',Company_Name__c=company,Membership_Type__c=p.Id,RecordTypeId=rt.Id);
// Cause the Trigger to execute.
insert sub;
if(sub.RecordTypeId==recordTypeID)
{
a.Product__c=sub.Membership_Type__c;
update a;
}
// Verify that the results are as expected.
a = [SELECT Name, Product__c FROM Account WHERE Id = :a.Id];
System.assertEquals(sub.Membership_Type__c, a.Product__c);
test.stopTest();
}
}
thanks,
Sam