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
oceanofstarsoceanofstars 

help in test class

Hi All I have one class. I wrote a test class for that its save but when run test i m getting error like this 

 

System.QueryException: List has no rows for assignment to SObjectClass.MagnysApprovalController.<init>: line 9, column 10 Class.Test_Pages.testMagnysApprovalController: line 24, column 46 External entry point

My class is 

public with sharing class MagnysApprovalController{
    Private string comment = '';
    public String oppId= ApexPages.currentPage().getParameters().get('OppId');
    public Opportunity o;
    public List<Quote__c> qlist;
    public Quote__c q;
    public List<QuoteLine__c> TempQuoteLine;
    public MagnysApprovalController(){
      o = [select Id, Name, Owner.Name, Owner.Employee_ID__c, AccountId, Account.Name, Scope_of_Work__c, Existing_Services_Replaced__c, Amount from Opportunity
                 where id= :oppId];
       qlist = [Select Id, Total_Quote_Amount__c, Primary__c, Opportunity__c, Name, Contract_Term__c, Contract_Start_Date__c, AverageDiscountPercentage__c From Quote__c 
                where Opportunity__c = :oppId and Primary__c = true limit 1];
      if( qlist.size()>0 ){
        q = qlist.get(0);
          TempQuoteLine = [select id, MasterProduct__c, Total_Sale__c, MagnysServiceCode__c, MagnysRevenue__c, Term__c, Previous_Price__c, MasterProduct__r.Name, MagnysServiceCode__r.Name from QuoteLine__c where Quote__c = :q.Id];
      }  
    } 
    public Opportunity getOpp(){
        return o; 
    }
    public Quote__c getQuote(){
        return q;
    }
    public List<QuoteLine__c> getqlList(){
        return TempQuoteLine;    
    }
    public string getComment()
    {
        return comment;
    }
    public void setComment(string Value){
        comment= Value;
    }
    public PageReference reject() {
        PageReference pr = null;
        try{        
            ProcessInstanceWorkitem piwi = [Select ProcessInstance.Status, ProcessInstance.TargetObjectId, ProcessInstanceId,OriginalActorId,Id,ActorId From ProcessInstanceWorkitem
            where ProcessInstance.TargetObjectId = : oppId and ProcessInstance.Status = 'Pending' and OriginalActorId =: UserInfo.getUserId() order by CreatedDate desc limit 1];    
            Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
            req.setComments(comment);
            req.setAction('Reject');
            req.setWorkitemId(piwi.Id);
            // Submit the request for approval      
            Approval.ProcessResult result =  Approval.process(req);        
            pr = new PageReference('/home/home.jsp');
        }
        catch(Exception ex){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,ex.getMessage()));
        }
        return pr;
    }
  public PageReference reject1() {
        PageReference pr = null;
        try{        
            ProcessInstanceWorkitem piwi = [Select ProcessInstance.Status, ProcessInstance.TargetObjectId, ProcessInstanceId,OriginalActorId,Id,ActorId From ProcessInstanceWorkitem
            where ProcessInstance.TargetObjectId = : oppId and ProcessInstance.Status = 'Pending' and OriginalActorId =: UserInfo.getUserId() order by CreatedDate desc limit 1];
            Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
            req.setComments(comment);
            req.setAction('Reject');
            req.setWorkitemId(piwi.Id);
            // Submit the request for approval      
            Approval.ProcessResult result =  Approval.process(req);        
            //pr = new PageReference('/home/home.jsp');
            pr = new PageReference('/apex/mobileApprovalList?sfdc.tabName=01r40000000LnKP');
        }
        catch(Exception ex){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,ex.getMessage()));
        }
        return pr;
    }
    public PageReference approve(){
        PageReference pr = null;
        try{  
            ProcessInstanceWorkitem piwi = [Select ProcessInstance.Status, ProcessInstance.TargetObjectId, ProcessInstanceId,OriginalActorId,Id,ActorId From ProcessInstanceWorkitem
            where ProcessInstance.TargetObjectId = : oppId and ProcessInstance.Status = 'Pending' and OriginalActorId =: UserInfo.getUserId() order by CreatedDate desc limit 1];
            Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
            req.setComments(comment);
            req.setAction('Approve');
            req.setWorkitemId(piwi.Id);
            // Submit the request for approval      
            Approval.ProcessResult result =  Approval.process(req);
            pr = new PageReference('/home/home.jsp');
        }
        catch(Exception ex){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,ex.getMessage()));
        }
        return pr;
    }
    public PageReference approve1(){
        PageReference pr = null;
        try{  
            ProcessInstanceWorkitem piwi = [Select ProcessInstance.Status, ProcessInstance.TargetObjectId, ProcessInstanceId,OriginalActorId,Id,ActorId From ProcessInstanceWorkitem
            where ProcessInstance.TargetObjectId = : oppId and ProcessInstance.Status = 'Pending' and OriginalActorId =: UserInfo.getUserId() order by CreatedDate desc limit 1];
            Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
            req.setComments(comment);
            req.setAction('Approve');
            req.setWorkitemId(piwi.Id);
            // Submit the request for approval      
            Approval.ProcessResult result =  Approval.process(req);
            //pr = new PageReference('/home/home.jsp');
            pr = new PageReference('/apex/mobileApprovalList?sfdc.tabName=01r40000000LnKP');
        }
        catch(Exception ex){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,ex.getMessage()));
        }
        return pr;
    }     
}

 

