+ Start a Discussion
Anju Alexander 5Anju Alexander 5 

Test class for the controller

My test class:
@isTest
public class pdfTest{
static testMethod void test()
{
opp opp1=new opp();
boolean sel=true;

Account a=new Account(name='Account1');
System.assertEquals(sel,true);

opp.AccountWrapper aw=new opp.AccountWrapper(a,sel);

if(sel==true)
{
    Opportunity op=new Opportunity();
    op.name='pdfTest';
    op.closedate=date.today();
    op.stagename='Prospecting';
    insert op;
}

PageReference pg= Page.Practice;
Test.setCurrentPage(pg);
String nextPage = opp1.createOpp().getUrl();
System.assertEquals('https://ap1.salesforce.com/home/home.jsp', nextPage);
}

static testMethod void test1()
{
opp opp1=new opp();
boolean sel=false;

Account a=new Account(name='Account2');
System.assertEquals(sel,false);

opp.AccountWrapper aw=new opp.AccountWrapper(a,sel);

if(sel==false)
{
    Opportunity op=new Opportunity();
    op.name='account2';
    op.closedate=date.today();
    op.stagename='Prospecting';
    insert op;
}


}

}

I am writing the test class for the controller first time. I tried it in this way. After creating opportunity it should redirect to the home page.
Pramod_SFDCPramod_SFDC
Hi Anju,

So, I believe you have wriiten a test class here. Did you run this test class and check the code coverage for the particular controller class ? . You can do this from the Develore console or Apex test Execute tab. Below link provides you more information on how to run test classes.

>>https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_unit_tests_running.htm

However, if you could let me know the issue you are facing, I can help you accordingly.

Regards
Pramod
kaustav goswamikaustav goswami
Please note a few points:

The System.assertEquals for the url might cause problem if you write the code in a sandbox and then try to migrate it to production. Between sandbox and production environment the server instance will change and that will result in a faliure of the assert statement. Thus your test class will fail.

Also try to use the Test,startTest() and Test,stopTest() methods. This takes care of resetting the applicable governor limits. This proves to be of a lot of help when there are lots of data trnsaction going on.

Also if there is functionality that might vary based on the type of user then you need to create a user record and then use the System.runAs(user) method to test the user specific functionality.

To have good understanding of how to write good test classes please refer to the apex developer's guide.

Let me know if this helps.

Thanks,
Kaustav