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
Benedetto Virzi 7Benedetto Virzi 7 

code coverage for custom controller class

Hi,

I am rooky in Salesforce. I did not  understand very well how I must write a test method for code coverage, but two my  first trigger is ok. I can not imagine how to write code coverage for this my controller class
public with sharing class FatturaStampaController {
    public FatturaStampaController(ApexPages.StandardController controller) {
        righe= [SELECT Id, Descrizione_Prodotto__c, Prezzo__c, Importo_Riga__c, Quantit__c, Prodotto__c, Iva__c, Importo_Iva__c, Imponibile_Riga__c  FROM Riga_Fattura__c WHERE Fattura__c = :controller.getId()];
    }
    
public List<Riga_Fattura__c>righe{get;set;}
}
Can you help me?

Thanks a lot
Benny
KevinPKevinP
Well, first things first, you're gonna want to create a testing class like this:

@isTest
public class Trigger_Name_UnitTests {
 // your tests go here.
}

Once you've setup a test class, you'll want to add some unit tests. Unit test methods are defined like this:

private void static test_method_name(){

}

Inside your test method, you'll want to do a few things.
  1. Create some test data - Everything you need to run the test.
  2. Call the method test.startTest();
  3. Execute whatever action will exercise the code.
  4. Call the method test.stopTest();
In general you want to repeat this patter a few times. Specifically, you want to test the following things:
  1. That, given proper inputs and a proper execution context that all works as expected.
  2. That, given *im*proper inputs and a proper execution context that the *error* you *expect* to occur has, in fact, occurred.
  3. That it works as expected with 1 record.
  4. That it works as expected with 213 records.
  5. That it fails as expected with 213 records if they contain invalid data.
  6. That it works as expected with multiple profiles and users. 
these unit tests will, in general demonstrate that the code is properly bulkified, that it is properly safeguarded for security and that you've written the branching logic to properly trap errors that are likely to occur. Here's a set of unit tests that you could write:

@testMethod
private static void proves_SingleRecord_Positive_UnitTest(){

}

@testMethod
private static void proves_SingleRecord_Negative_TrapsQueryError_UnitTest(){

}

@testMethod
private static void proves_bulkRecord_Positive_UnitTest(){

}

@testMethod
private static void proves_BulkRecord_negative_UnitTest(){

}

@testMethod
private static void proves_BulkRecord_Positive_ProfileX(){

}

@testMethod
private static void proves_BulkRecord_Positive_ProfileY(){

}

@testMethod
private static void proves_BulkRecord_Positive_ProfileZ(){

}


Shabbir ShaikShabbir Shaik
Hi Benedetto Virzi 7,

Create test data for standard object in Testclass like this,

@isTest
public class FatturaStampaTest{
public static void TestMethod Fatturatest(){
Account acc=new Account();
acc.Name='test account';
insert acc;

create test data for your custom object,
Riga_Fattura__c righe=new Riga_Fattura__c();
//Give Required fields here
righe.Fattura__c=acc.Id;  // Pass standard object id
insert righe;

ApexPages.StandardController sc = new ApexPages.standardController(acc);
FatturaStampaController fattura=new FatturaStampaController(sc);
}
}

Try this......
Benedetto Virzi 7Benedetto Virzi 7
Hi,

thanks a lot KevinP  for your explanation and a thanks a Shabbir Shaik for example class. I Try it and tell the result.