And test class is 

 

  public static testMethod void testMagnysApprovalController() 
    {      
        Test.startTest();
        PageReference result = Page.MagnysApproval;
        Test.setCurrentPage(result);
                        
        Account account= new Account(Name='Test Account', Type='Standard Account', Federal_Tax_ID_No__c='01010101', 
        Main_BTN__c='(000)000-7654', ShippingStreet='000 Test St', ShippingCity='Dallas', 
        ShippingState='Tx', ShippingPostalCode='75050');
        insert account; 
       
        Opportunity opp = new Opportunity(Name='Test Opp1', Existing_Services_Replaced__c = 'none', AccountId=account.Id, Scope_of_Work__c='Test', 
        Proposal_Issued_Date__c=Date.Today(), StageName='Closed Won',CloseDate=System.today());
        insert opp;
        
        Quote__c q = new Quote__c(Contract_Term__c = '12',Name='Test',Opportunity__c = opp.id);
        insert q;
        Profile p = [select id from Profile limit 1];
         User u = new User(ProfileId = p.id,LastName = 'Test', FirstName = 'Test', Username = 'testing456@test.com', Alias='test', CommunityNickname='test', Email='test@email.com',TimeZoneSidKey='America/Chicago',EmailEncodingKey='ISO-8859-1', LanguageLocaleKey='en_US', LocaleSidKey='en_US',IsActive=false);
                
        insert u;
         
 
        MagnysApprovalController extension = new MagnysApprovalController();
    
        extension.getComment(); 
        extension.reject();
        extension.reject1();
        extension.approve();
        extension.approve1();              
    }  
    

SanchSanch

you are querying data and assigning to a single object. Try using List rather than using a single object.

 

List<Opportunity> test = [Select Id From Opportunity];

oceanofstarsoceanofstars

When i put List<Opportunity>  o  = [Select Id From Opportunity];

in test calss instead of  

Opportunity opp = new Opportunity(Name='Test Opp1', Existing_Services_Replaced__c = 'none', AccountId=account.Id, Scope_of_Work__c='Test', 
        Proposal_Issued_Date__c=Date.Today(), StageName='Closed Won',CloseDate=System.today());
        insert opp;

 

I am getting a error 

Initial term of field expression must be a concrete SObject: LIST<Opportunity> at line 290 column 88

 

oceanofstarsoceanofstars

Can any one help me on this prob plz

Greg HGreg H

The test class doesn't know the oppId. In your test you insert all the records appropriately then call the controller for your page but the controller doesn't know the Opportunity Id to use.

-greg

oceanofstarsoceanofstars

I got your point but what is the best way in order to overcome this issue

Greg HGreg H

It seems that you are relying on the query string parameters being passed in the URL for your OppId variable. So you need to add the parameter to the URL in your test class (so-to-speak). You could try adding the following line somewhere after you insert the Opportunity sobject "opp" variable but before you instantiate the MagnysApprovalController controller:

 

PageReference.getParameters().put('OppId', opp.Id);

 

-greg