You need to sign in to do that
Don't have an account?

Code coverage for the test class.
Hi,
I have a test class written for my apex class. But i am not sure if correct. In the sandbox if RUN TEST for the class itself it says the following
Apex Test Result
Test Class | HFSendSurveyTestClass |
Tests Run | 1 |
Test Failures | 1 |
Code Coverage Total % | 100 |
Total Time (ms) | 269.0 |
Test Failures |
Method Name Message Stack Trace
HFSendSurveyTestClass.SendSurvey | 269.0 | System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name] | Class.HFSendSurveyTestClass.SendSurvey: line 19, column 10 External entry point |
Does this mean my class succesfully covered the code coverage.
Here is my test clss.
public class HFSendSurveyTestClass {
static testMethod void SendSurvey()
{
// Get the most recently created config object.
Config_Object__c config = new Config_Object__c();
config.Name = 'HostFamily_Survey';
config.HFSurveyCode__c = 'HF1MoArv';
config.Survey_Date__c = Date.today();
config.SOQL_query__c = string.ValueOf('Select o.Id, o.Account.Id, o.AccountId, o.Account.Survey_Opp_Id__c, o.Account.Survey_Dt__c, o.Account.Survey_Code__c, o.Account.Name From Opportunity o where o.StageName= &#Active&# AND o.Placmnt_Start_Date_Arrive__c < Last_N_days :30 AND o.Placmnt_Start_Date_Arrive__c = Last_N_days :60');
insert config;
Account acc = new Account();
//acc.Name = 'TestSurvey';
acc.Survey_Code__c = 'HF1MoArv';
acc.Survey_Dt__c= Date.today();
//acc.Survey_Opp_Id__c=opp.id;
insert acc;
Opportunity opp= new Opportunity();
opp.AccountId = acc.Id;
opp.StageName='Active';
opp.Placmnt_Start_Date_Arrive__c = Date.ValueOf('2/15/2011');
insert opp;
HFSendSurveyClass hfsend = new HFSendSurveyClass();
hfsend.SendSurvey();
config = [Select HFSurveyCode__c, Survey_Date__C, SOQL_Query__C from Config_Object__C where Name =:config.Name];
acc = [Select Survey_Code__c,Survey_Dt__c,Survey_Opp_Id__c from Account where id =:opp.AccountId ];
System.assertEquals(config.HFSurveyCode__c,acc.Survey_Code__c);
System.assertEquals(config.Survey_Date__C,acc.Survey_Dt__c);
//System.assertEquals(opp.Id,acc.Survey_Opp_Id__c);
}
}
static testMethod void SendSurvey()
{
// Get the most recently created config object.
Config_Object__c config = new Config_Object__c();
config.Name = 'HostFamily_Survey';
config.HFSurveyCode__c = 'HF1MoArv';
config.Survey_Date__c = Date.today();
config.SOQL_query__c = string.ValueOf('Select o.Id, o.Account.Id, o.AccountId, o.Account.Survey_Opp_Id__c, o.Account.Survey_Dt__c, o.Account.Survey_Code__c, o.Account.Name From Opportunity o where o.StageName= &#Active&# AND o.Placmnt_Start_Date_Arrive__c < Last_N_days :30 AND o.Placmnt_Start_Date_Arrive__c = Last_N_days :60');
insert config;
Account acc = new Account();
//acc.Name = 'TestSurvey';
acc.Survey_Code__c = 'HF1MoArv';
acc.Survey_Dt__c= Date.today();
//acc.Survey_Opp_Id__c=opp.id;
insert acc;
Opportunity opp= new Opportunity();
opp.AccountId = acc.Id;
opp.StageName='Active';
opp.Placmnt_Start_Date_Arrive__c = Date.ValueOf('2/15/2011');
insert opp;
HFSendSurveyClass hfsend = new HFSendSurveyClass();
hfsend.SendSurvey();
config = [Select HFSurveyCode__c, Survey_Date__C, SOQL_Query__C from Config_Object__C where Name =:config.Name];
acc = [Select Survey_Code__c,Survey_Dt__c,Survey_Opp_Id__c from Account where id =:opp.AccountId ];
System.assertEquals(config.HFSurveyCode__c,acc.Survey_Code__c);
System.assertEquals(config.Survey_Date__C,acc.Survey_Dt__c);
//System.assertEquals(opp.Id,acc.Survey_Opp_Id__c);
}
}
And the apex class
public class HFSendSurveyClass {
public void SendSurvey()
{
// Get the most recently created config object.
Config_Object__c config = [Select HFSurveyCode__c, Survey_Date__C, SOQL_Query__C from Config_Object__C
where Name ='HostFamily_Survey' Order by CreatedDate desc Limit 1];
List<Opportunity> oppList = Database.query(config.SOQL_Query__c);
List<Account> accList = new List<Account>();
for(Opportunity opp:oppList)
{
if(opp.AccountId!= null)
{
Account acc = opp.Account;
if(acc.Survey_Code__c != String.valueOf(config.HFSurveyCode__c) || acc.Survey_Opp_Id__c != opp.Id)
{
string oppcode= opp.Id;
acc.Survey_Code__c = String.valueOf(config.HFSurveyCode__c);
acc.Survey_Dt__c = config.Survey_Date__c;
acc.Survey_Opp_Id__c = oppcode.subString(0,15);
}
accList.add(acc);
}
}
update accList;
}
}
public void SendSurvey()
{
// Get the most recently created config object.
Config_Object__c config = [Select HFSurveyCode__c, Survey_Date__C, SOQL_Query__C from Config_Object__C
where Name ='HostFamily_Survey' Order by CreatedDate desc Limit 1];
List<Opportunity> oppList = Database.query(config.SOQL_Query__c);
List<Account> accList = new List<Account>();
for(Opportunity opp:oppList)
{
if(opp.AccountId!= null)
{
Account acc = opp.Account;
if(acc.Survey_Code__c != String.valueOf(config.HFSurveyCode__c) || acc.Survey_Opp_Id__c != opp.Id)
{
string oppcode= opp.Id;
acc.Survey_Code__c = String.valueOf(config.HFSurveyCode__c);
acc.Survey_Dt__c = config.Survey_Date__c;
acc.Survey_Opp_Id__c = oppcode.subString(0,15);
}
accList.add(acc);
}
}
update accList;
}
}
Can any one please suggest if my test class is correct. or how can i get 75% code coverage.
Thanks in advance.
Unfortunately you have a test failure there, so you wouldn't be able to deploy to production.
When you are creating your test opportunity, it is failing because you haven't supplied the Name field.
You'll need something like:
Try changing this an re-running your test.
Hello Bob,
Thank you so much for your response.
Yes i now actually did the change and added
opp.Name
acc.Name = 'Test acc';
public class HFSendSurveyTestClass {
static testMethod void SendSurvey()
{
// Get the most recently created config object.
Config_Object__c config = new Config_Object__c();
config.Name = 'HostFamily_Survey';
config.HFSurveyCode__c = 'HF1MoArv';
config.Survey_Date__c = Date.today();
config.SOQL_query__c = string.ValueOf('Select o.Id, o.Account.Id, o.AccountId, o.Account.Survey_Opp_Id__c, o.Account.Survey_Dt__c, o.Account.Survey_Code__c, o.Account.Name From Opportunity o where o.StageName= &#Active&# AND o.Placmnt_Start_Date_Arrive__c < Last_N_days :30 AND o.Placmnt_Start_Date_Arrive__c = Last_N_days :60');
insert config;
Account acc = new Account();
acc.Name = 'Test acc';
acc.Survey_Code__c = 'HF1MoArv';
acc.Survey_Dt__c= Date.today();
//acc.Survey_Opp_Id__c=opp.id;
insert acc;
Opportunity opp= new Opportunity();
opp.Name='Test Opp';
opp.AccountId = acc.Id;
opp.StageName='Active';
//Date plcdate = new Date();
opp.Placmnt_Start_Date_Arrive__c = Date.valueof('02/10/2011');
insert opp;
HFSendSurveyClass hfsend = new HFSendSurveyClass();
hfsend.SendSurvey();
config = [Select HFSurveyCode__c, Survey_Date__C, SOQL_Query__C from Config_Object__C where Name =:config.Name];
acc = [Select Survey_Code__c,Survey_Dt__c,Survey_Opp_Id__c from Account where id =:opp.AccountId ];
System.assertEquals(config.HFSurveyCode__c,acc.Survey_Code__c);
System.assertEquals(config.Survey_Date__C,acc.Survey_Dt__c);
System.assertEquals(opp.Id,acc.Survey_Opp_Id__c);
}
}
Can you please suggest Bob.
Hmm. It seems to be having problems with your opportunity date - not sure where it is getting 2/16/2011 from, but Date.valueof expects a different format - “yyyy-MM-dd HH:mm:ss”.
Its probably as easy to do: