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 class for extension 50% coverage

Hi my extension class is giving only 50% code coverage .Any help how to cover this

the following two lines are not covered in test class
 

public List<Opportunity> getdemo() {
        return [Select Name , Account.name , StageName from Opportunity];

Test class
@isTest
Public class DemoTable2Test {
 static testmethod void testDemoTable2(){
 
  Opportunity oppy = new opportunity();
  oppy.name='ABC';
  oppy.StageName='abc';
  oppy.CloseDate=System.today();
  insert oppy;
  
  DemoTable2 controller = new DemoTable2(new ApexPages.StandardController(oppy));
  //Test converage for the myPage visualforce page
  PageReference pageref = Page.DemoTable2;
  Test.setCurrentPageReference(pageref);
  ApexPages.currentPage().getParameters().put('Id', oppy.Id);
  
  // create an instance of the controller
 DemoTable2 dm = new DemoTable2();

 }
}

extension
public class DemoTable2 {
 
    public DemoTable2(ApexPages.StandardController controller) {

    
    }
   public DemoTable2()
   {}
    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>

 
Prabhat Kumar12Prabhat Kumar12
Please use following update test class.
@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);
  


 }
}

 
Rals ChaddaRals Chadda

Prabhat ,No coverage still .following lines not covered

public List<Opportunity> getdemo() {
        return [Select Name , Account.name , StageName from Opportunity];
 

public DemoTable2()

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;

  PageReference pageref = Page.DemoTable2;
  Test.setCurrentPageReference(pageref);
  ApexPages.currentPage().getParameters().put('Id', oppy.Id);
  ApexPages.StandardController controller = new ApexPages.standardController(oppy);
  DemoTable2 e = new DemoTable2(controller );
  List<Opportunity> listOpp = e.getdemo();


 }
}

NOTE: This code has not been tested and may contain typographical or logical errors

NOTE:- Always follow below best pratice for test classes.
1) Make calls to methods using both valid and invalid inputs.
2) Complete successfully without throwing any exceptions, unless those errors are expected and caught in a try…catch block.
3) Always handle all exceptions that are caught, instead of merely catching the exceptions.
4) Use System.assert methods to prove that code behaves properly.
5) Use the runAs method to test your application in different user contexts.
6) Exercise bulk trigger functionality—use at least 20 records in your tests.


Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help

Thanks
Amit Chaudhary