0

controller code:

public with sharing class silogletter {
    public Contact contact {get;set;}
public silogletter(ApexPages.StandardController controller)
    {
    for(contact record:[select Name, MobilePhone, Title,email
         from Contact where AccountId = :controller.getId() and title='CEO' limit 2]){
        if(contact==null)
        contact=record;
        else
        contact=null;
    }

test case:

@istest

public class testsilogletter{
 static testmethod void unittest(){
Account a=new account(name='IndAccount',billingcountry='india');
insert a;
contact c=new contact(lastname='reert',mailingcountry='india',accountid=a.id);
insert c;
contact co=[SELECT id,name,accountid,title FROM contact WHERE id=:c.id ];
// System.assertEquals(c1.name,'test');  

       Test.startTest();
        // start the test execution context

        // set the test's page to your VF page (or pass in a PageReference)
        Test.setCurrentPage(Page.account_details_for_silog);

        // call the constructor
        silogletter controller = new silogletter(new ApexPages.StandardController(a));
   if(co.title=='CEO')
        system.assertEquals(co.name,'reert');
        // stop the test


   if(co.title=='purchaser')   
       system.assertEquals(co.name,'dwqfwer');


        Test.stopTest();
    }
}

In my code coverage it is not covering the

        if(contact==null)
        contact=record;
        else
        contact=null;

Can you help how to assert this one?

3
  • for your set of code if(contact==null) contact=record; else contact=null; dont u think if and else is doing the same work.your code should be if(contact!=null)
    – Sonal
    Commented Jan 24, 2014 at 12:19
  • this login is fine .it is working well
    – Teja
    Commented Jan 24, 2014 at 12:22
  • if it don't have two records(limit 2).it will take the the record .otherwise it return null
    – Teja
    Commented Jan 24, 2014 at 12:24

1 Answer 1

3

You have the following conditions where AccountId = :controller.getId() and title='CEO' in your where clause yet the contact inserted in your test data will not meet the title='CEO criteria, therefore your loop will not run the following lines as no records are there to iterate over.

if(contact==null)
    contact=record;
else
    contact=null;

To gain coverage on these lines you will need to set the Title field on your inserted contact i.e.

contact c=new contact(lastname='reert',mailingcountry='india',accountid=a.id, title='CEO');
insert c;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .