+ Start a Discussion
sfdc18sfdc18 

test class not covering controller code

Hi,

I need a help in test coverage for a controller having wrapper class. Currently it is covering only 60% of code.

Apex Controller :

User-added image
User-added image
User-added image
User-added image
User-added image

Test Class :
 

@isTest(SeeAllData = true) 
public class CopyAttachmentControllerTest {

private  static  RecordType supportCaseRecordType  = [SELECT Name, Id FROM RecordType 
                                                        WHERE Name ='Support Case' 
                                                        AND SObjectType = 'Case'];
                                                        
private  static  RecordType L2CaseRecordType  = [SELECT Name, Id FROM RecordType 
                                                        WHERE Name ='L2 Case' 
                                                        AND SObjectType = 'Case'];

    static testmethod void CopyAttachmentTestCase(){
        Account   testAccount = new Account();
        testAccount.Name  = 'test';
    
        insert testAccount;
    
        Contact   testContact = new Contact();
        testContact.LastName  = 'Test Name';
        testContact.AccountId = testAccount.Id;
    
        insert testContact;

        Product2  testProduct = new Product2();
        testProduct.Id                    = null;
        testProduct.Name                  = 'Test Product Name';
        testProduct.Product_Category__c   = 'Category';
        testProduct.Product_Family__c     = 'Family';
        testProduct.Product_Sub_family__c = 'Sub-Family';    

        insert testProduct;
    
        Case  testCase  = new Case();
        testCase.RecordTypeId = supportCaseRecordType.Id;
        testCase.Summary__c   = 'Summary';
        testCase.Description  = 'Description';
        testCase.Origin       = 'Email';
        testCase.Status       = 'New';
        testCase.I_Agree__c   = true;
        testCase.ContactId    = testContact.Id;
        testCase.ProductId    = testProduct.Id;

        insert testCase;
        
        Case  testCase2  = new Case();
        testCase2.RecordTypeId = L2CaseRecordType.Id;
        testCase2.Summary__c   = 'Summary2';
        testCase2.Description  = 'Description2';
        testCase2.Origin       = 'Email2';
        testCase2.Status       = 'New2';
        testCase2.I_Agree__c   = true;
        testCase2.ContactId    = testContact.Id;
        testCase2.ProductId    = testProduct.Id;

        insert testCase2;


        Attachment att = new Attachment();
        att.ParentId = testCase.Id;
        att.name = 'test'; 
        att.body = blob.valueof('test');
        
        insert att;

        Attachment attDup = new Attachment();
        attDup.ParentId = testCase2.Id;
        attDup.name = att.name; 
        attDup.body = att.body;
        
        List<Attachment> listatt = new List<Attachment>();
        listatt.add(attDup);
        insert listatt;
        System.assertEquals(1, listatt.size());
        
        Id idSelected;
        ApexPages.StandardController sc = new ApexPages.standardController(testCase);
        CopyAttachmentController cls = new CopyAttachmentController(sc);
                cls.Cancel();
        cls.selectall = true;
        cls.displayAttTable = true;
        cls.matchingAttTable = true;        
        CopyAttachmentController.AttachmentWrapper innerClass = new CopyAttachmentController.AttachmentWrapper(att);
        innerClass.selected = true;
        innerClass.att = att;
        List<CopyAttachmentController.AttachmentWrapper> attList = new List<CopyAttachmentController.AttachmentWrapper>();
        //attList = null;
        system.currentPageReference().getParameters().put('idSelected', testCase.Id);
        System.assert(testCase.Id != null);
        
        List<CopyAttachmentController.AttachmentWrapper> listInnerClass = new List<CopyAttachmentController.AttachmentWrapper>();
        listInnerClass.add(new CopyAttachmentController.AttachmentWrapper(attDup));
        //List<string> s = new List<string>();
        //s.add(att.name);
        //cls.matchingFiles = s;        
        cls.SelectAttachments();
        //System.assert(cls.matchingFiles.size()>0); 

    }
}
Thanks
pconpcon
For starters you really need to split this into multiple smaller tests and actually have asserts that test real behavior.  I would recommend reading over this article [1] to learn how you can decide how to do the tests.  As for why those lines are not being covered it's because you do not have the correct data inserted, or your data is not in the expected state.  For example you likely do not have an Attachment record that is selected by line 29 of your class.  This then causes you to not have any AttachmentWrappers and therefore lines 39, 48 and 49 are never hit.  You would know this if you had a System assert that checked to make sure that the ApexPage did not have any errors after running your code.

Also, never use seeAllData in your test unless you know 100% why you are using it.  And given your test, there is no reason to use it.

[1] http://blog.deadlypenguin.com/blog/testing/strategies/