This is my main class when i wrote test class for this class i got 41%.i need 75% code coverage for this class. Please check it..
-----Main class-------
public with sharing class AccAndCons {
public AccAndCons (){}
public Account acc{get;set;}
public AccAndCons(ApexPages.StandardController controller) {
this.acc=(Account)controller.getRecord();
index = new List<integer>();
List<contact> listCon = new List<contact>();
for(integer i=0;i<=1;i++){
index.add(i);
listCon.add(new Contact());
}
}
public List<integer> index { get; set; }
public List<contact> listCon { get; set; }
public PageReference cancel1(){
PageReference pg =new PageReference(' https://ap1.salesforce.com/001/o');
pg.setredirect(true);
return pg;
}
public PageReference save() {
list<contact> cc=new list<contact>();
Account acc= new Account();
acc.name=name;
Database.SaveResult srList = Database.insert(acc);
if(srList.isSuccess()){
System.debug('list of Contacts'+listCon);
for(contact c:listCon ){
c.AccountId=acc.id;
cc.add(c);
}
System.debug('insert account'+acc);
// insert acc;
insert cc;
}
PageReference pg =new PageReference(' https://ap1.salesforce.com/'+acc.id);
pg.setredirect(true);
return pg ;
}
public String name { get; set; }
}
-------------TestClass------
@isTest (seeAllData=false)
public class AccAndCons_Test{
static testMethod void AccAndCons_testMethod(){
Test.startTest();
string name='xyz';
Account a =new Account();
a.name=name;
insert a;
contact c1=new contact(lastName='test1',AccountId=a.id);
contact c2=new contact(lastName='test2',AccountId=a.id);
contact[] con =new contact[]{c1,c2};
c1.LastName='test';
c2.LastName='test1';
AccAndCons aa=new AccAndCons();
aa.name='xyz';
aa.save();
aa.cancel1();
Test.stopTest();
}
}
Thank You.