+ Start a Discussion
Sumit  61Sumit 61 

Written test class but getting only 16% of coverage

  1. @isTest
  2. public class alu_Internship_ControllerTest{
  3.     
  4.     Id OppRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Internship Opportunity').getRecordTypeId();
  5.     
  6.     public static testMethod void testaluController(){
  7.         
  8.          Account testAccount = new Account();
  9.          testAccount.Name = 'Test Account' ;
  10.          insert testAccount;
  11.          
  12.          alu_Internship_Cycle__c ic = new alu_Internship_Cycle__c();
  13.          ic.Name = 'Test Cycle';
  14.          ic.Start_Date__c = System.Today();
  15.          ic.End_Date__c    = System.Today() + 60;
  16.          insert ic;
  17.          
  18.          Opportunity opp = new Opportunity();
  19.          opp.RecordTypeId = '0120Y000000QELh';
  20.          opp.Name = 'Test Opportunity';
  21.          opp.StageName = 'Under Discussion';
  22.          opp.CloseDate = System.Today();
  23.          opp.AccountId = testAccount.Id;
  24.          opp.Internship_Cycle__c = ic.Id;
  25.          opp.Number_of_Internships_Committed__c = 1;
  26.          opp.Opportunity_Countries__c = 'Algeria';
  27.          insert opp;
  28.          
  29.          PageReference pageRef = Page.alu_Internship_form;
  30.          Test.setCurrentPage(pageRef);
  31.            alu_Internship_Controller testIntCyc = new alu_Internship_Controller();
  32.          testIntCyc.doInsert();
  33.  
  34.                   
  35.     }
  36. }
  37. public class alu_Internship_Controller {
  38.     
  39.     Id OppRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Internship Opportunity').getRecordTypeId();
  40.     public final static String STAGE_NAME_UD = 'Under Discussion';
  41.     
  42.     public Opportunity opps {get;set;}
  43.     public string str {get;set;}
  44.     public string counName {get;set;}
  45.     public String oppdes {get; set;}
  46.     public String names {get; set;}
  47.     public string cycleName { get; set;}
  48.     public Id Idc {get; set;}
  49.     public string cmpweb { get; set;}
  50.     public string oppcitiesofOper{get;set;}
  51.     public string oppPotPro{get;set;}
  52.     public List<string> couName { get; set;}
  53.     public List<String> listOfCompany {get; set;}
  54.     public Map<Id,String> mapIdStr {get; set;} 
  55.     public Id Ids {get; set;}
  56.     public Id opIds {get; set;}
  57.     public String acc {get; set;}
  58.     public Decimal noOfPosition {get;set;}
  59.     
  60.     public alu_Internship_Controller() {
  61.         opps = new Opportunity();
  62.         opps.RecordTypeId = OppRecordTypeId;
  63.         
  64.         mapIdStr = new Map<Id,String>();
  65.         listOfCompany = new List<String>();
  66.         for(Account a: [SELECT Id, Name From Account]) {
  67.             mapIdStr.put(a.Id, a.Name);
  68.             listOfCompany.add(a.name);
  69.         }
  70.     } 
  71.     public list<SelectOption> listCycleName {
  72.         get{
  73.             list<SelectOption> listSO = new list<SelectOption>();
  74.             listSO.add(new SelectOption('', 'Select Internship Cycle'));
  75.             for (alu_Internship_Cycle__c cyc : [select Id, Name from alu_Internship_Cycle__c]) {
  76.                 if(cyc != null) {
  77.                     listSO.add(new SelectOption(cyc.Id, cyc.Name));
  78.                 }
  79.             }
  80.             return listSO;
  81.         }
  82.        set;
  83.     }
  84.     public List<SelectOption> getCountries() {
  85.  
  86.         List<SelectOption> options = new List<SelectOption>();
  87.         Schema.DescribeFieldResult fieldResult = Opportunity.Opportunity_Countries__c.getDescribe();
  88.         List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();   
  89.         for( Schema.PicklistEntry f : ple) {
  90.             options.add(new SelectOption(f.getLabel(), f.getValue()));
  91.         }
  92.         return options;
  93.     }
  94.     public List<SelectOption> getStages() {
  95.  
  96.         List<SelectOption> options = new List<SelectOption>();
  97.         Schema.DescribeFieldResult fieldResult = Opportunity.StageName.getDescribe();
  98.         List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
  99.         for(Schema.PicklistEntry f : ple){
  100.             options.add(new SelectOption(f.getLabel(), f.getValue()));
  101.         }
  102.         return options;
  103.     }
  104.     
  105.     // Logic for Save Button.
  106.     public PageReference doInsert() {
  107.        
  108.         // Storing Id and Acccount Name If acc contains same name.
  109.         Map<Id,String> mapString = new Map<Id,String>();    
  110.         for(Id tmp : mapIdStr.KeySet()) {
  111.             String name = mapIdStr.get(tmp);
  112.             if(acc.Contains(name)){
  113.                 mapString.put(tmp,acc);
  114.             }          
  115.         }
  116.         for(Id Accid: mapString.KeySet()){
  117.             Ids = Accid;
  118.         }
  119.         System.debug('Ids'+Ids);
  120.         
  121.         
  122.         // validation Logic Start
  123.         if (acc == ''){
  124.         ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select a Company Name'));
  125.         return null;
  126.         }
  127.         if (cmpweb == ''){
  128.         ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter an Company Website'));
  129.         return null;
  130.         }
  131.         if (cycleName == null){
  132.         ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter an Preffered Internship Cycle'));
  133.         return null;
  134.         }
  135.         if (noOfPosition == null){
  136.         ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select a Number of Position'));
  137.         return null;
  138.         }    
  139.              
  140.         // Iterating OpportunityId from List Of Opportunity
  141.         for(Opportunity opps : opportunitylists(Ids, cycleName)) {
  142.             opIds = opps.Id;
  143.         }
  144.         for(string s : couName) {
  145.             if(counName != null){
  146.                 counName= counName+';'+s;
  147.             }else{
  148.                 counName = s;
  149.             }            
  150.         }
  151.         if (counName == null){
  152.         ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select a Countries Operation'));
  153.         return null;
  154.         } 
  155.     
  156.         Account ac = new Account();        
  157.         if(opIds != null) {
  158.             Opportunity opp = new Opportunity();
  159.             opp.Id = opIds;
  160.             opp.Opportunity_Countries__c = '';
  161.             opp.Opportunity_Description__c = oppdes;
  162.             opp.AccountId = Ids;
  163.             opp.Internship_Cycle__c = cycleName;
  164.             opp.Number_of_Internships_Committed__c = noOfPosition;
  165.             opp.Opportunity_Countries__c = counName;
  166.             update opp;
  167.             system.debug('Opp Update'+ opp);
  168.             
  169.             ac.Id= opp.AccountId;
  170.             ac.Website = cmpweb;
  171.             update ac;
  172.             system.debug('ACC Update'+ ac);
  173.             
  174.         } else {
  175.             Opportunity opp = new Opportunity();
  176.             opp.RecordTypeId = OppRecordTypeId;
  177.             opp.name = '-UG Internship-';
  178.             opp.Opportunity_Description__c = oppdes;
  179.             opp.CloseDate = System.today();
  180.             opp.AccountId = Ids;
  181.             opp.StageName = STAGE_NAME_UD;
  182.             opp.Internship_Cycle__c = cycleName;
  183.             opp.Opportunity_Countries__c = counName;
  184.             opp.Number_of_Internships_Committed__c = noOfPosition;
  185.             insert opp;
  186.             system.debug('Opp Insert'+ opp);
  187.             
  188.             ac.Id= opp.AccountId;
  189.             ac.Website = cmpweb;
  190.             update ac; 
  191.             system.debug('ACC Update'+ ac);
  192.         }      
  193.         pagereference ref = new pagereference('/apex/alu_Internship_Thanks_Msg');
  194.         ref.setredirect(true);
  195.         return ref;
  196.     }  
  197.      
  198.     // getting list of Opportunity    
  199.     public List<Opportunity> opportunitylists (Id Ids, String cycleName) {
  200.         return [SELECT Id, AccountId, Internship_Cycle__c 
  201.                 FROM Opportunity 
  202.                 WHERE AccountId =:Ids 
  203.                 AND Internship_Cycle__c = : cycleName 
  204.                 AND Stagename=: STAGE_NAME_UD
  205.                 ];
  206.     }
  207. }
  208.  
