function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sebastien PlanteSebastien Plante 

Confused with test class... simple Apex code.

I got this simple code 
 
public class AccountShipTo {

    public Account originalAccount{get; set;}
    public list<Account> accnt {get;set;}

    public AccountShipTo (ApexPages.StandardController controller) {
        originalAccount = [SELECT Id, ParentId FROM Account WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
        
        accnt = new list<Account>();
        accnt = [SELECT Id, Name, ShippingStreet,ShippingCity,ShippingState,ShippingCountry,ShippingPostalCode FROM Account WHERE ParentId = :originalAccount.Id AND ShippingStreet<>''];
    }

}

and I want to make a test class
 
@isTest(seealldata=true)
public class AccountShipToTest {

    @isTest
    public static void testAccountShipTo(){
        
        Account originalAccount = [Select Id from Account limit 1];
        
        list<Account> accnt = [SELECT Id, Name, ShippingStreet,ShippingCity,ShippingState,ShippingCountry,ShippingPostalCode FROM Account WHERE ParentId = :originalAccount.Id AND ShippingStreet<>''];
    }
}


but I got 0% coverage... what do I miss?

 

Sorry, pretty new in this! :(

Best Answer chosen by Sebastien Plante
Vikash GoyalVikash Goyal
Hi Sebastien,

You can try this :
@isTest
public class AccountShipToTest {
    @isTest
    public static void testAccountShipTo(){
        Account originalAccount = [Select Id from Account limit 1];
        ApexPages.StandardController stdcontroller = new ApexPages.StandardController(originalAccount);
        ApexPages.currentPage().getParameters().put('id', originalAccount.Id);
        AccountShipTo accountShipToCtrl = new AccountShipTo(stdcontroller);
    }
    
    @testSetup
    public static void createTestData(){
        Account acc1 = new Account(Name = 'Account1');
        insert acc1;
        Account acc2 = new Account(Name = 'Account2', ParentId = acc1.Id, ShippingStreet = 'Test Street');
        insert acc2;
    }
}

 

All Answers

Vikash GoyalVikash Goyal
Hi Sebastien,

You can try this :
@isTest
public class AccountShipToTest {
    @isTest
    public static void testAccountShipTo(){
        Account originalAccount = [Select Id from Account limit 1];
        ApexPages.StandardController stdcontroller = new ApexPages.StandardController(originalAccount);
        ApexPages.currentPage().getParameters().put('id', originalAccount.Id);
        AccountShipTo accountShipToCtrl = new AccountShipTo(stdcontroller);
    }
    
    @testSetup
    public static void createTestData(){
        Account acc1 = new Account(Name = 'Account1');
        insert acc1;
        Account acc2 = new Account(Name = 'Account2', ParentId = acc1.Id, ShippingStreet = 'Test Street');
        insert acc2;
    }
}

 
This was selected as the best answer
Sebastien PlanteSebastien Plante
Got 100% with this. Thanks a lot !

Analyzing, you did add a "fake" ApexPage.StandardController and created two dumb account, just to be able to select them so the test could do something ?

So, as I understand, I'm better create data so the test can pull them and not try to use actual data ?