-2

Apex Class Code:

public class task1 {
public string obj{set;get;}
public integer year{set;get;}
public list<account> acclist{set;get;}
public list<contact> conlist{set;get;}
public list<opportunity> opplist{set;get;}
public list<lead> lelist{set;get;}
public boolean az{set;get;}
public boolean bz{set;get;}
public boolean cz{set;get;}
public boolean dz{set;get;}
public task1(){
acclist=new list<Account>();
az=false;
bz=false;
cz=false;
dz=false;
}
public void getdata(){
if(obj.equals('None')&&year==0)
{}else
if(obj.equals('Account')){
acclist=[select id,name from account where calendar_year(createddate)=:year];
az=true;
}else
if(obj.equals('contact')){
conlist=[select id,lastname from contact where calendar_year(createddate)=:year];bz=true;
}
if(obj.equals('lead')){
lelist=[select id,lastname from lead where calendar_year(createddate)=:year];cz=true;
}
}
}

Test Class Code:

@istest
public class task1_tc{
static testmethod void tstmethd(){
 task1 t=new task1();
 t.obj='';
 Account a =new Account(name='xxx');
 Contact c=new contact(lastname='xxx');
 Opportunity o=new Opportunity(name='xxx');
 Lead l=new Lead(lastname='xxx');
 integer year=2014;
 t.az=false;
 t.bz=false;
 t.cz=false;
 t.dz=false;
 list<Account> acclist=new list<Account>();
 acclist.add(a);
 list<contact> conlist=new list<contact>();
 conlist.add(c);
 list<opportunity> opplist=new list<opportunity>();
 opplist.add(o);
 list<lead> lelist=new list<lead>();
 lelist.add(l);
 t.getdata();
 }
 }

image Code Coverage: enter image description here

  • 1
    In your test class, set the obj variable to 'None' , 'Account' , 'contact' , 'lead' and then call the getdata() method to match the conditions you have used. – Vamsi Krishna Feb 8 '15 at 11:17
  • see vamsi its not correct which u said if u can do it please modify it and send the code man i think it is clear to you which i had posted the question ok just do it and send if it possible from you – Abdul Azeem Feb 8 '15 at 12:28
  • In this assignment statement from your test class " t.obj=''; , did you try assigning with values like 'None', 'Account' etc and rerun the test ? – Varun Feb 8 '15 at 17:09
  • AzuSfdc, can you explain us why you are stuck and what's confusing you about covering those lines from your test ? Without knowing so we don't know, and can't explain you either. – Samuel De Rycke Feb 8 '15 at 22:26
  • @AzuSfdc we can give directions on how you can approach the problem / solve them.. but giving direct code answers may not help you to understand / handle these type of issues in future. – Vamsi Krishna Feb 8 '15 at 23:19
0

You have a series of if-else statements in your class and appear to be missing a final "else" before the last "if" statement. That means you need at least four separate unit test methods. One for each of the if statement conditions that you're testing in your class beginning with one that supplies data which satisfies the condition where if(obj.equals('None')&&year==0). You also need to assert that condition at the end of your method; not just see that the line of code is executed.

0

You have to test for all the different possibilities.

In your code you set t.obj = ''. You have to set it to 'Account' and then call getdata(), then set it to 'contact' and call getdata(), then set it to 'lead' and call getdata();

t.obj = 'Account';
t.getdata();
//do any account assertions you want here
t.obj = 'contact';
t.getdata();
//do any contact assertions you want here
t.obj = 'lead';
t.getdata();
//do any lead assertions you want here

You can also just delete these lines and it won't have any effect:

if(obj.equals('None')&&year==0)
{}else

Because they aren't doing anything.

Here's a cleaned up version of that method:

public void getdata(){
  if(obj.equals('Account')){
    acclist=[select id,name from account where calendar_year(createddate)=:year];
    az=true;
  } else if(obj.equals('contact')) {
    conlist=[select id,lastname from contact where  calendar_year(createddate)=:year];bz=true;
  } else if(obj.equals('lead')){
    lelist=[select id,lastname from lead where calendar_year(createddate)=:year];cz=true;
  }
}

}

If you're going to use strings for this you probably also want some consistency - either all lower case, or start them all with an uppercase (so 'account' and 'lead' OR 'Account' and 'Lead').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.