+ Start a Discussion
Kamil MieczakowskiKamil Mieczakowski 

Writing a test class for a for loop based method

Hi Guys, I wrote the below code and test method for it, but for some reason my test method is not providing sufficient coverage for deployment. I run out of ideas on how to increase the coverage of this test method. Any advice on how to do it?

Actual code:
public class  standardiseWebsites {
 
    public static void standardiseWebsites(){
    //extracting the host from the address for websites that have web-protocol (http:// and https://)
    list<Account> accts = [SELECT Website FROM Account WHERE Website LIKE 'http://%' OR Website LIKE 'https://%'];
        for (Account acct : accts){            
            string website = acct.Website;
            Url u = new Url(acct.Website);
            acct.Website = u.getHost();
            update acct;
        }
        
      //removing first 4 digits of the address if it starts with 'www.' (non-web-protocol values)
      list<Account> accts2 = [SELECT Website FROM Account WHERE Website LIKE 'www.%'];
        for (Account acct2 : accts2){
            string website = acct2.website;
            website = website.substring(4);
            acct2.website = website;
            update acct2;
        }
    }
}
Test:
@isTest
public class standardiseWebsitesTest {
 
        static void standardiseWebsitesTest() {
        standardiseWebsites.standardiseWebsites();
    }
}

 
Best Answer chosen by Kamil Mieczakowski
SFDC EvangelistSFDC Evangelist
Kamil, this is because you have to create your Test Data before calling the Class method. You test data should satisfy the query condition; which will return Account data and then only it will go inside your for-loop.

Something like this below.

@isTest
public class standardiseWebsitesTest {
 
    @testSetup
    public static void setupData()
    {
        Account acct = new Account();
        acct.Name='Test Account';
        acct.Website='http://www.google.com';
        insert acct;
    }
    
     static testMethod void standardiseWebsitesTest() {
        standardiseWebsites.standardiseWebsites();
    }
}

All Answers

SFDC EvangelistSFDC Evangelist
Hi Kamil,

You have to use the below code to run the Test Class successfully and to have coverage on the Apex Code.

static testMethod void standardiseWebsitesTest() {
       standardiseWebsites.standardiseWebsites();
 }

Salesforce will not recognize the method as test method; unless you mention "testMethod" in the Test Class method. 


 
Kamil MieczakowskiKamil Mieczakowski
SFDC Evangelist, thank you for this. I run with testMethod added. Unfortunately, the coverage I am getting is only at 38% (5/13).
GulshanRajGulshanRaj
Hi Kamil,

You are missing generating test data.
Here is code which will help you to move forward:
@isTest
public class standardiseWebsitesTest {
 
	 @testSetup
 	public static void GenerateData()
 	{
 		Account acc = new Account();
 		acc.Name='Test Account';
 		acc.Website='http://www.salesforce.com';
 		insert acc;
 	}
        static testmethod void  testOne() {
        standardiseWebsites.standardiseWebsites();
    }
}
The code which I provide above will resolve your code coverage issue.

Moreove, I urge you to please refer following documents to learn more about test classes and get started:
1)Trailhead: https://trailhead.salesforce.com/en/modules/apex_testing/units/apex_testing_intro
2)  Best Practices: https://s3.amazonaws.com/dfc-wiki/en/images/4/41/Apex_code_testing_webinar.pdf


By the way I also explained about you "void" method issue in your differnt thread. Please read that as well.

Thanks
Gulshan Raj

 
SFDC EvangelistSFDC Evangelist
Kamil, this is because you have to create your Test Data before calling the Class method. You test data should satisfy the query condition; which will return Account data and then only it will go inside your for-loop.

Something like this below.

@isTest
public class standardiseWebsitesTest {
 
    @testSetup
    public static void setupData()
    {
        Account acct = new Account();
        acct.Name='Test Account';
        acct.Website='http://www.google.com';
        insert acct;
    }
    
     static testMethod void standardiseWebsitesTest() {
        standardiseWebsites.standardiseWebsites();
    }
}
This was selected as the best answer