+ Start a Discussion
selva kumar 14selva kumar 14 

Testcase execution error in sandbox

Hi Everyone,

I have executed a test case in sandbox. But it throws the follwoing error.

Error:
System.QueryException: List has no rows for assignment to SObject

Stack Trace:
Class.TestBulkPricebookOpp.validatepric: line 6, column 1

I couldn't able to resolve it. My test class coding is given below.

@isTest
public class TestBulkPricebookOpp {
    Public static testmethod void validatepric(){
    Id pbID = null;

    pbID = [Select Name, Description From Pricebook2 where Name = 'Bulk Price Book'].Id;
    List<RecordType> recordtypId =  [Select SobjectType, Name, Id, DeveloperName From RecordType  Where SobjectType = 'Opportunity' And  Name = 'k) Bulk Services'];
Account obj = new Account( Name = 'Test', Zip_Code__c='29801');
insert obj;

Opportunity objOpp = new Opportunity(Name = 'newTest',recordtypeId=recordtypId[0].Id,AccountId = obj.Id,StageName ='Prospecting',CloseDate=Date.today());
        objOpp.Pricebook2Id=pbID;       
insert objOpp;

Opportunity objUOppt = new opportunity(Id =objOpp.Id );
update objUOppt ;

}
}


Please any one correct me in the coding part. Thanks in advance.
Best Answer chosen by selva kumar 14
SantoshChitalkarSantoshChitalkar
insert the proper opportunity records. It will solve your problem. In order to confirm that there is a problem with opportunity use following code, it will run test class successfully - 

@isTest (SeeAllData == true)
public class TestBulkPricebookOpp {
    Public static testmethod void validatepric(){
    Id pbID = null;

    pbID = [Select Name, Description From Pricebook2 where Name = 'Bulk Price Book'].Id;
    List<RecordType> recordtypId =  [Select SobjectType, Name, Id, DeveloperName From RecordType  Where SobjectType = 'Opportunity' And  Name = 'k) Bulk Services'];
Account obj = new Account( Name = 'Test', Zip_Code__c='29801');
insert obj;

Opportunity objOpp = new Opportunity(Name = 'newTest',recordtypeId=recordtypId[0].Id,AccountId = obj.Id,StageName ='Prospecting',CloseDate=Date.today());
        objOpp.Pricebook2Id=pbID;       
insert objOpp;

Opportunity objUOppt = new opportunity(Id =objOpp.Id );
update objUOppt ;

}
}
 

All Answers

SantoshChitalkarSantoshChitalkar
insert the proper opportunity records. It will solve your problem. In order to confirm that there is a problem with opportunity use following code, it will run test class successfully - 

@isTest (SeeAllData == true)
public class TestBulkPricebookOpp {
    Public static testmethod void validatepric(){
    Id pbID = null;

    pbID = [Select Name, Description From Pricebook2 where Name = 'Bulk Price Book'].Id;
    List<RecordType> recordtypId =  [Select SobjectType, Name, Id, DeveloperName From RecordType  Where SobjectType = 'Opportunity' And  Name = 'k) Bulk Services'];
Account obj = new Account( Name = 'Test', Zip_Code__c='29801');
insert obj;

Opportunity objOpp = new Opportunity(Name = 'newTest',recordtypeId=recordtypId[0].Id,AccountId = obj.Id,StageName ='Prospecting',CloseDate=Date.today());
        objOpp.Pricebook2Id=pbID;       
insert objOpp;

Opportunity objUOppt = new opportunity(Id =objOpp.Id );
update objUOppt ;

}
}
 
This was selected as the best answer
ManojjenaManojjena
selva kumar 14selva kumar 14
Thanks SantoshChitalkar.

Test case is working fine.
selva kumar 14selva kumar 14
thanks for you help Manoj.

I have corrected my test case.