prateek jainprateek jain
Hi
Try  writing @isTest(SeeAllData=true) instead @isTest 
If still face issue let me know. I would be happy  to help you out
Thanks 
 
prateek jainprateek jain
Please test you select option methods-getConties,getStages like
 alu_Internship_Controller tempObject= new alu_Internship_Controller();
 List<SelectOption> options1 = tempObject.getStages();
 List<SelectOption> options2 = tempObject.getCountries();
let me known if it works
Thanks 
 
Sumit  61Sumit 61
Thanks @prateek jain, I have made the changes but Its not covering  code inside PageReference

// public PageReference doInsert()
prateek jainprateek jain
Hi 
Try this code

PageReference pageRef = Page.yourPageName;
 Test.setCurrentPage(pageRef);
 pageRef.getParameters().put('id',testAccount.id);
ApexPages.StandardController sc = new ApexPages.standardController(testAccount);
alu_Internship_Controller controller = new alu_Internship_Controller(sc);
 controller.doInsert();
i Think you are missing Account id 
let me know if any issue
Thanks 
Sumit  61Sumit 61
Thanks a lots! @prateek jain I have added that. but getting this error on this line Constructor not defined: [alu_Internship_Controller].<Constructor>(ApexPages.StandardController)
//Constructor not defined: [alu_Internship_Controller].<Constructor>(ApexPages.StandardController)