You need to sign in to do that
Don't have an account?
Salesforce Admin 110
unit test only gives 34%
need some help with unit test which gives mee 100% on trigger but only 34% on class
trigger iainb on Contact (before insert, after insert, before update, after update, before delete, after delete, after undelete) { if(trigger.isupdate && trigger.isafter){ iain.sellercon (Trigger.new); iain.sellercon2 (Trigger.new); } }
public with sharing class iain { public static void sellercon (list<Contact> cons) { //seller resi list<Easy_Opportunity__c> opplist = new list <Easy_Opportunity__c>(); recordtype[] tt = [Select r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Easy_Opportunity__c' and Name = 'Residential Sales']; Contact[] allBuyers = [Select id, contacttype__c From Contact where Id In : cons ]; set<id> allBuyersSet = new set<id>(); for (Contact buyer : allBuyers) if (buyer.contacttype__c.contains('Residential') && buyer.contacttype__c.contains('Seller')) allBuyersSet.add(buyer.id); set<id> fltrCons = new set<id>(); for (Easy_Opportunity__c easyopp : [Select Contact_Name__c From Easy_Opportunity__c where Contact_Name__c In :allBuyersSet and name = 'Market Appraisal']) fltrCons.add(easyopp.Contact_Name__c); for(Contact con : cons){ if( fltrCons.contains(con.id) == false && con.Stage__c == 'Lead' && con.contacttype__c.contains('Residential') && con.contacttype__c.contains('Seller')) { /*con.Seller__c == true && con.Residential__c == true && con.Buyer__c == false && con.Commercial__c == false && con.Tenant__c == false && con.Landlord__c == false*/ Easy_Opportunity__c newopp = new Easy_Opportunity__c (); newopp.ownerid = con.allocated_user__c; newopp.name = 'Market Appraisal'; newopp.account_name__c = con.accountid; newopp.CurrencyIsoCode = con.currencyisocode; newopp.stage__c = 'New'; newopp.recordtypeid = tt[0].Id; newopp.Contact_Name__c = con.id; newopp.Close_Date__c = Date.today().addDays(2); opplist.add(newopp); } } //outsideforloop // if(oppList.size() > 0) { system.debug('###opplist' +opplist); insert opplist; list<Product__c> pdlist = new list <Product__c>(); recordtype[] aa = [Select r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Product__c' and Name = 'Residential Sales']; for(Easy_Opportunity__c eachopp:opplist){ Product__c newpd = new Product__c(); newpd.name = 'Market Appraisal'; newpd.Sale_Price__c = 0.00; newpd.recordtypeid = aa[0].id; newpd.Easy_Opportunity__c = eachopp.id; pdlist.add(newpd); } insert pdlist; } } //seller resi buyer public static void sellercon2 (list<Contact> cons) { //seller resi list<Easy_Opportunity__c> opplist = new list <Easy_Opportunity__c>(); recordtype[] tt = [Select r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Easy_Opportunity__c' and Name = 'Residential Sales']; Contact[] allBuyers = [Select id, contacttype__c From Contact where Id In : cons ]; set<id> allBuyersSet = new set<id>(); for (Contact buyer : allBuyers) if (buyer.contacttype__c.contains('Residential') && buyer.contacttype__c.contains('Seller') && buyer.contacttype__c.contains('Buyer')) allBuyersSet.add(buyer.id); set<id> fltrCons = new set<id>(); for (Easy_Opportunity__c easyopp : [Select Contact_Name__c From Easy_Opportunity__c where Contact_Name__c In :allBuyersSet and name = 'Market Appraisal']) fltrCons.add(easyopp.Contact_Name__c); for(Contact con : cons){ if( fltrCons.contains(con.id) == false && con.Stage__c == 'Lead' && con.contacttype__c.contains('Residential') && con.contacttype__c.contains('Seller') && con.contacttype__c.contains('Buyer')) { /*con.Seller__c == true && con.Residential__c == true && con.Buyer__c == false && con.Commercial__c == false && con.Tenant__c == false && con.Landlord__c == false*/ Easy_Opportunity__c newopp = new Easy_Opportunity__c (); newopp.ownerid = con.allocated_user__c; newopp.name = 'Market Appraisal'; newopp.account_name__c = con.accountid; newopp.CurrencyIsoCode = con.currencyisocode; newopp.stage__c = 'New'; newopp.recordtypeid = tt[0].Id; newopp.Contact_Name__c = con.id; newopp.Close_Date__c = Date.today().addDays(2); opplist.add(newopp); } } //outsideforloop // if(oppList.size() > 0) { system.debug('###opplist' +opplist); insert opplist; list<Product__c> pdlist = new list <Product__c>(); recordtype[] aa = [Select r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Product__c' and Name = 'Residential Sales']; for(Easy_Opportunity__c eachopp:opplist){ Product__c newpd = new Product__c(); newpd.name = 'Market Appraisal'; newpd.Sale_Price__c = 0.00; newpd.recordtypeid = aa[0].id; newpd.Easy_Opportunity__c = eachopp.id; pdlist.add(newpd); } insert pdlist; } } }
@istest public class iaintest { static testmethod void test_trigger(){ account acct = new account(Name='test'); insert acct; contact ct = new contact(firstname='iain', lastname='banks'); insert ct; Test.StartTest(); ct.Stage__c = 'Lead'; ct.ContactType__c ='Seller'; ct.ContactType__c ='Buyer'; ct.ContactType__c ='Residential'; update ct; Test.StopTest(); } }
Outmost important thing you need to do is that, based on the 'if' conditions you are checking in your main class, do you have your records with the same values ?
Try to query those and check if the record inserted satisfies the same conditions and returns some record.
You can also check what lines are not covered using Developer Console -> Test Coverage -> Overall Test Coverage.
PLEASE MARK THIS AS THE BEST ANSWER, IF YOU LIKE IT.
Thanks,
Rohit Alladi