You need to sign in to do that
Don't have an account?

Test Class Coverage is 71% and I want to Cover the Exception also !!!
// Apex Class
public with sharing class mysimpleclass {
Public void samplemethod1()
{
try
{
Integer a = 10;
Integer b = a + 20;
system.debug('The value of b is'+b);
}
catch (Exception e)
{
system.debug('Exception'+e);
}
}
}
//Test Class
@isTest
private class mysimpleclass_TestMethod // Name of the test class, Can be any name
{
static testmethod void mysimpleclass_TestMethod() // Name of the test method. Must be the same as the class name
{
mysimpleclass testcls = new mysimpleclass(); // Initialize variable for original class
testcls.samplemethod1();// Call Function1 of Original class
}
}
Hi All,
I want to cover the Catch block in my Test Class in my program.....I expreiment with this simple logic as how to the catch block.....
To Cover catch block, I need to create a run time error. For example, If I set Integer value to string, I got error in compile time itself in Eclipse....
Is there any logic to attain this...Plz share your thoughts...
Regards,
Muruganand_Sforce
This is something that I've hit a few times in the past. Its quite difficult to test exception handling unless you can engineer the exception being thrown using inversion of control techniques. In the simple case you have posted, you can't engineer the exception as the code will never fail.
private class TestCre {
//public static testmethod void myUnitTest() {
// TO DO: implement unit test
// here you insert what are the required fields is their in your org.
//Start of Positive Case
Public static void init(){
Account account = new Account();
//Mandatory Fields
account.Name = 'test';
account.Phone = '3399174';
//Non-Mandatory Fields
account.Start_Date__c = system.today();
account.End_Date__c = system.today();
system.debug('Before insert ******'+account);
insert account;
system.debug('After insert ******'+account);
ApexPages.CurrentPage().getParameters().put('id',account.Id);
system.debug('Account ID ******'+account.Id);
}//End of Init()
/** Positive Test **/
public static testMethod void testAMethod1(){
PVCreateScheduleController conExtension1;
init();
Test.startTest();
//PVCreateScheduleController conExtension1 = new PVCreateScheduleController(new ApexPages.StandardController(new account()));
ApexPages.StandardController con1 = new ApexPages.StandardController(new Account());
conExtension1 = new PVCreateScheduleController(con1);
Account account1 = new Account();
//Account.Nome_da_Conta_Atual__c = 'test';
account1.Name = 'test';
conExtension1.Finding();
conExtension1.first();
conExtension1.last();
conExtension1.previous();
conExtension1.next();
conExtension1.getAccountss();
Test.stopTest();
System.Debug('Account Value1 : '+account1);
System.Assert(True);
}
/** Negative Test **/
/*
public static testMethod void testAMethod2(){
PVCreateScheduleController conExtension2;
init();
Test.startTest();
ApexPages.StandardController con = new ApexPages.StandardController(new Account());
conExtension2 = new PVCreateScheduleController(con);
Account account2 = new Account();
account2.Name = 'null';
conExtension2.Finding();
conExtension2.first();
conExtension2.last();
conExtension2.previous();
conExtension2.next();
conExtension2.getAccountss();
Test.stopTest();
System.Debug('Account Value2 : '+account2);
System.Assert(True);
} */
}
Hi Bob,
This is my real Test Class which gives 85% of coverage.
In fact, It has Contoller with Pagination (With Wrapper Class),AccountDAo class,AccountService Class,VF Page.
Design of the code itself suggest you It follows DAO Class and BusinessService and Controller...All are binded by static instantation and follwed by method invokcation.
Catch Block is not Covered in all the Methods i wrote in.
How to do inversion of Control Technique for this complex design....Thanks in Advance.
Muruganand_Sforce
Inversion of control would mean setting a value into your controller so that it causes the exception to be thrown, by overwriting the real data with fake data that would change, for example.
Its a bit of a moot point as to whether this is really testing the behaviour of the controller - in order to test it, you have to write additional code that causes it to behave in a different fashion.
Regardless of all that, I need to see your actual controller to advise further, particularly the code that traps the exception.