My test class runs with no error, but unfortunately, not all methods are covered. Only the addNewRow() method is covered.


public with sharing class MyController {

    private ApexPages.StandardController parentController;
    public Parent__c parent {get;set;}
    public List<MyWrapper> wraplist {get;set;} 
    public Integer rowToRemove {get;set;}
    public String selected {get; set;}
    public Boolean form1 {get; set;}
    public Boolean form2 {get; set;} 

    public MyController(ApexPages.StandardController controller) {
        parentController = controller;
        this.parent = (Parent__c)controller.getRecord();

        form1 = form2 = false;
        selected = '';

        wraplist = new List<MyWrapper>();


    public void addNewRow() {
        List<MyWrapper> temp_list = new List<MyWrapper>();
        MyWrapper newRecord = new MyWrapper();
        Child__c newChildRecord = new Child__c();
        newRecord.ch = newChildRecord;
        newRecord.index = temp_list.size();
        for(MyWrapper wloop : temp_list) {

    public void removeRow() {
        List<MyWrapper> rem_list = new List<MyWrapper>();
        for(MyWrapper wloop : wraplist) {
        for(MyWrapper rloop : rem_list) {

    Public PageReference cancel() {

        PageReference pageref = new PageReference('/home/home.jsp');
        return pageref;

    public PageReference save() {

        List<MyWrapper> childlist = new List<MyWrapper>();
        List<Child__c > newChildList = new List<Child__c >();
        Decimal total = 0;
        for(MyWrapper wloop : wraplist) {

        for(MyWrapper tloop : childlist) {
            total += tloop.ch.Amount__c;


        Parent__c p1 = new Parent__c();
        p1.Account__c = parent.Account__c;

        p1.Total__c = total;
        insert p1;

        if(childlist !=null && !childlist.isEmpty()) {

            for(MyWrapper rec : childlist) {
                Child__c chTemp = new Child__c ();
                chTemp.Amount__c = rec.ch.Amount__c;
                chTemp.Date__c = rec.ch.Date__c;
        chTemp.Child_Name_Type__c = rec.ch.Child_Name_Type__c;

                chTemp.Parent__c = p1.id;


        if(newChildList.size() > 0) {
            insert newChildList;

        PageReference pageref = new PageReference('/' + p1  .Id);
        return pageref;

    public void onChangeValue(){

        if(selected == 'A'){
            form1 = true;
            form2 = false;

        else if(selected == 'B'){
            form1 = false;
            form2 = true;

            form1 = false;
            form2 = false;

   public class MyWrapper {

        public Integer index {get;set;}
        public Child__c ch {get;set;}



Test Class:

public class MyTestClass {
    private static testMethod void insertRecord() {
        Schema.DescribeSObjectResult sch = Schema.SObjectType.Parent__c;
        Map<String,Schema.RecordTypeInfo> ParentRecordTypeInfo = sch.getRecordTypeInfosByName();

        Id rtId = ParentRecordTypeInfo.get('B').getRecordTypeId();

        Parent__c p = new Parent__c();
        p.RecordTypeId = ParentRecordTypeInfo.get('B').getRecordTypeId();
        insert p;

        List<Child__c> childList = new List<Child__c>();
        for(Integer i=0; i<200; i++) {
            Child__c c = new Child__c();
            c.Child_Name_Type__c = 'B1';
            c.Date__c = Date.parse('1/1/2016');
            c.Amount__c = 1000.00;
        insert childList;


        ApexPages.StandardController sc = new ApexPages.StandardController(p);
        MyController mc = new MyController(sc);
        PageReference pg = Page.MyPage;


3 Answers 3


In your test class, only the method named addNewRow() is invoked. In order to cover other methods, you have to invoke them. The methods need to be invoked are

removeRow() , cancel() , save() and onChangeValue().


Junaid is correct that you must invoke all the methods you have written, and some of them multiple times in order to hit all the execution paths. But more importantly, you should focus on adding assertions. Right now, you are merely smoke testing, which is not nearly as helpful to future developers. Remember, that may include yourself.

Take a look at An Introduction to Apex Code Test Methods:

  • Use System.assert methods to prove that code behaves properly.

This is key to ensure that the Apex code executes to the expected value or behavior. This will also facilitate regression testing as the Apex code changes over time.

And also check out How to Write Good Unit Tests (emphasis mine):

Verify the results are correct

Verifying that your code works as you expect it to work is the most important part of unit testing. It’s also one of the things that Force.com developers commonly neglect. Unit tests that do not verify the results of the code aren’t true unit tests. They are commonly referred to as smoke tests, which aren’t nearly as effective or informative as true unit tests.

A good way to tell if unit tests are properly verifying results is to look for liberal use of the System.assert() methods. If there aren’t any System.assert() method calls, then the tests aren’t verifying results properly. And, no, System.assert(true); doesn’t count.

Sample verification code for this trigger might look like this:

a = [SELECT Name, Most_Recently_Created_Opportunity_Name__c
     FROM Account
     WHERE Id = :a.Id];

System.assertEquals('My Opportunity', a.Most_Recently_Created_Opportunity_Name__c);

Notice that this unit testing code explicitly verifies that the trigger performed the action that we expected.


For more test coverage, you have to call every method by making an instance of MyController.

MyController myController = new MyController();

Example :


Also, u have to initialise the public variable that is used in the method of the controller.

Example :

myController.rowToRemove = 5;
myController.form1 = true ;
myController.form2 = true ;
  • How can I do this inside my testMethods? Forgive me for asking but I'm really not sure if I'm doing it right on the code I've posted.
    – curdual
    Commented Aug 17, 2016 at 8:13
  • @curdual yes, u r doing correct. Commented Aug 17, 2016 at 8:34
  • Inside ur test method , just add the following code . MyController myController = new MyController(); myController.removeRow(); myController.save(); myController.cancel();myController.onChangeValue Commented Aug 17, 2016 at 8:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .