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
Che SFDCChe SFDC 

Need help with Test Class

Dear all, I`m new to Apex programming and I need help with test class. Below is my apex class and i`m not getting any test coverage on this class.  Can someone please help me with test code to get me started? Thanks in advance.

 

public with sharing class Example {

    String oppId;
    String ownerId;
    private final Opportunity Oppty;
 
    public Example (ApexPages.StandardController stdController) {
        oppId = stdController.getId();
        Oppty = (Opportunity) stdController.getRecord();
        ownerId = oppty.OwnerId;
        oppty.StageName = 'Identified Opportunity';
         oppty.Probability = 0;
    }



public transient Map<String, Decimal> probabilityStageNameMap;

public PageReference changeStageName() {

if (probabilityStageNameMap == null) {
 probabilityStageNameMap = new Map<String, Decimal>();
for (OpportunityStage oppStage : [Select MasterLabel, DefaultProbability
                                    From OpportunityStage]) {
 probabilityStageNameMap.put(oppStage.MasterLabel, oppStage.DefaultProbability);
   }
  }

 if (probabilityStageNameMap.containsKey(Oppty.StageName)) {
   Oppty.Probability = probabilityStageNameMap.get(Oppty.StageName);
 
 }

  return null;
 }
 
           public PageReference dosave()
  {
    insert oppty;
    return new PageReference('/' + 'apex/OppNEWLayout?id= + oppty.id'); 
    }


}
 


 

Best Answer chosen by Che SFDC
ManojjenaManojjena
Hi Chetan,

Try with below code and let me know any issue .
@isTest
public class TestExample{
  public static testmethod void unitTest(){
	Opportunity opp=new opportunity();
		opp.closeDate=System.today();
		opp.Name='TestOpportunity';
		opp.stageName='Identified Opportunity';
	ApexPages.StandardController stdCont =new ApexPages.StandardController(opp);
	Example  exam=new Example (stdCont );
		exam.changeStageName();
		exam.dosave();
   }
}

Thanks 
Manoj

All Answers

ManojjenaManojjena
Hi Chetan,

Try with below code and let me know any issue .
@isTest
public class TestExample{
  public static testmethod void unitTest(){
	Opportunity opp=new opportunity();
		opp.closeDate=System.today();
		opp.Name='TestOpportunity';
		opp.stageName='Identified Opportunity';
	ApexPages.StandardController stdCont =new ApexPages.StandardController(opp);
	Example  exam=new Example (stdCont );
		exam.changeStageName();
		exam.dosave();
   }
}

Thanks 
Manoj
This was selected as the best answer
Che SFDCChe SFDC
You are a genius. I received 100% test coverage! Thank you so much Manoj !