如何在测试类中实现100%需要帮助

时间:2015-09-07 11:09:28

标签: salesforce visualforce apex test-class

我一直在努力为100%覆盖率编写测试类。我很难理解如何调用标准控制器并调用参数化构造函数也将页面设置为当前页面作为当前页面。 在帐户视图页面上,我已覆盖自定义对象的相关列表上的新按钮。我在编辑记录时可以使用相同的类。在编辑记录视图页面和新记录视图页面上,我都能够检索帐户ID。目前我能够达到65%的覆盖率。我不确定我做错了什么。请尽可能帮助。我附上了以下代码。

由于 加亚特里讷

//my Class

public class NewTaxExempt{

    public TaxExempt__c obj {get; set;}   
    public String selectedIso {get;set;}

    public List<selectOption> isoCodes {

        get {
            List<selectOption> options = new List<selectOption>();

            for (State__c iso : State__c.getAll().values())
            options.add(new SelectOption(iso.State_ISO__c,iso.State_Name__c+' - '+iso.State_ISO__c));

            return options;

        }

        set;
    }

    // onload standard controller will be called.
    public NewTaxExempt(ApexPages.StandardController controller) {

        ID idte=controller.getRecord().id;
        if(idte==null){
            obj=(TaxExempt__c)controller.getRecord();
        }
        else{
            obj= [Select Account__c, Certificate_ID__c, Certificate_Type__c,Description__c, Issuing_Jurisdiction__c,Status__c,Tax_Exempt_Effective_Date__c,Tax_Exempt_Expiration_Date__c from TaxExempt__c where id=:idte];         
        }


    }

    //on click on cancel button
    public PageReference cancel() {
        return new PageReference('/'+obj.account__c);
    } 

    //on click on Save button
    public PageReference save() {

        obj.Issuing_Jurisdiction__c=selectedIso;
        if(obj.id==null){        
            insert obj;
        }
        else{
            update obj;
        }
        return new PageReference('/'+obj.account__c);
    }
}
//my test class

@isTest
public class NewTaxExemptTest{

    public static testMethod void whenIssueJurisdictionIsBlankDefaultValueIsSet(){

        try{    

            BET_ObjectFactory.generateTaxExemptRequirements();
            Account aobj = ( Account)TestFactory.createSObject(new Account(Name='test acc gs250144'),true);
            PageReference ref = Page.NewTaxExempt;
            Test.setCurrentPage(ref);
            TaxExempt__c obj=new TaxExempt__c();
            obj.account__c=aobj.Id;
            obj.Issuing_Jurisdiction__c='RI';
            ApexPages.StandardController sc = new ApexPages.StandardController(obj);
            NewTaxExempt ob=new NewTaxExempt(sc);
            ob.selectedIso='RI'; 
            ob.obj=obj; 

            ob.save(); 

            System.assertEquals(obj.Issuing_Jurisdiction__c,null);


        List<State__c> stList=new List<State__c>();

        State__c st=new State__c();
        st.Name='ST_11009';        
        st.State_Name__c='Alabama';
        st.State_ISO__c='AL';
        st.State_Country_Name__c='United States';
        st.State_Country_ISO__c='US';
        stList.add(st);

        State__c st1=new State__c();
        st1.Name='ST_11008';
        st1.State_Name__c='Alabama';
        st1.State_ISO__c='AL';
        st1.State_Country_Name__c='United States';
        st1.State_Country_ISO__c='US';
        stList.add(st1);

        State__c st2=new State__c();
        st2.Name='ST_11019';        
        st2.State_Name__c='Alaska';
        st2.State_ISO__c='AK';
        st2.State_Country_Name__c='United States';
        st2.State_Country_ISO__c='US';
        stList.add(st2);

        State__c st3=new State__c();
        st3.Name='ST_12009';        
        st3.State_Name__c='Arizona';
        st3.State_ISO__c='AZ';
        st3.State_Country_Name__c='United States';
        st3.State_Country_ISO__c='US';
        stList.add(st3);

        State__c st4=new State__c();
        st4.Name='ST_11309';
        st4.State_Name__c='Arkansas';
        st4.State_ISO__c='AR';
        st4.State_Country_Name__c='United States';
        st4.State_Country_ISO__c='US';
        stList.add(st4);

        State__c st5=new State__c();
        st5.Name='ST_21009';        
        st5.State_Name__c='California';
        st5.State_ISO__c='CA';
        st5.State_Country_Name__c='United States';
        st5.State_Country_ISO__c='US';
        stList.add(st5);

        State__c st6=new State__c();
        st6.Name='ST_23';        
        st6.State_Name__c='California';
        st6.State_ISO__c='';
        st6.State_Country_Name__c='';
        st6.State_Country_ISO__c='';
        stList.add(st6);

        insert stList;

        List<State__c> allStates = [select State_Name__c, State_ISO__c, State_Country_Name__c, State_Country_ISO__c 
                                    from State__c
                                    order by State_Name__c];

        for(State__c stt : allStates){
            if(stt.State_ISO__c == '' || stt.State_ISO__c == null){
                ob.isoCodes.add(new SelectOption(stt.State_ISO__c, stt.State_Name__c));
            }
            else{
                ob.isoCodes.add(new SelectOption(stt.State_ISO__c, stt.State_Name__c));                
            }

        }        
        System.assertNotEquals(ob.isoCodes.size(),0);


        }catch(Exception e){

            System.debug('Gayatri exception :'+e);
        }   
    }

    public static testMethod void whenUserClicksCancel(){

        BET_ObjectFactory.generateTaxExemptRequirements();
        Account aobj = ( Account)TestFactory.createSObject(new Account(Name='test acc gs250144'),true);

        // PageReference ref = new PageReference(pageName)
        // ref.getParameters().put('TaxExempt__c',obj.Id);
        // Test.setCurrentPage(ref);

        PageReference ref = Page.NewTaxExempt;
        Test.setCurrentPage(ref);
        TaxExempt__c obj=new TaxExempt__c();
        ApexPages.StandardController sc = new ApexPages.StandardController(obj);
        NewTaxExempt ob=new NewTaxExempt(sc);
        ob.cancel();  

    }    
}

1 个答案:

答案 0 :(得分:0)

如果您知道测试覆盖中缺少代码的哪一部分,那么它会更容易。

  1. 转到开发者控制台并运行测试
  2. 页面底部的
  3. 选择测试标签
  4. 从右侧找到您的测试(标题:整体代码覆盖率)
  5. 双击您的测试
  6. 未覆盖的部分将为红色。现在尝试为它们编写测试。

    如果您想要覆盖您的课程属性,您只需要这样做:

    @isTest static void myPropertiesTest(){
    
        MyClass obj = new MyClass();
        obj.myProperty = SomeValue;
        // now assert a function in you class that uses this property. 
    
        // an easy and dumb way is to just assert the property. 
        System.assertEquals(SomeValue, obj.myProperty);
    }