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
Rals ChaddaRals Chadda 

Test coverage fr extension

extension class is giving only 33% code coverage .Any help how to can I cover these lines ??

the following two lines are not covered in test class
public List<Opportunity> getdemo() {
        return [Select Name , Account.name , StageName from Opportunity];

here is the test class which i m using
@isTest
Public class DemoTable2Test {
 static testmethod void testDemoTable2(){
 
  Opportunity oppy = new opportunity();
  oppy.name='ABC';
  oppy.StageName='abc';
  oppy.CloseDate=System.today();
  insert oppy;
  
  ApexPages.StandardController controller = new ApexPages.standardController(oppy);
  DemoTable2 e = new DemoTable2(controller );


  //Test converage for the myPage visualforce page
  PageReference pageref = Page.DemoTable2;
  Test.setCurrentPageReference(pageref);
  ApexPages.currentPage().getParameters().put('Id', oppy.Id);
  


 }
}

extension
 
public class DemoTable2 {
    //public  Opportunity oppty {get;set;}
    public DemoTable2(ApexPages.StandardController controller) {
    //this.oppty = (Opportunity)controller.getRecord();
    
    }
  
    public List<Opportunity> getdemo() {
        return [Select Name , Account.name , StageName from Opportunity];
    }

}

page
<apex:page standardController="opportunity" extensions="DemoTable2">
    <apex:pagemessages />
    <html lang="en">
        <head>
            <meta charset="utf-8"/>
            <meta name="viewport" content="width=device-width, initial-scale=1"/>
            <apex:stylesheet value="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"/>
            <!--<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">-->
            <apex:includeScript value="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"/>
            <!--- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>-->
            <apex:includeScript value="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"/>
            <!---<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>-->
        </head>
        <apex:form >
            <apex:pageBlock id="here">
                <body>
                    <div class="container">
                        <br></br>
                        <!-- Trigger the modal with a button -->
                        <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" style="margin-left: 45%;margin-right: 60%;">Create Opportunity</button>
                            <br></br><br></br>
   
                            <!-- <br></br>Modal -->
                            <div class="modal fade" id="myModal" role="dialog">
                                <div class="modal-dialog">
       
                                <!-- Modal content-->
                                    <div class="modal-content">
                                    <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                                            <h4 class="modal-title">Create New Opportunity</h4>
                                    </div>
                                    <div class="modal-body" >
                                        <table class="table">
                                            <tr>
                                                <td>Name</td> 
                                                <td><apex:inputField value="{! Opportunity.Name}" required="true"/></td>
                                            </tr>   
                                            <tr>
                                                <td>Account </td> 
                                                <td><apex:inputField value="{! Opportunity.Accountid}" required="true"/></td>
                                            </tr> 
                                            <tr>
                                                <td>Close Date</td> 
                                                <td><apex:inputField value="{! Opportunity.CloseDate}"/></td>
                                            </tr>  
                                            <tr>
                                                <td>Stage Name</td> 
                                                <td><apex:inputField value="{! Opportunity.StageName}"/></td>
                                            </tr>   
                                        </table>
                                    </div>
                                    <div class="modal-footer">
                                        <apex:commandButton value="Cancel"/>&nbsp;&nbsp;&nbsp;
                                        <apex:commandButton value="Save"  action="{!save}" rerender="here" />
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!---- Table--->
                    <div class="panel panel-default" style="margin-left: 20%;margin-right: 20%;">
                        <div class="panel-heading">
                            <table width="100%" cellpadding ="1" cellspacing = "1">
                                <tr>
                                    <th>#</th>
                                    <th>Opportunity Name</th>
                                    <th> Account Name </th>
                                    <th>Stage</th>
                                </tr>
                                <apex:variable var="i" value="{!1}"/>
                                <apex:repeat value="{!demo}" var="item">
                                    <tr>
                                        <td>{!i}<apex:variable var="i" value="{!i+1}"/></td>
                                        <td><apex:outputText value="{!item.Name}" /></td>
                                        <td><apex:outputText value="{!item.Account.name}"/></td>
                                        <td><apex:outputText value="{!item.StageName}"/></td>
                                    </tr>        
                                </apex:repeat>
                            </table>
                        </div >
                    </div>
                </body> 
            </apex:pageBlock>
        </apex:form>
    </html>
</apex:page>

 
Amit Chaudhary 8Amit Chaudhary 8
Please try below code:-
@isTest
Public class DemoTable2Test 
{
 static testmethod void testDemoTable2()
 {
 
  Opportunity oppy = new opportunity();
  oppy.name='ABC';
  oppy.StageName='abc';
  oppy.CloseDate=System.today();
  insert oppy;
  
  ApexPages.StandardController controller = new ApexPages.standardController(oppy);
  DemoTable2 e = new DemoTable2(controller );
  List<Opportunity> listOpp = e.getdemo();

/*	
  PageReference pageref = Page.DemoTable2;
  Test.setCurrentPageReference(pageref);
  ApexPages.currentPage().getParameters().put('Id', oppy.Id);
*/

 }
}


Please mark this as solution if this will help you.