+ Start a Discussion
Harsh P.Harsh P. 

Not getting logic of test class


I am new in Test class.I am not getting Logic for this test class ....Help.
public with sharing class MonthlyParkerHistoryCntl {
    public List<M_Parker__c> mnthlyParkerList{get;set;}
    public String urlAmount{get;set;}
    public String locationID{get;set;}
    public String amnt{get;set;}
    public String mParkerID{get;set;}
    public Map<ID,M_Parker__c> mparkerMap;
    //public Map<ID,M_Parker__c> mparkerMap;
    public boolean tableData{get;set;}
    public Contact objCon{get;set;}
    
    
    public String successError{get;set;}
    public String msg{get;set;}
    public boolean isError{get;set;}
    
    public MonthlyParkerHistoryCntl(){
        
        User objUser = new User();
        objUser = [SELECT ID,ContactID FROM User WHERE ID=: UserInfo.getUserId() LIMIT 1];
        
        objCon = new Contact();
        if(objUser.ContactID != null )
            objCon = [SELECT ID,AccountID,FirstName,LastName,Email,Phone,MailingStreet,MailingCity,MailingState,MailingPostalCode,MailingCountry,Tenant__c,Auto_Make__c,Auto_Model__c,Auto_Color__c,Auto_Year__c,Lic_Plate_State__c FROM Contact WHERE ID=: objUser.ContactID];
        
        if(objCon.AccountID != null){
            mparkerMap = new Map<ID,M_Parker__c>();
            mnthlyParkerList = new List<M_Parker__c>();
            for(M_Parker__c parker: [SELECT ID,Name,FacID__r.Name,FacID__c,Start_Date__c,End_Date__c,FacID__r.Revenue_Type__c, FacID__r.Location_Phone__c,FacID__r.Monthly_Rate__c FROM M_Parker__c WHERE Account__c =: objCon.AccountID ORDER By Name DESC]){
                mnthlyParkerList.add(parker);
                mparkerMap.put(parker.ID,parker);
            }
        }
        tableData = true;
        isError = false;
    }
    
    public pagereference displaymParkerInfo(){
        System.debug('Inside -displaymParkerInfo '+mParkerID );
        if(mparkerMap.containsKey(mParkerID)){
            tableData = false;          
            M_Parker__c parker = mparkerMap.get(mParkerID);
            
            System.debug('parker -> '+parker);
        }
        return null;
    }
    
    public void backtoParkerHistory(){
        tableData = true;
        isError = false;
    }
     
    public pagereference renewParking(){
        
        if(locationID != null && amnt!=null){
            return new PageReference('https://techiladev-atlanticparking.cs77.force.com/s/parkagreementclone?locID='+locationID+'&amount='+amnt);   
        }       
        return null;
    } 
    
    public Pagereference cancelParking(){
        if(mParkerID != null && mParkerID != ''){
            if(mparkerMap.containsKey(mParkerID)){
                M_Parker__c parker = mparkerMap.get(mParkerID);
                parker.End_Date__c = System.today();
                update parker;
            }
            
        }
        return null;
    } 

    public void saveUserInfo(){
        if(objCon != null){
            update objCon;
            
            isError = true;
            successError = 'success';
            msg = '"Details Saved Successfully"';
        }
        
    }
    
}
Best Answer chosen by Harsh P.
Raj VakatiRaj Vakati
Try this
 
@isTest
private class MonthlyParkerHistoryCntlTest {
    @isTest static void myTest() {
       
	   M_Parker__c m = new M_Parker__c();
	   m.Name = 'Demo';
	   // add all fields 
	   insert m ; 

	   Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
		
			
			Account a = new Account(name='test acc',phone='9494146144');
        insert a;
        Account a2 = new Account(name='test acc 2',phone='9494146144');
        insert a2;
        Contact con = new Contact(accountid=a.id,lastname='test con',email='lnarasimha823@gmail.com');
        insert con;
        Contact con2 = new Contact(accountid=a.id,lastname='test con2',email='lnarasimha823@gmail.com');
		insert con2;
		
		
		
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
						 AccountId = a.Id , 
						 ContactId = con.Id ,
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
		System.runAs(uu){
		
		
	   
	   MonthlyParkerHistoryCntl mCon= new MonthlyParkerHistoryCntl();
	   
	   mCon.objCon = con2 ;
	   mCon.displaymParkerInfo();
	   mCon.backtoParkerHistory();
	   mCon.renewParking();
	   mCon.cancelParking();
	 
  mCon.saveUserInfo();
 
	   
		}
    }
}

 

All Answers

Raj VakatiRaj Vakati
Try this
 
@isTest
private class MonthlyParkerHistoryCntlTest {
    @isTest static void myTest() {
       
	   M_Parker__c m = new M_Parker__c();
	   m.Name = 'Demo';
	   // add all fields 
	   insert m ; 

	   Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
		
			
			Account a = new Account(name='test acc',phone='9494146144');
        insert a;
        Account a2 = new Account(name='test acc 2',phone='9494146144');
        insert a2;
        Contact con = new Contact(accountid=a.id,lastname='test con',email='lnarasimha823@gmail.com');
        insert con;
        Contact con2 = new Contact(accountid=a.id,lastname='test con2',email='lnarasimha823@gmail.com');
		insert con2;
		
		
		
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
						 AccountId = a.Id , 
						 ContactId = con.Id ,
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
		System.runAs(uu){
		
		
	   
	   MonthlyParkerHistoryCntl mCon= new MonthlyParkerHistoryCntl();
	   
	   mCon.objCon = con2 ;
	   mCon.displaymParkerInfo();
	   mCon.backtoParkerHistory();
	   mCon.renewParking();
	   mCon.cancelParking();
	 
  mCon.saveUserInfo();
 
	   
		}
    }
}

 
This was selected as the best answer
Harsh P.Harsh P.
Thanks @Raj...........