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
Amit Jadhav 13Amit Jadhav 13 

list has no rows for assignment to sobject test class

I'm new in test class plese help me below code to write test class
public class FindProject {
   public Id OppObj{get;set;}
    public string Accname {get;set;}
    public string ReciName {get;set;}
    public string ProjectName;
    public opportunity objopp{
        get{
           objopp=[Select id,name,Account.Name,Owner.Name,Amount from opportunity where id=:OppObj limit 1]; 
        return objopp;
        }set;
    }
    public OpportunityLineItem objPro{
        get{
            objPro=[Select id,Projects__r.name,Start_Date__c from OpportunityLineItem where opportunity.id=:OppObj limit 1];
            return objPro;
        }set;
    }
    
    public Project__c proobj{
        get{
        ProjectName=[Select id,Projects__r.name from OpportunityLineItem where opportunity.id=:OppObj limit 1].Projects__r.name;
        proobj=[Select id,Program_Manager__r.name from Project__c where name=:ProjectName];
        return proobj;
        }set;
    }
}

Test Class:
@isTest
public class FindProjectTest {

    @isTest
    public static void testDataSetup(){
         Product2 prod = new Product2(Name = 'Laptop X200', Family = 'Hardware');
        insert prod;
        
        Id pricebookId = Test.getStandardPricebookId();
        
        PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = pricebookId, 
                                                          Product2Id = prod.Id, 
                                                          UnitPrice = 10000, 
                                                          IsActive = true);
        insert standardPrice;
        
        Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
        insert customPB;
        
        PricebookEntry customPrice = new PricebookEntry(Pricebook2Id = customPB.Id, 
                                                        Product2Id = prod.Id, 
                                                        UnitPrice = 12000, 
                                                        IsActive = true);
        insert customPrice;
        Account acc = new Account(name='Test Account');
        insert acc;
        opportunity opp = new opportunity(name='Test Opportunity', 
                                          AccountId=acc.id,
                                          CloseDate =Date.newInstance(2016, 12, 9),
                                          stageName='Closed Lost',
                                          ForecastCategoryName='Pipeline',
                                          PaymentTerms__c='Net 30',
                                          type='New Business',
                                          Amount=5676,
                                          Pricebook2Id=customPB.Id
                                         );
        insert opp;
        
        OpportunityLineItem oppLineItem = new OpportunityLineItem(OpportunityId=opp.id,
                                                                  TotalPrice =664557,
                                                                  End_Date__c=Date.newInstance(2016, 12, 9),
                                                                  Quantity =56,
                                                                  PricebookEntryId = customPrice.Id);
        insert oppLineItem;
        
         FindProject obj = new FindProject();
        opportunity objopp = obj.objopp;
        OpportunityLineItem objPro = obj.objPro;
        
       
    }
    
}

​​​​​​​
Amit Jadhav 13Amit Jadhav 13
Can any one help me in test class
SUCHARITA MONDALSUCHARITA MONDAL

Hi Amit,

I guess, for line 47 and 48. it should be as 

FindProject obj = new FindProject();
obj.objopp = opp;
obj.objPro = oppLineItem ;

 

Share your thoughts,

Thanks,
Sucharita