+ Start a Discussion
DhuravDhurav 

Test class.....

How i can create the test class of this code................

 

  public List<String> availableFields {
        get {
        controller.addFields(new List<String>(bookFields));
        return new List<String>(bookFields);
        }
    }
Best Answer chosen by Admin (Salesforce Developers) 
mariagusmariagus

Hi,

 

I tested your code and the bookExtension is covered with your test but I got an error. You can not call to addFields in the availbleFields method.

If you comment controller.addFields(new List<String>(bookFields)); line, the test is executed without error.

 

Actually I would not modify data in the getter or setter ... you can get errors like this one. if you need to modify something, create a method for this and call it before the get. And this method should just return the bookFields values.

Hope this helps

 

Cheers

All Answers

mariagusmariagus

Hi,

 

I would just create a method that calls to yours. For instance, if you have this:

 

public with sharing class myController
{
    private List<String> m_bookFields;
    public static List<String> availableFields()
    {
        get {
        controller.addFields(new List<String>(m_bookFields));
        return new List<String>(m_bookFields)
    }

    static testMethod myTest()
    {
        m_bookFields = new List<String>();
        //set some values to m_bookFields
        available();        
    }     
}

Hope this helps.

 

Regards

DhuravDhurav

public with sharing class bookExtension {
    private ApexPages.StandardController controller;
    
    private Set<String> bookFields = new Set<String>();
    
    public bookExtension (ApexPages.StandardController controller) {
        this.controller = controller;   
        Map<String, Schema.SobjectField> fields =Schema.SobjectType.Book__c.fields.getMap();
        system.debug(fields +'*************OOOOOOOOO******************');
        system.debug(fields.keySet()+'@@@@@@@@@@@@Keys@@@@@@@@@@');
        for (String s : fields.keySet()) {
        // Only include accessible fields
        system.debug(fields.get(s).getDescribe().isCustom()+'#@@@@@@@@@@@@Keys@@@@@@@@@@');
            if (fields.get(s).getDescribe().isAccessible() && fields.get(s).getDescribe().isCustom()) {
            bookFields.add(s);
            system.debug(bookFields+'*******************************');
            }
        }
    }
    public List<String> availableFields {
        get {
        controller.addFields(new List<String>(bookFields));
        return new List<String>(bookFields);
        }
    }
}

 

 

 

@isTest
private class bookExtensionTest {
    static testMethod void testBookExtension() {
        Test.startTest();
        Book__c b=new Book__c(Author__c='A',ISBN__c='B' ,Price__c=10.50,Publisher__c='C',    Title__c='D');
        bookExtension extension=new bookExtension(new Apexpages.Standardcontroller(b));    
        System.assert(extension.availableFields!=null);
        
        List<String> bookList=new List<String>();
        
                
        Test.stopTest();        
    }
}

 

My actual code as above And i am trying for test method for the field in public List<String> availableFields {
        get {
        controller.addFields(new List<String>(bookFields));
        return new List<String>(bookFields);
        }
    }

mariagusmariagus

Hi,

 

I tested your code and the bookExtension is covered with your test but I got an error. You can not call to addFields in the availbleFields method.

If you comment controller.addFields(new List<String>(bookFields)); line, the test is executed without error.

 

Actually I would not modify data in the getter or setter ... you can get errors like this one. if you need to modify something, create a method for this and call it before the get. And this method should just return the bookFields values.

Hope this helps

 

Cheers

This was selected as the best answer