0

I've got a line of code that I can't cover in my tests:

enter image description here Any idea of how I can cover this?

Apex Class:

public class wrapperclass_1 {
public list<wrapperclass> wrapperlist{set;get;}
public list<wrapperclass> wrapperlist1{set;get;}
public list<Account> acclist{set;get;}
public list<account> selectedacc{set;get;}
public list<contact> conlist{set;get;}
public list<contact> selectedcon{set;get;}
public wrapperclass_1()
{
wrapperlist=new list<wrapperclass>();
wrapperlist1=new list<wrapperclass>();
acclist=[select id,name,rating,annualrevenue from account limit 5];
conlist=[select id,name,email,phone from contact limit 5];
for(account a:acclist)
{
wrapperclass w=new wrapperclass();
w.cbox=false;
w.acc=a;
wrapperlist.add(w);
}
for(contact c:conlist)
{
wrapperclass w1=new wrapperclass();
w1.cbox=false;
w1.con=c;
wrapperlist1.add(w1);
}
}
//5
public void selectedrec()
{
selectedacc=new list<account>();
for(wrapperclass wc:wrapperlist)
{
if(wc.cbox==true)
selectedacc.add(wc.acc);
}
}
public class wrapperclass{
public boolean cbox{set;get;}
public account acc{set;get;}
public contact con{set;get;}
public wrapperclass()
{
acc=new account();
}
}
}

VF Page:

<apex:page controller="wrapperclass_1">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!wrapperlist}" var="w">
<apex:column >
<apex:inputCheckbox value="{!w.cbox}"/>
</apex:column>
<apex:column value="{!w.acc.name}"/>
<apex:column value="{!w.acc.rating}"/>
<apex:column value="{!w.acc.annualrevenue}"/>
</apex:pageBlockTable>
</apex:pageBlock>
<apex:commandButton value="Selected Record" action="{!selectedrec}"/>
<apex:pageBlock >
<apex:pageBlockTable value="{!selectedacc}" var="a">
<apex:column value="{!a.name}"/>
<apex:column value="{!a.rating}"/>
<apex:column value="{!a.annualrevenue}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

TestClass:

@istest(seealldata=true)
public class wrapperclass_1_Tc{
//public boolean cbox{set;get;}
static testmethod void wrapperclass_1_Tc()
{
account a=new account(name='xxx',rating='xxx',annualrevenue=888);
insert a;
wrapperclass_1 w1=new wrapperclass_1();
w1.selectedrec();
}
}

Image:

  • 1
    can you be specific on what's your issue and give more details on your question, also you are missing the image. – Vamsi Krishna Jun 1 '15 at 2:05
1

Looking at the wrapperclass_1 code you included in the question, that line requires at least one item in the wrapperlist to have a true cbox value. So one of your test methods will need to look something like this:

static testmethod void cbox() {
    insert new account(name='xxx', rating='xxx', annualrevenue=888);
    wrapperclass_1 w1 = new wrapperclass_1();
    for (wrapperclass wc : w1.wrapperlist) {
        wc.cbox = true;
    }
    w1.selectedrec();
    System.assertEquals(w1.wrapperlist.size(), w1.selectedacc.size());
}

Don't get too hung up on code coverage: tests that cover 90% of the code but assert all the required behaviour are better than tests that cover 100% of the code but don't assert all the required behaviour. Focus on checking that the code works in your tests and the coverage will happen as a side effect.

But before adding more tests, I suggest you should first improve the names you are using in your code. Generally it is better to use names from the problem domain.

Also @istest(seealldata=true) is best avoided and is not needed in your tests.

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.