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
Matthew RogersMatthew Rogers 

How would I write a test class for this very simple Custom controller?

public class OpportunityController {
    
    public List<Opportunity> opp {get;set;}

    public OpportunityController() {
        opp = [SELECT Id, Name, CreatedDate, StageName, Placement__c, LDN_Company__c, Description, Job_Title__c, CloseDate, 
				NextStep, Salary__c, Future_Prospects__c, Duration__c, Training_Delivery_Site__c, Qualification_taken__c, Entry_Requirements__c, Key_Responsibilities__c, 
				Skills_Required__c, Account.Name, Account.BillingPostalCode, Account.BillingStreet, Account.BillingCity FROM Opportunity 
				WHERE (StageName = '1 - Qualifying' or StageName = '2 - Booked for CK' or StageName = '3 - Live'
               or StageName = '4 - Work Trial') AND (Placement__c = 'New Apprentice')];
        	}
}

I can't figure out how to approach it for a custom and not standard controller

Thank you

Khan AnasKhan Anas (Salesforce Developers) 
Hi Matthew,

Greetings to you!

Please try the below code:
@isTest
private class Test_OpportunityController {	
    @isTest
    static testMethod void oppList(){
        
        Opportunity o = new Opportunity();
        o.Name='Test Name';
        o.StageName='1 - Qualifying';
        o.CloseDate = System.today();
        o.Placement__c='New Apprentice';
        // Add other required fields
        INSERT o;
        
        Test.startTest();

        OpportunityController oc = new OpportunityController();
        Opportunity o2 = [SELECT Id, Name, CreatedDate, StageName, Placement__c, LDN_Company__c, Description, Job_Title__c, CloseDate, 
				NextStep, Salary__c, Future_Prospects__c, Duration__c, Training_Delivery_Site__c, Qualification_taken__c, Entry_Requirements__c, Key_Responsibilities__c, 
				Skills_Required__c, Account.Name, Account.BillingPostalCode, Account.BillingStreet, Account.BillingCity FROM Opportunity 
				WHERE (StageName = '1 - Qualifying' or StageName = '2 - Booked for CK' or StageName = '3 - Live'
               or StageName = '4 - Work Trial') AND (Placement__c = 'New Apprentice')];

        Test.stopTest();

        System.assert(o2!=null);        
    }    
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Deepali KulshresthaDeepali Kulshrestha
Hi Matthew,

Here is the dummy data which I am providing in this test class, please fill the opportunity custom fields values as per your requirement. Kindly share with me whatever error and exception you are getting:
@IsTest
public class OpportunityController_Test {
    @IsTest
    public static void method1(){
        
        Account acct = new Account ();
        acct.Name='test';
        acct.BillingPostalCode='12345';
        acct.BillingStreet='12';
        acct.BillingCity='Noida';
        insert acct;
        
                
        Opportunity opp=new Opportunity();
        opp.Name='TestOpp';
        opp.StageName='1 - Qualifying';
        opp.Description='Opp_Description';
        opp.CloseDate=system.today();
        opp.NextStep='Updation';
        opp.Placement__c='New Apprentice';
        opp.LDN_Company__c='Salesforce';    
        opp.Job_Title__c='Develeoper'; 
        opp.Salary__c=100;
        opp.Future_Prospects__c='New Apprentice';
        opp.Duration__c='30-min';
        opp.Training_Delivery_Site__c='www.training.com';
        opp.Qualification_taken__c='Masters';
        opp.Entry_Requirements__c='';
        opp.Key_Responsibilities__c= '';
        opp.Skills_Required__c='Development';
            
        Insert opp;
         OpportunityController oppCont=new OpportunityController();
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Ajay K DubediAjay K Dubedi
Hi Matthew,
Try the below code it will help you and change the custom fields values.
 
@isTest
public class OpportunityControllerTest{
    @isTest
    static void OpportunityController_TestMethod(){
        Account accObj = new Account();
        accObj.Name= 'Test Account';
        accObj.BillingPostalCode = '85556';
        accObj.BillingStreet = 'Xyz';
        accObj.BillingCity = 'Test City';
        insert accObj;
        Opportunity OppObj = new Opportunity();
        OppObj.Name = 'Test Opportunity';
        OppObj.AccountId = accObj.Id;
        OppObj.StageName = '1 - Qualifying';
        OppObj.Placement__c = 'New Apprentice';
        OppObj.LDN_Company__c = 'ABC';
        OppObj.Description = 'Opportunity for test';
        OppObj.CloseDate = System.today().addMonths(2);
        OppObj.Job_Title__c = 'Xyz';
        OppObj.NextStep = 
        OppObj.Salary__c = 50000;
        OppObj.Future_Prospects__c= 'propectes';
        OppObj.Duration__c = 'Durations';
        OppObj.Training_Delivery_Site__c = 'abc.com';
        OppObj.Qualification_taken__c = 'Qualification';
        OppObj.Entry_Requirements__c = 'Entry_Requirements';
        OppObj.Key_Responsibilities__c = 'Key_Responsibilities';
        OppObj.Skills_Required__c = 'Skills';
        insert OppObj;
        Test.startTest();
        OpportunityController opp = new OpportunityController();
        Test.stopTest();
    }
}




I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Tad Aalgaard 3Tad Aalgaard 3
You are asking a question about the exact same test class in two different posts.  Please limit your question to one post.

Duplicate post.

https://developer.salesforce.com/forums/ForumsMain?id=9062I000000IKo5QAG
Matthew RogersMatthew Rogers
Thank you all for your suggestions, I will try these!