通过以下类插入联系人的测试类 [英] test class for inserting contacts through the following class

查看:21
本文介绍了通过以下类插入联系人的测试类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个插入联系人的顶点类.我为它正在通过的地方编写了一个测试类,但代码覆盖率为零.有人可以建议我错过了什么吗?测试类:@isTest公共类 TestReferalAccessclass{static testMethod void ReferalAccessclassMethod(){Test.StartTest();Contact c=new Contact(FirstName='fname',LastName = 'lname',Email = 'email@gmail.com',Phone = '9743800309');插入 c;System.AssertNotEquals(Null, c.Id);Test.StopTest();

I have one apex class which inserts the contacts.i wrote one test class for that where its is passing but the code coverage is zero.can somebody suggest what i missed? test class: @isTest public class TestReferalAccessclass { static testMethod void ReferalAccessclassMethod() { Test.StartTest(); Contact c=new Contact(FirstName='fname',LastName = 'lname',Email = 'email@gmail.com',Phone = '9743800309'); insert c; System.AssertNotEquals(Null, c.Id); Test.StopTest();

 }    

}
apex class:
    public without sharing class ReferalAccessclass {
    public String inputID{get; set;}
    public String firstName{get; set;}
    public String lastName{get; set;}
    public String email{get; set;}
    public String phone{get; set;}
    public Decimal exp{get; set;}
    public String location{get; set;}
    public contact con{get;set;}

    Public attachment objAttachment{get; set;}

    public ReferalAccessclass(ApexPages.StandardController controller) 
      { 

    objAttachment = new Attachment();

    }

  public void saveInformation()
{
try{
    IF(inputID != 'NULL'){
    con = [SELECT ID,Name,FirstName,LastName,Email,Phone,Years_of_Experience__c,Location__c FROM Contact where ID =: inputID ];

    con.FirstName = firstName;
    con.LastName = lastName;
    con.Email = email;
    con.Phone = phone; 
    }
    update con;
    objAttachment.ParentId = con.id;
    Insert objAttachment;

   }
  catch(exception e){} 
  ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Thank you for your valuable response');

//返回空值;

}

}

推荐答案

您没有从测试类中调用您的实际类.这就是为什么它没有提供代码覆盖率.试试这个测试类.

You didn't invoke your actual class from test class. That's why its not giving code coverage. Try this test class.

@isTest public class TestReferalAccessclass { 
    static testMethod void ReferalAccessclassMethod() { 

        Contact c=new Contact(
            FirstName='fname',
            LastName = 'lname',
            Email = 'email@gmail.com',
            Phone = '9743800309'); 
        insert c; 
        Test.StartTest(); 
        System.AssertNotEquals(Null, c.Id); 

        ApexPages.StandardController sc = new ApexPages.StandardController(c);
        ReferalAccessclass refClass = new ReferalAccessclass(sc);
        refClass.inputID = c.id;
        refClass.firstName = c.id;
        refClass.lastName = c.id;
        refClass.email = c.id;
        refClass.phone = c.id;
        refClass.con = c;
        refClass.saveInformation();

        Test.StopTest();
    }    
}

这篇关于通过以下类插入联系人的测试类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