Test Class
Below points we should know as a developer :-
Test class must start with @isTest annotation if class class version is more than 25
Test environment support @testVisible, @testSetUp as well
To deploy to production at least 75% code coverage is required and al test case should pass .
System.debug statement are not counted as a part of apex code coverage .
@Testvisible annotaion to make visible private methods inside test classes.
@testSetup to create test records once in a method and use in every test method in the test class .
How to write test class for both 'If 'and 'Else' statements.
public class abc
{
public integer a,b,c;
public string s;
public void maths()
{
if(s=='add')
{
c=a+b;
}
else if(s=='sub')
{
c=a-b;
}
}
}
@isTest
public class Testabc
{
static testMethod void testAddMethod()
{
abc obj = new abc();
obj.a = 5;
obj.b = 5;
obj.s = 'add';
obj.maths();
System.AssertEquals(obj.c,10);
}
static testMethod void testSubMethod()
{
abc obj = new abc();
obj.a = 5;
obj.b = 5;
obj.s = 'sub';
obj.maths();
System.AssertEquals(obj.c,0);
}
}
Below tips helps us to move towards 100 % code coverage :-
1.Tips for standardcontroller
ApexPages.standradController con=ApexPages.StandradController(Needs to pass the instance of the standard/Custom Object);
2.StandardSetcontroller
ApexPages.standradSetController con=ApexPages.StandradSetController(Needs to pass the list of the standard/Custom Object);
3.For wrapper class
ClassName.WrapperclassName wrp=new ClassName.WrapperclassName();
4.Test code catch block .
We need to create a exception in test method to cover .We can do in two different ways one suppose we want an excption on update .we can do like below .
Trick 1-
Account acc =new Account();
try{
insert/update acc;
}catch(Exception ex){}
As mandatory fields are missing it will throw exception and it will cover the catch block .
Trick 2-We need to add two line of code in class like below .
try{
DML statement;
if(Test.isRunningTest())
Integer intTest =20/0;
} catch(Exception qe){
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.error,qe.getMessage());
ApexPages.addMessage(msg);
}
--------------------------------------------------------------------------------------------------------------
Good Unit Test Example:
trigger updateParent on Opportunity (after insert) {
// Create a Map from Account Ids to Opportunities.
Map<Id, Opportunity> accountIdOpportunityMap = new Map<Id, Opportunity>();
for(Opportunity o : Trigger.new){
accountIdOpportunityMap.put(o.AccountId, o);
}
// Create a list of Accounts to Update.
List<Account> accounts = new List<Account>();
for(Account a : [SELECT Id, Most_Recently_Created_Opportunity_Name__c
FROM Account
WHERE Id IN :accountIdOpportunityMap.keySet()]){
a.Most_Recently_Created_Opportunity_Name__c = ((Opportunity) accountIdOpportunityMap.get(a.Id)).Name;
accounts.add(a);
}
update accounts;
}
There is a class named Test in apex which has some method which help us to write some useful test case Test class method
Please refer below Link for writing Test class:
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html
Below points we should know as a developer :-
Test class must start with @isTest annotation if class class version is more than 25
Test environment support @testVisible, @testSetUp as well
To deploy to production at least 75% code coverage is required and al test case should pass .
System.debug statement are not counted as a part of apex code coverage .
@Testvisible annotaion to make visible private methods inside test classes.
@testSetup to create test records once in a method and use in every test method in the test class .
How to write test class for both 'If 'and 'Else' statements.
public class abc
{
public integer a,b,c;
public string s;
public void maths()
{
if(s=='add')
{
c=a+b;
}
else if(s=='sub')
{
c=a-b;
}
}
}
@isTest
public class Testabc
{
static testMethod void testAddMethod()
{
abc obj = new abc();
obj.a = 5;
obj.b = 5;
obj.s = 'add';
obj.maths();
System.AssertEquals(obj.c,10);
}
static testMethod void testSubMethod()
{
abc obj = new abc();
obj.a = 5;
obj.b = 5;
obj.s = 'sub';
obj.maths();
System.AssertEquals(obj.c,0);
}
}
Below tips helps us to move towards 100 % code coverage :-
1.Tips for standardcontroller
ApexPages.standradController con=ApexPages.StandradController(Needs to pass the instance of the standard/Custom Object);
2.StandardSetcontroller
ApexPages.standradSetController con=ApexPages.StandradSetController(Needs to pass the list of the standard/Custom Object);
3.For wrapper class
ClassName.WrapperclassName wrp=new ClassName.WrapperclassName();
4.Test code catch block .
We need to create a exception in test method to cover .We can do in two different ways one suppose we want an excption on update .we can do like below .
Trick 1-
Account acc =new Account();
try{
insert/update acc;
}catch(Exception ex){}
As mandatory fields are missing it will throw exception and it will cover the catch block .
Trick 2-We need to add two line of code in class like below .
try{
DML statement;
if(Test.isRunningTest())
Integer intTest =20/0;
} catch(Exception qe){
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.error,qe.getMessage());
ApexPages.addMessage(msg);
}
--------------------------------------------------------------------------------------------------------------
Good Unit Test Example:
trigger updateParent on Opportunity (after insert) {
// Create a Map from Account Ids to Opportunities.
Map<Id, Opportunity> accountIdOpportunityMap = new Map<Id, Opportunity>();
for(Opportunity o : Trigger.new){
accountIdOpportunityMap.put(o.AccountId, o);
}
// Create a list of Accounts to Update.
List<Account> accounts = new List<Account>();
for(Account a : [SELECT Id, Most_Recently_Created_Opportunity_Name__c
FROM Account
WHERE Id IN :accountIdOpportunityMap.keySet()]){
a.Most_Recently_Created_Opportunity_Name__c = ((Opportunity) accountIdOpportunityMap.get(a.Id)).Name;
accounts.add(a);
}
update accounts;
}
--------------------------------------------------------------------------------------------------------
Test classes:
@isTest
public class updateParent_Test {
static testMethod void testUpdateParentAccount(){
// Set up the Account record.
Account a = new Account(Name='Test Account');
insert a;
// Verify that the initial state is as expected.
a = [SELECT Name, Most_Recently_Created_Opportunity_Name__c
FROM Account
WHERE Id = :a.Id];
System.assertEquals(null, a.Most_Recently_Created_Opportunity_Name__c);
// Set up the Opportunity record.
String opportunityName = 'My Opportunity';
Opportunity o = new Opportunity(AccountId=a.Id, Name=opportunityName,
StageName='Prospecting', CloseDate=Date.today());
// Cause the Trigger to execute.
insert o;
// Verify that the results are as expected.
a = [SELECT Name, Most_Recently_Created_Opportunity_Name__c
FROM Account
WHERE Id = :a.Id];
System.assertEquals(opportunityName, a.Most_Recently_Created_Opportunity_Name__c);
}
}
There is a class named Test in apex which has some method which help us to write some useful test case Test class method
Please refer below Link for writing Test class:
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html