You need to sign in to do that
Don't have an account?
Kamil Mieczakowski
for loop test
Hi, I am attempting to write a simple test for a for loop that cleans the websites fields of unwanted elements such as 'http', 'www' etc. The script works for me but the test class that I wrote gets me 0% of coverage. Could anyone advise on what I am doing wrong? Thank you in advance.
Here's the actual script:
And here's the test script:
Here's the actual script:
public class standardiseWebsites { list<Account> accts = [SELECT Website FROM Account]; public standardiseWebsites(){ for (Account acct : accts){ string website = acct.website; website = website.replace('http://www.',''); website = website.replace('https://www.',''); website = website.replace('https://',''); website = website.replace('http://',''); website = website.replace('www.',''); acct.website = website; update acct; } } }
And here's the test script:
@isTest public class standardiseWebsitesTest { static testMethod void standardiseWebsitesTest(){ //Create test Accounts with various types of websites Account account1 = new Account(Name = 'Test Account1', Website='https://www.test.com'); insert account1; Account account2 = new Account(Name = 'Test Account1', Website='http://www.test.com'); insert account2; Account account3 = new Account(Name = 'Test Account1', Website='https://test.com'); insert account3; Account account4 = new Account(Name = 'Test Account1', Website='http://test.com'); insert account4; Account account5 = new Account(Name = 'Test Account1', Website='www.test.com'); insert account5; Account account6 = new Account(Name = 'Test Account1', Website='test.com'); insert account6; Test.startTest(); System.debug(accts); StandardiseWebsitesTest obj = new standardiseWebsitesTest(); Test.stopTest(); //Check the desired results system.assertEquals(Account1.Website,'test.com'); system.assertEquals(Account2.Website,'test.com'); system.assertEquals(Account3.Website,'test.com'); system.assertEquals(Account4.Website,'test.com'); system.assertEquals(Account5.Website,'test.com'); system.assertEquals(Account6.Website,'test.com'); } }
Just as a quick tip, many other programming languages will not allow you to make your method name the same as the class name (if you understand what a constructor is, then you may have an idea of why), so I changed your method name to simply standardise() to make things less confusing. Anyway, here are some other suggestions:
standardiseWebsites:
standardiseWebsitesTest:
I hope this helps!
All Answers
There are a few different things that are wrong with your code:
Once you fix all of these, please feel free to post your updated code here and I will be happy to look through it again and make sure it looks correct. (I also have some best practice tips I would like to share after you get your code working.)
Therefore I decided to extract the host using the getHost() method in Apex: However ,I am now facing another problem. Some of the Website addresses are already recorded without the protocol (without 'http(s)://' or www. preceding it, e g google.com instead of https://google.com), and when the script comes across them it throws the below error: So I have a couple of questions:
1. How do I create an if loop inside of a for loop that would determine if the given website address comes with a protocol. If it does it would run the script and if it doesn't it would move on to the next website. I tried to use the following condition, but it would return 'Method does not exist or incorrect signature: [Url].Contains(String)' error: 2. Why is my script only running when I don't define the method as void ? As soon as I add void to the method the script runs but with no effect.
Here's the script (I marked the relevant if loop with question marks):
3. If I now add an if loop into the script how does it impact testing?
Thank you in advance for all your answers.
Just as a quick tip, many other programming languages will not allow you to make your method name the same as the class name (if you understand what a constructor is, then you may have an idea of why), so I changed your method name to simply standardise() to make things less confusing. Anyway, here are some other suggestions:
standardiseWebsites:
standardiseWebsitesTest:
I hope this helps!