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
Deja BondDeja Bond 

Hi, I am new to apex. I have this controller, can someone assist in writing a test class?

public class DuplicateLexController {
    
    public static Case cas {get;set;}
    public static String caseId{get;set;}
    public static String message {get;set;}
    
    public DuplicateLexController(ApexPages.StandardController s) {
        cas = (Case)s.getRecord();
        caseId = cas.id;
        System.debug('caseId'+caseId);
        
    }
    public PageReference updateCaseRecord(){
        List<Case> caseList = [SELECT RecordType.Name, Status, Tone__c, Category__c , SSR_Category__c , Reason
                               FROM Case 
                               WHERE Id = :caseId limit 1];
        if(caseList!=null && caseList.size()>0){
            if(caseList.get(0).RecordType.Name=='Support'){
                caseList.get(0).Status='Closed - Duplicate';
                caseList.get(0).Tone__c='Neutral';
                caseList.get(0).Category__c = 'Duplicate';   
            }else if(caseList.get(0).RecordType.Name=='SSR'){
                caseList.get(0).Status='Closed - Duplicate';
                caseList.get(0).Tone__c='Neutral';
                caseList.get(0).Category__c = 'Duplicate';
                caseList.get(0).SSR_Category__c = 'Duplicate';
                caseList.get(0).Reason='Duplicate';
            }
            update caseList.get(0);
        }
        Pagereference redirect =new Pagereference('/'+caseId);
        redirect.setRedirect(true);
        return redirect;
    }
}
Raj VakatiRaj Vakati
Use this code and change the VF page in this test class
 
@IsTest
public class DuplicateLexControllerTest {

    @isTest
    public static void dupTestCl(){
       //create account
    Account acc = new Account();
    //enter details  
    acc.Name = 'Test Account';
    insert acc;
	
	Id caseRecType = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Support').getRecordTypeId();

	
   //create case
    Case c = new Case();
    //enter details
    c.AccountId = acc.Id;
    c.Type = 'My Type';
    c.Origin = 'My Origin';
    c.Status = 'My Status';
	c.RecordTYpeId = caseRecType ;
     
    insert c;

	
	
	        
        ApexPages.StandardController sc = new ApexPages.StandardController(testAccountPlanInsert);
        DuplicateLexController testAccPlan = new DuplicateLexController(c);
        
        PageReference pageRef = Page.YOURPAGENAME;
        pageRef.getParameters().put('Id', c.Id);
        Test.setCurrentPage(pageRef);
    
	  testAccPlan.updateCaseRecord(); 
	
        
    }
	 @isTest
    public static void dupTestC3(){
       //create account
    Account acc = new Account();
    //enter details  
    acc.Name = 'Test Account';
    insert acc;
	
	Id caseRecType = Schema.SObjectType.Case.getRecordTypeInfosByName().get('SSR').getRecordTypeId();

	
   //create case
    Case c = new Case();
    //enter details
    c.AccountId = acc.Id;
    c.Type = 'My Type';
    c.Origin = 'My Origin';
    c.Status = 'My Status';
	c.RecordTYpeId = caseRecType ;
     
    insert c;

	
	
	        
        ApexPages.StandardController sc = new ApexPages.StandardController(testAccountPlanInsert);
        DuplicateLexController testAccPlan = new DuplicateLexController(c);
        
        PageReference pageRef = Page.YOURPAGENAME;
        pageRef.getParameters().put('Id', c.Id);
        Test.setCurrentPage(pageRef);
    
	  testAccPlan.updateCaseRecord(); 
	
        
    }
}

 
Raj VakatiRaj Vakati
Does its solved ?
Deja BondDeja Bond
Hi Raj, I will try this today, thank you.