You need to sign in to do that
Don't have an account?
Ravi 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..
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..
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
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.
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..
Try with this:
ApexPages.CurrentPage().getparameters().put('id', rid);
ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(opp);
A hc = new A(sc);
hc.method();
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.
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
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();
}
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.