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
Lindsey Colvin2Lindsey Colvin2 

Test Class Help for Opportunity Controller Extension

Hi,

I am fairly new to developing classes and pages, and need assistance in getting a test class to work for this controller extension.  Your help is appreciated!

Controller:

public with sharing class OpportunityControllerExtension {

    private final Opportunity opp;
    
        public OpportunityControllerExtension(ApexPages.StandardController stdController)  {
            this.opp = (Opportunity) stdController.getRecord();
            
       }
       
    public PageReference Dismiss()  {
    
        opp.Display_Alert__c = false;
        update opp;
        
            PageReference pageRef = ApexPages.currentPage();
            pageRef.setRedirect(true);
            return pageRef;
            
            
       }
   }

I've tried a simple insert new opportunity test class, but I keep getting the error "Error: Compile Error: Constructor not defined".

Thank you!
Best Answer chosen by Lindsey Colvin2
Ankit SehgalAnkit Sehgal
@isTest
public class testOpportunityControllerExtension
{
   static testMethod void test()
   {
        PageReference pageRef = Page.<your visualforce page name>;
        Test.setCurrentPage(pageRef);
        Account testAcct = new Account (Name = 'My Test Account');
         insert testAcct;
        // Creates first opportunity
        Opportunity oppt = new Opportunity(Name ='New mAWS Deal',
                            AccountID = testAcct.ID,
                            StageName = 'Customer Won',
                            Amount = 3000,
                            CloseDate = System.today(),
                            Display_Alert__c=false);

          insert oppt;
         ApexPages.StandardController sc = new      ApexPages.StandardController(Oppt);
         OpportunityControllerExtension testObj=new OpportunityControllerExtension(sc);

         Test.startTest();
           testObj.Dismiss();
         Test.stopTest();
   }
}

try this one.

All Answers

Ankit SehgalAnkit Sehgal
@isTest
public class testOpportunityControllerExtension
{
   static testMethod void test()
   {
        PageReference pageRef = Page.<your visualforce page name>;
        Test.setCurrentPage(pageRef);
        Account testAcct = new Account (Name = 'My Test Account');
         insert testAcct;
        // Creates first opportunity
        Opportunity oppt = new Opportunity(Name ='New mAWS Deal',
                            AccountID = testAcct.ID,
                            StageName = 'Customer Won',
                            Amount = 3000,
                            CloseDate = System.today(),
                            Display_Alert__c=false);

          insert oppt;
         ApexPages.StandardController sc = new      ApexPages.StandardController(Oppt);
         OpportunityControllerExtension testObj=new OpportunityControllerExtension(sc);

         Test.startTest();
           testObj.Dismiss();
         Test.stopTest();
   }
}

try this one.
This was selected as the best answer
Lindsey Colvin2Lindsey Colvin2
Perfect coverage!  Thank you!