+ Start a Discussion
designdesign 

Test coverage for custom controller

Hello,

in addition to this post, I made a controller for visualforce page, but I can't test cover lines in the if conditionals? Any idea how to do it? Now i have only 26% test coverage.

 

Controller:

 

public class ManageAssets {

Asset tempAssets;

List<Contact> contactLines = new List<Contact>();

List<Asset> assetLines = new List<Asset>();

 

public ManageAssets() {

// left blank

}

 

public PageReference launch() {

startProcess();

PageReference thisPage = ApexPages.currentPage();

thisPage.setRedirect(true);

return thisPage;

}

 

public void startProcess() {

List<Asset> tempAssets = [select Id, Import_AccountName__c, Import_AccountCity__c,

Import_AccountAddress__c, Import_AccountPhone__c, Import_AccountPhone2__c,

Import_AccountUrl__c, Import_FirstName1__c, Import_FirstName2__c,

Import_FirstName3__c, Import_LastName1__c, Import_LastName2__c,

Import_FirstName3__c, Import_LastName1__c, Import_LastName2__c,

Import_LastName3__c, Import_Title1__c, Import_Title2__c, Import_Title3__c,

Import_MobilePhone1__c, Import_MobilePhone2__c, Import_MobilePhone3__c,

Import_ProductCode__c, InstallDate, SerialNumber

from Asset

where Account.Name = 'Account For Imports'

and CreatedDate = today];

 

for (Integer i=0; i < tempAssets.size(); i++) {

String AccId;

if (tempAssets[i].Import_AccountName__c != null) {

List<Account> acc = [Select Id from Account where Name = :tempAssets[i].Import_AccountName__c];

if (acc.size() == 0){

Account accnew;

accnew = new Account();

accnew.Name = tempAssets[i].Import_AccountName__c;

accnew.BillingCity = tempAssets[i].Import_AccountCity__c;

accnew.BillingStreet = tempAssets[i].Import_AccountAddress__c;

accnew.Phone = tempAssets[i].Import_AccountPhone__c;

accnew.Tel2__c = tempAssets[i].Import_AccountPhone2__c;

accnew.Website = tempAssets[i].Import_AccountUrl__c;

insert accnew;

accId = accnew.Id;

} else {

accId = acc[0].Id;

}

}

 

if (tempAssets[i].Import_FirstName1__c != null){

Contact cnt1 = new Contact();

cnt1.FirstName = tempAssets[i].Import_FirstName1__c;

cnt1.LastName = tempAssets[i].Import_LastName1__c;

cnt1.Title = tempAssets[i].Import_Title1__c;

cnt1.Phone = tempAssets[i].Import_MobilePhone1__c;

cnt1.AccountId = accId;

contactLines.add(cnt1);

}

 

if (tempAssets[i].Import_FirstName2__c != null){

Contact cnt2 = new Contact();

cnt2.FirstName = tempAssets[i].Import_FirstName2__c;

cnt2.LastName = tempAssets[i].Import_LastName2__c;

cnt2.Title = tempAssets[i].Import_Title2__c;

cnt2.Phone = tempAssets[i].Import_MobilePhone2__c;

cnt2.AccountId = accId;

contactLines.add(cnt2);

}

 

if (tempAssets[i].Import_FirstName3__c != null){

Contact cnt3 = new Contact();

cnt3.FirstName = tempAssets[i].Import_FirstName3__c;

cnt3.LastName = tempAssets[i].Import_LastName3__c;

cnt3.Title = tempAssets[i].Import_Title3__c;

cnt3.Phone = tempAssets[i].Import_MobilePhone3__c;

cnt3.AccountId = accId;

contactLines.add(cnt3);

}

 

if (tempAssets[i].Import_ProductCode__c != null){

List<Product2> prod = [Select Id, Name from Product2 where ProductCode = :tempAssets[i].Import_ProductCode__c];

if (prod.size() > 0){

Asset assetNew;

assetNew = new Asset();

assetNew.Name = prod[0].Name;

assetNew.Product2Id = prod[0].Id;

assetNew.AccountId = accId;

assetNew.Status = 'Установлено';

assetNew.SerialNumber = tempAssets[i].SerialNumber;

assetLines.add(assetNew);

}

}

}

insert contactLines;

insert assetLines;

}

}

 

 Test class:

 

static testMethod void ManageAssets(){

String accId;

Product2 product = new Product2(Name = 'Test Product');

product.ProductCode = '1234567890';

insert product;

System.assert(product.id != null);

 

Account acc = new Account(Name = 'Test Account');

insert acc;

System.assert(acc.id != null);

 

accId = acc.id;

System.assert(accId != null);

 

Contact cnt = new Contact();

cnt.FirstName = 'Test';

cnt.LastName = 'Test';

cnt.AccountId = acc.Id;

insert cnt;

 

Asset asst = new Asset();

asst.Name = 'Test Product';

asst.Product2Id = product.Id;

asst.AccountId = acc.Id;

asst.ContactId = cnt.Id;

asst.Import_AccountAddress__c = 'Test Street';

asst.Import_AccountCity__c = 'Test City';

asst.Import_AccountName__c = 'Test Account';

asst.Import_AccountPhone2__c = '1234567890';

asst.Import_AccountPhone__c = '1234567890';

asst.Import_AccountUrl__c = 'www.test.com';

asst.Import_FirstName1__c = 'Test';

asst.Import_FirstName2__c = 'Test2';

asst.Import_FirstName3__c = 'Test3';

asst.Import_LastName1__c = 'Test';

asst.Import_LastName2__c = 'Test2';

asst.Import_LastName3__c = 'Test3';

asst.Import_MobilePhone1__c = '1234567890';

asst.Import_MobilePhone2__c = '1234567890';

asst.Import_MobilePhone3__c = '1234567890';

asst.Import_Title1__c = 'Title';

asst.Import_Title2__c = 'Title';

asst.Import_Title3__c = 'Title';

asst.Import_ProductCode__c = '1234567890';

insert asst;

System.assert(asst.Id != null);

System.assert(asst.Import_FirstName1__c != null);

System.assert(asst.Import_FirstName2__c != null);

System.assert(asst.Import_FirstName3__c != null);

System.assert(asst.Import_AccountName__c != null);

System.assert(asst.Import_ProductCode__c != null);

 

ManageAssets cc = new ManageAssets();

cc.startProcess();

 

PageReference pageRef = Page.ManageAssets;

Test.setCurrentPage(pageRef);

pageRef = cc.launch();

if (pageRef != null) {

Test.setCurrentPage(pageRef);

}

}

 

 

 

 

Message Edited by design on 07-29-2009 08:02 AM
Message Edited by design on 07-29-2009 08:04 AM
Message Edited by design on 07-29-2009 08:09 AM
Message Edited by design on 07-29-2009 08:10 AM
Best Answer chosen by Admin (Salesforce Developers) 
designdesign

Thanks to All!

All decided by adding multiple entries instead of one

Message Edited by design on 07-29-2009 12:56 PM

All Answers

wesnoltewesnolte

Hey

 

It seems that you don't have the correct data to test each if statement. You will need to create data in your test such that each branch of your code can be reached. The data needs to be created in your test class because when you deploy it to a new org there will be no data there, thus your testing will be data independent. More on this can be found here.

 

Cheers,

Wes 

designdesign

Thanks to All!

All decided by adding multiple entries instead of one

Message Edited by design on 07-29-2009 12:56 PM
This was selected as the best answer