function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ravi kumar 292Ravi kumar 292 

How to Pass ID in Test Class

Hi,

I have two classes A and B. While writing the test class am able to get the code coverage of Class A. But I was stucked at Class B.
Please help on this. Am getting the error as List Has No Row to Assign for SObject at line numer 5 in class B.

Class A:

public with sharing class A {
    
    public string rid {get; set;}
    public Opportunity o {get;set;}
    public Account a {get; set;}
    public Contact c {get;set;}
    private ApexPages.StandardController standardController;
    public A(ApexPages.StandardController standardController)
    {
        rid = ApexPages.CurrentPage().getparameters().get('id');
        this.standardController = standardController; 
        try{
            o=[Select id,Name,AccountId,Nationality__c,Purpose_of_Loan__c,Sanction_Amount__c,Application_Creation_Date__c,Original_Face__c,Loan_Tenor_in_Month__c,Other_Loan_Purpose__c from Opportunity where id= :rid];
            a=[Select Id,Loan_Amount__c, Sector__c from Account where id = :o.AccountId];
            c=[Select Id,FirstName,LastName,Birthdate,Borrower_Type__c,Name_of_Firm_Company_Establishment_In__c,Gender__c,Age1__c,PAN_ID__c,Residence_Type__c, Phone, Annual_Income_Gross__c,Organisation__c,MailingStreet, MailingCity,MailingState, MailingCountry,MailingPostalCode,Marital_Status__c,Email,MobilePhone From Contact where AccountId = :a.Id]; 
        }catch(Exception e){
            o = new Opportunity();
            a = new Account();
            c = new Contact();
        }    
    }
    
    public PageReference method(){
        B b1 = new B(); // instantiate Class
        b1.B(rid);                               
        return null;
    }   
}


Class B:

Public class B{
    
    public void B(string rid){ 
        
        Opportunity o=[Select id,Name,AccountId,Nationality__c,Purpose_of_Loan__c,Sanction_Amount__c,Application_Creation_Date__c,Original_Face__c,Loan_Tenor_in_Month__c,Other_Loan_Purpose__c from Opportunity where id= :rid];
        system.debug('print me account id'+o.AccountId);
        Account a=[Select Id,Loan_Amount__c, Sector__c from Account where id = :o.AccountId];
        system.debug('Print me account id'+a.id);
        List<Contact> c=[Select Id,FirstName,LastName,Birthdate,Borrower_Type__c,Name_of_Firm_Company_Establishment_In__c,Gender__c,Age1__c,PAN_ID__c,Residence_Type__c, Phone, Annual_Income_Gross__c,Organisation__c,MailingStreet, MailingCity,MailingState, MailingCountry,MailingPostalCode,Marital_Status__c,Email,MobilePhone From Contact where AccountId = :a.Id]; 
        system.debug('Contact' + c);
   .
   .
.
.
.
.
.

}
}

Test Class:

@isTest
Public class AB_Test{

    Public static testMethod void testcase(){
        
     
        Opportunity opp = new Opportunity();
        opp.Name = 'Test';
        opp.StageName = 'Rejected';
        opp.Collateral_Type__c = 'Other';
        
        insert opp;
        
       String rid = opp.id;
       system.debug('-----'+rid);
        
        
        ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(opp);
        A hc = new A(sc);
        hc.method();
        
      B hc1 = new B();
      hc1.B(rid);
     
        
        
    }
}

Please help on this... Thanks in advance..
 
Best Answer chosen by Ravi kumar 292
Pankaj_GanwaniPankaj_Ganwani
 Public static testMethod void testcase(){
       
        Account acc = new Account();
        acc.Name = 'AccountTest';
        acc.Sector__c = 'Education';
        insert acc;

       Opportunity opp = new Opportunity();
        opp.Name = 'Test';
        opp.StageName = 'Rejected';
        opp.Collateral_Type__c = 'Other';
        opp.AccountId = acc.Id;
        insert opp;

        String rid = opp.id;
       
        String aid= acc.id;
        system.debug('Aid --'+aid);
        Contact con = new Contact();
        con.LastName = 'Ram';
        con.AccountId = acc.Id;
        insert con;
        String cid = con.id;
        system.debug('ConId---'+cid);
        system.debug('-----'+rid);
        ApexPages.CurrentPage().getParameters().put('id',rid);
        ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(opp);
        A hc = new A(sc);
       // hc.rid=rid;
        hc.Hunter();
}

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi,

