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
Ankita LAnkita L 

Hello everyone, need to write test class. please help me

User-added image
Best Answer chosen by Ankita L
ApuroopApuroop
Hey Ankita,

First thing's first, please post the code next time rather than an image. :)

Try the below code, adjust all the queries before testing. I didn't create any fields in mine, I just tested with the standard fields and it has 100% coverage. Also, add as many assert statements as you can.

Class:
public class UpdateOnlineSale {
    private final Account a;
    public Opportunity opp {get; set;}
    ApexPages.StandardController controller;
    
    public UpdateOnlineSale(ApexPages.StandardController controller){
        Account act = (Account)controller.getRecord();
        Account queriedAcc = [SELECT Id, CreatedDate FROM Account WHERE Id =:act.id];
        System.debug('Account - line 8 ----> '+queriedAcc);
        this.controller = controller;
        opp = [SELECT Id, StageName, CloseDate FROM Opportunity WHERE AccountID = :queriedAcc.id]; //Change this to your query!!!
        System.debug('Opportunity - line 11 ----> '+opp);
        opp.StageName ='Closed Won';
        DateTime dt = queriedAcc.CreatedDate;
        Date actCreateDate = Date.newInstance(dt.year(), dt.month(), dt.day());
    }
    
    public PageReference save(){
        if(opp.Id != null && ApexPages.hasMessages() == false){
            update opp;
            controller.save();
            PageReference save = new PageReference('/' + ApexPages.currentPage().getParameters().get('id'));
            save.setRedirect(true);
            return save;
        } else{
            return null;
        }
    }
}

Test Class:
@isTest
public class UpdateOnlineSale_Test {
    //Positive
    @isTest
    public static void myTestMethod1(){
        //Create according to your org by populating all the required fields
        Account acc = new Account(Name='TestAccount');
        insert acc;
        Opportunity opp = new Opportunity(RecordTypeId='012f20000006hY5', Name='TestOpp1', CloseDate=System.today()+7, StageName='Qualification', AccountId = acc.Id);
        insert opp;
        
        PageReference myPage = Page.AccountVFPage; //Change it to your Visualforce page..
        myPage.getParameters().put('Id', acc.Id);
        Test.setCurrentPage(myPage);
        
        UpdateOnlineSale updateOnline = new UpdateOnlineSale(new ApexPages.StandardController(acc));
        updateOnline.save();
    }
    //Negative
    @isTest
    public static void myTestMethod2(){
        //Create according to your org by populating all the required fields
        Account acc2 = new Account(Name='TestAccount2');
        insert acc2;
        Opportunity opp2 = new Opportunity(RecordTypeId='012f20000006hY5', Name='TestOpp1', CloseDate=System.today()+7, StageName='Qualification', AccountId = acc2.Id);
        insert opp2;
        
        PageReference myPage2 = Page.AccountVFPage; //Change it to your Visualforce page..
        myPage2.getParameters().put('Id', acc2.Id);
        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Testing Error');
        Test.setCurrentPage(myPage2);
        ApexPages.addMessage(msg);
        
        UpdateOnlineSale updateOnline2 = new UpdateOnlineSale(new ApexPages.StandardController(acc2));
        updateOnline2.save();
    }
}

All Answers

ApuroopApuroop
Hey Ankita,

First thing's first, please post the code next time rather than an image. :)

Try the below code, adjust all the queries before testing. I didn't create any fields in mine, I just tested with the standard fields and it has 100% coverage. Also, add as many assert statements as you can.

Class:
public class UpdateOnlineSale {
    private final Account a;
    public Opportunity opp {get; set;}
    ApexPages.StandardController controller;
    
    public UpdateOnlineSale(ApexPages.StandardController controller){
        Account act = (Account)controller.getRecord();
        Account queriedAcc = [SELECT Id, CreatedDate FROM Account WHERE Id =:act.id];
        System.debug('Account - line 8 ----> '+queriedAcc);
        this.controller = controller;
        opp = [SELECT Id, StageName, CloseDate FROM Opportunity WHERE AccountID = :queriedAcc.id]; //Change this to your query!!!
        System.debug('Opportunity - line 11 ----> '+opp);
        opp.StageName ='Closed Won';
        DateTime dt = queriedAcc.CreatedDate;
        Date actCreateDate = Date.newInstance(dt.year(), dt.month(), dt.day());
    }
    
    public PageReference save(){
        if(opp.Id != null && ApexPages.hasMessages() == false){
            update opp;
            controller.save();
            PageReference save = new PageReference('/' + ApexPages.currentPage().getParameters().get('id'));
            save.setRedirect(true);
            return save;
        } else{
            return null;
        }
    }
}

Test Class:
@isTest
public class UpdateOnlineSale_Test {
    //Positive
    @isTest
    public static void myTestMethod1(){
        //Create according to your org by populating all the required fields
        Account acc = new Account(Name='TestAccount');
        insert acc;
        Opportunity opp = new Opportunity(RecordTypeId='012f20000006hY5', Name='TestOpp1', CloseDate=System.today()+7, StageName='Qualification', AccountId = acc.Id);
        insert opp;
        
        PageReference myPage = Page.AccountVFPage; //Change it to your Visualforce page..
        myPage.getParameters().put('Id', acc.Id);
        Test.setCurrentPage(myPage);
        
        UpdateOnlineSale updateOnline = new UpdateOnlineSale(new ApexPages.StandardController(acc));
        updateOnline.save();
    }
    //Negative
    @isTest
    public static void myTestMethod2(){
        //Create according to your org by populating all the required fields
        Account acc2 = new Account(Name='TestAccount2');
        insert acc2;
        Opportunity opp2 = new Opportunity(RecordTypeId='012f20000006hY5', Name='TestOpp1', CloseDate=System.today()+7, StageName='Qualification', AccountId = acc2.Id);
        insert opp2;
        
        PageReference myPage2 = Page.AccountVFPage; //Change it to your Visualforce page..
        myPage2.getParameters().put('Id', acc2.Id);
        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Testing Error');
        Test.setCurrentPage(myPage2);
        ApexPages.addMessage(msg);
        
        UpdateOnlineSale updateOnline2 = new UpdateOnlineSale(new ApexPages.StandardController(acc2));
        updateOnline2.save();
    }
}
This was selected as the best answer
Deepali KulshresthaDeepali Kulshrestha
Hi Ankita,

Try the following code it may be helpful for you:
 
@Istest()
public class UpdateOnlineSale_Test{
      @Istest()
      public static void UpdateOnlineSaleMethod(){

        Account acct = new Account ();
        acct.Name='test';
        insert acct;
        
        Opportunity opp=new Opportunity();
        opp.AccountId=acct.Id
        opp.Name='TestOpp';
        opp.RecordTypeId='01260000000UItt';
        opp.StageName='Qualifi';
        opp.Description='Opp_Description';
        opp.CloseDate=system.today();
        opp.NextStep='Updation';
        Insert opp;
        
        PageReference pageRef = Page.ApexPageName;
        Test.setCurrentPage(pageRef);

        pageRef.getParameters().put('Id', String.valueOf(acct.Id));
        ApexPages.StandardController sc = new ApexPages.StandardController(acct);
        UpdateOnlineSale updateOnline = new UpdateOnlineSale(sc);

         }
 }

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