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

Need some clarification with Test class
Hello Guys,
I'm into a situation with a Test class , please help me to resolve it .
My Apex Code : -
public class Acc_dtls_nw
{
public String s;
public integer i =0;
public string acc_type_check(String Type_value)
{
string qry ='select count() from account where type=:Type_value';
i= database.countQuery(qry);
if (i>0)
{
s='Total no of account Types are: '+i;
}
else
{
s='Invalid type :';
}
return s;
}}
I'm not able to get a 100% code coverage for this class. below part is not included under code coverage
s='Total no of account Types are: '+i;
Now the test class I wrote is below
@istest
public class test_acc_dtls_nw
{
static testMethod void test_trucond()
{
Acc_dtls_nw ac= new Acc_dtls_nw();
string tst_true = ac.acc_type_check('Prospect');
// System.AssertEquals(ac.i,2);
string p ='prospect';
// System.AssertEquals(ac.s,p);
}
static testMethod void test_flscond()
{
Acc_dtls_nw ac= new Acc_dtls_nw();
string tst_flse = ac.acc_type_check('xyz');
// System.AssertEquals(ac.i,0);
}
}
So why that part is getting missed under code coverage? is it because S is not doing anything and used just for strong some text value? please help me to get this clarified.
I'm into a situation with a Test class , please help me to resolve it .
My Apex Code : -
public class Acc_dtls_nw
{
public String s;
public integer i =0;
public string acc_type_check(String Type_value)
{
string qry ='select count() from account where type=:Type_value';
i= database.countQuery(qry);
if (i>0)
{
s='Total no of account Types are: '+i;
}
else
{
s='Invalid type :';
}
return s;
}}
I'm not able to get a 100% code coverage for this class. below part is not included under code coverage
s='Total no of account Types are: '+i;
Now the test class I wrote is below
@istest
public class test_acc_dtls_nw
{
static testMethod void test_trucond()
{
Acc_dtls_nw ac= new Acc_dtls_nw();
string tst_true = ac.acc_type_check('Prospect');
// System.AssertEquals(ac.i,2);
string p ='prospect';
// System.AssertEquals(ac.s,p);
}
static testMethod void test_flscond()
{
Acc_dtls_nw ac= new Acc_dtls_nw();
string tst_flse = ac.acc_type_check('xyz');
// System.AssertEquals(ac.i,0);
}
}
So why that part is getting missed under code coverage? is it because S is not doing anything and used just for strong some text value? please help me to get this clarified.
Use below test class. Also, visit below links.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
Please let us know if thi helps :)
Thanks,
AMit Singh
All Answers
Is that Acc_dtls_nw class you created working properly? I checked in the method acc_type_check. Do you able to use that parameter Type_value? I think you need to use something like this : string qry ='select count() from account where type=:'+Type_value;
Use below test class. Also, visit below links.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
Please let us know if thi helps :)
Thanks,
AMit Singh
Ok so it mandatory to insert record(s) in test class if Im using object records in apex class?
Thanks,