Please use below mentioned statements instead. I think this will fix your issue:

ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(opp);
        A hc = new A(sc);
        hc.rid = rid;        
hc.method();
        
You don't need to explicitly call B class.
Ravi kumar 292Ravi kumar 292
Hi Pankaj,

I tried with your code but its not working. Still am getting the error as List has no rows to assign to Sobject at Opportunity Query in Class B.

Help on this..

 
Pankaj_GanwaniPankaj_Ganwani
Hi Ravi,

Try with this:

ApexPages.CurrentPage().getparameters().put('id', rid);
ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(opp);
        A hc = new A(sc);
        hc.method();
 
Ravi kumar 292Ravi kumar 292
Thanks for your reply.. I already done with that code but didnt get.. Same error with that also..
Pankaj_GanwaniPankaj_Ganwani
Okay, Just put the debug statement in method at the beginning and check what is coming there.
Ravi kumar 292Ravi kumar 292
I am getting the Id of the opportunity in rid. Now its working at Opportunity query bt nw getting same error at Account Query.
Pankaj_GanwaniPankaj_Ganwani
Hi Ravi,

This is error you are getting since you have not inserted any Account record in test class. Just insert one Account record in test class and associate the Opportunity with that Account.
Ravi kumar 292Ravi kumar 292
Hi Pankaj,

I have inserted Account and Contact records in test class. but am unable to associate these with opportuntiy. Can u please let me know how to do this.

This is my code

 Public static testMethod void testcase(){
        Opportunity opp = new Opportunity();
        opp.Name = 'Test';
        opp.StageName = 'Rejected';
        opp.Collateral_Type__c = 'Other';
        insert opp;
        String rid = opp.id;
        Account acc = new Account();
        acc.Name = 'AccountTest';
        acc.Sector__c = 'Education';
        insert acc;
        String aid= acc.id;
        system.debug('Aid --'+aid);
        Contact con = new Contact();
        con.LastName = 'Ram';
        insert con;
        String cid = con.id;
        system.debug('ConId---'+cid);
        system.debug('-----'+rid);
        ApexPages.CurrentPage().getParameters().put('id',rid);
        ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(opp);
        A hc = new A(sc);
       // hc.rid=rid;
        hc.Hunter();
}

Thanks
Pankaj_GanwaniPankaj_Ganwani
 Public static testMethod void testcase(){
       
        Account acc = new Account();
        acc.Name = 'AccountTest';
        acc.Sector__c = 'Education';
        insert acc;

       Opportunity opp = new Opportunity();
        opp.Name = 'Test';
        opp.StageName = 'Rejected';
        opp.Collateral_Type__c = 'Other';
        opp.AccountId = acc.Id;
        insert opp;

        String rid = opp.id;
       
        String aid= acc.id;
        system.debug('Aid --'+aid);
        Contact con = new Contact();
        con.LastName = 'Ram';
        con.AccountId = acc.Id;
        insert con;
        String cid = con.id;
        system.debug('ConId---'+cid);
        system.debug('-----'+rid);
        ApexPages.CurrentPage().getParameters().put('id',rid);
        ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(opp);
        A hc = new A(sc);
       // hc.rid=rid;
        hc.Hunter();
}
This was selected as the best answer
Ravi kumar 292Ravi kumar 292
Thanks Pankaj.. Its Working..
Pankaj_GanwaniPankaj_Ganwani
Hi Ravi,

You are welcome. Please mark it as best answer so that others can also refer this post if they stuck at similar kind of problems.