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
BB 

How can i imporve my code coverage, i currently have 47% but i dont now how to improve it anymore

Here is what i tried
apex class:
public with sharing class SolutionWrapper {
    public ApexPages.StandardSetController controller;
    public Opportunity opp{get; set;}
    public Solutions__c oppId{get; set;}
    public Solutions__c sol{get; set;}
    public Solutions__c solution{get; set;}
    public Account acc{get; set;}
  
    public SolutionWrapper(ApexPages.StandardSetController controller) {
        try{
            solution = new Solutions__c();
            solution = (Solutions__c)controller.getRecord();
            if(solution.Id != null){
                oppId = [SELECT id, Solutions__c.Opportunity__c 
                    FROM Solutions__c
                WHERE id =: solution.Id
                LIMIT 1];
                
                opp = [Select id,Name, AccountId, CurrencyIsoCode  from 
                         Opportunity where id =: oppId.Opportunity__c  LIMIT: 1];
            }
            
            if(opp.id !=null){
                sol = [SELECT id,Name, Mail_Merge_Id__c,Solution_Country__c, Solutions__c.Opportunity__c 
            FROM Solutions__c
            WHERE Solutions__c.Opportunity__c =: opp.id
            LIMIT 1];
                 acc = [Select id,Name,Country__c from 
                         Account where id=:opp.AccountId LIMIT: 1];
            }
            
            
        }
        catch(Exception e){
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR,e.getMessage()));
        }

    }

}



apex test class:
@isTest 
public class SolutionWrapperTest {

         static testMethod void testMethodOpp(){
             Account acc = new Account(Name='test', Country__c='test'); 
              insert acc; 
             Opportunity opp = new Opportunity(Name='test', AccountId=acc.id, CurrencyIsoCode='GBP', StageName = 'Good',
              CloseDate =  date.today());
             insert opp;
             Solutions__c sol = new Solutions__c( Opportunity__c= opp.id, CurrencyIsoCode='GBP');       
               insert sol;          
             
             List<Solutions__c> listSol = new List<Solutions__c>();
             listSol.add(sol);
             PageReference pageRef = Page.NewVisualForcePage;
             Test.setCurrentPage(pageRef); 
             
            Test.startTest();
             ApexPages.StandardSetController stdController = new ApexPages.StandardSetController(listSol);
             SolutionWrapper testSolution = new SolutionWrapper(stdController);
            Test.stopTest();

            }
        
        
    }
 
Best Answer chosen by B
BB
Solution id is different from the one that i inserted in the test class. I inserted a dummy value (eq 'test') on a field like Currency. After that i selected from the db based on Currency instead of id.

All Answers

Dushyant SonwarDushyant Sonwar
Try setting the created solution record like this below.
ApexPages.StandardSetController stdController = new ApexPages.StandardSetController(listSol);
stdController.setSelected(listSol);

Hope this helps.



 
Raj VakatiRaj Vakati
try this
@isTest 
public class SolutionWrapperTest {

         static testMethod void testMethodOpp(){
             Account acc = new Account(Name='test', Country__c='test'); 
              insert acc; 
             Opportunity opp = new Opportunity(Name='test', AccountId=acc.id, CurrencyIsoCode='GBP', StageName = 'Good',
              CloseDate =  date.today());
             insert opp;
             Solutions__c sol = new Solutions__c( Opportunity__c= opp.id, CurrencyIsoCode='GBP');       
               insert sol;          
             
             List<Solutions__c> listSol = new List<Solutions__c>();
             listSol.add(sol);
             PageReference pageRef = Page.NewVisualForcePage;
				pageRef.getParameters().put('id', String.valueOf(sol.Id));

             Test.setCurrentPage(pageRef); 
             
            Test.startTest();
             ApexPages.StandardSetController stdController = new ApexPages.StandardSetController(listSol);
			stdController.setSelected(listSol);

             SolutionWrapper testSolution = new SolutionWrapper(stdController);
            Test.stopTest();

            }
        
        
    }

 
BB
@Raj is still the same code coverage :(
@Dushyant Sonwar thx for trying as well

It shows that the test is not working in the class starting from this line of code in the apex class 
         opp = [Select id,Name, AccountId, CurrencyIsoCode  from 
                         Opportunity where id =: oppId.Opportunity__c  LIMIT: 1];
BB
@Raj i wanted to give a like srry , misclicked :(. Still not figure it out 
BB
I receive this error , i just found it in logs 
"common.apex.runtime.impl.ExecutionException: List has no rows for assignment to SObject"|0x4888dd3c
BB
Solution id is different from the one that i inserted in the test class. I inserted a dummy value (eq 'test') on a field like Currency. After that i selected from the db based on Currency instead of id.
This was selected as the best answer