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
TeddyAbleTeddyAble 

Controller Test Case Error

Hello

 

Im having issues with writing Testcase for the controller below. What I have written s showing error message.

 

=======================================================================================

public class A1 {

public A1()
{
a=new adviser__c();
b=new contact();
c=new account();

}

Public Adviser__c a {get;set;}
public contact b {get;set;}
public account c {get;set;}


public pageReference Save()

{
insert c;
b.Account__c=c.id;
a.Name=c.name;

insert b;
a.Contact__c=b.id;
a.Name=b.FirstName+' '+b.LastName;

insert a;
b.Adviser__c=a.id;

update b;
pageReference pref=New Pagereference(URL.getSalesforceBaseUrl().toExternalForm()+'/apex/AdviserView?id='+a.id);


return pref;
}

public pageReference Cancel()
{
return page.AdviserView;
}

}

===========================================================================================

 


static testmethod void CodeCoverageTests()
{
    
        //instantiate the controller
        PageReference pageRef = page.AdviserView;
       Test.setCurrentPage(pageRef);
        Adviser__c Ad1 = new Adviser__c();
      ApexPages.StandardController sc = new ApexPages.StandardController(Ad1);
     A1 ctrl = new A1(sc);
    
      System.Assert(Ad1 != null);
    
     ApexPages.currentPage().getParameters().put('a.name', 'test');
       ApexPages.currentPage().getParameters().put('b.name', 'test');
       ApexPages.currentPage().getParameters().put('c.name', 'test');
       ApexPages.currentPage().getParameters().put('b.Phone', '1234567890');
    
     ctrl.Save();
     ctrl.insertlead();
   System.AssertEquals('Your information has been saved.  Thank you.', ctrl.StrSaveResult);

}

 

 

==========================================================================

 

Please some one help with the correct syntax for this controller Test case

 

 

Thanks you in Advance.


Best Answer chosen by Admin (Salesforce Developers) 
TeddyAbleTeddyAble

The final testcase that ends up working, after making few changes

Thanks to Chris and AnKit

Code below is the final solution just incase anybody needs to know how the end product was constructed.

 

static testmethod void CodeCoverageTests()
            {
                
            PageReference current_page = Page.AdviserView;
            Test.setCurrentPage(current_page);
                
                
                
            Adviser__c AB = new Adviser__c();
            ApexPages.StandardController sc = new ApexPages.StandardController(AB);
           // A1 ctrl = new A1(sc);
                
                               
            contact cc = new contact(adviser__c ='a0JQ0000002a7g1', FirstName='Charlie', LastName='Harper');
            insert cc;
                
                ApexPages.currentPage().getParameters().put('id',cc.id);
                
                ApexPages.StandardController ctrl = new Apexpages.Standardcontroller(cc);
               
            Adviser__c ac= new Adviser__c(name ='Charlie'+' '+'Harper', contact__c = '003Q000000U836P');
            insert ac;                
                
                ApexPages.currentPage().getParameters().put('id',ac.id);
                ApexPages.StandardController trl = new Apexpages.Standardcontroller(ac);
                                     
           //update cc;
          //  pageReference pref=New Pagereference(URL.getSalesforceBaseUrl().toExternalForm()+'/apex/AdviserView?id='+cc.id);
                
 
            
            ctrl.Save();
                trl.Save();
           
         
         
            }

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

Try the below snipped code as reference:

 

public class A1
{

public A1()
{
a=new adviser__c();
b=new contact();
c=new account();

}

Public Adviser__c a {get;set;}
public contact b {get;set;}
public account c {get;set;}


public pageReference Save()

{
insert c;
b.Account__c=c.id;
a.Name=c.name;

insert b;
a.Contact__c=b.id;
a.Name=b.FirstName+' '+b.LastName;

insert a;
b.Adviser__c=a.id;

update b;
pageReference pref=New Pagereference(URL.getSalesforceBaseUrl().toExternalForm()+'/apex/AdviserView?id='+a.id);


return pref;
}

public pageReference Cancel()
{
return page.AdviserView;
}


static testmethod void CodeCoverageTests()
{
A1 obj = new A1();

account aa = new account();
aa.name ='name';
insert aa;

contact cc = new contact();
cc.account__c =aa.id;
cc.name = aa.name
isnert cc;

Adviser__c ac= new Adviser__c();
ac.name =cc.firstname+' '+cc.lastname;
ac.contact__c = cc.id;
insert ac;
obj.Save();
obj.Cancel();

}

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

Shashikant SharmaShashikant Sharma

You can see this post to understand the structure of test class.

 

http://forceschool.blogspot.in/search/label/Test%20Apex

 

 

let me know if any questions.

 

 

TeddyAbleTeddyAble

Hello Ankit

 

There was a compile error @ Line 55 i cannot figure out why it says unexpected Token "Insert" Run the code that you have and at Line 55 after correcting

isnert cc; to  "Insert cc" it comes up with an unexpected Token.

 

Please let me know what i havent done right.

 

Compile Error: unexpected token: 'insert' at line 55 column 0


static testmethod void CodeCoverageTests()
            {
                    A1 obj = new A1();

                    account aa = new account();
                    aa.name ='name';
                    insert aa;
                   

                   
                    contact cc = new contact();
                    cc.account__c =aa.id;
                    cc.name = aa.name
                    insert cc;
                  
                  

                    Adviser__c ac= new Adviser__c();
                    ac.name =cc.firstname+' '+cc.lastname;
                    ac.contact__c = cc.id;
                    insert ac;
                
                    obj.Save();
                    obj.Cancel();

}

 

 

Thanks Ankit

 

Regards,


Shashikant SharmaShashikant Sharma

Hi

 

you are missing a semicolon in this statement

 

cc.name = aa.name
insert cc;

 

 

It should be 

 

cc.name = aa.name;
insert cc;

TeddyAbleTeddyAble

Hello Thanks for correcting that.

 

Now the test ran and only covered 33%  and came up with this Error message

 

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName]: [LastName]

Stack Trace

Class.A1.CodeCoverageTests: line 57, column 1

 

The code was modified to this

public class A1 {

   public A1(ApexPages.StandardController Controller)
    
        {
            a=new adviser__c();
            b=new contact();
            c=new account();

        }

            Public Adviser__c a {get;set;}
            public contact b {get;set;}
            public account c {get;set;}


        public pageReference Save()

            {
            insert c;
            b.Account__c=c.id;
            a.Name=c.name;

            insert b;
            a.Contact__c=b.id;
            a.Name=b.FirstName+' '+b.LastName;

            insert a;
            b.Adviser__c=a.id;

            update b;
            pageReference pref=New Pagereference(URL.getSalesforceBaseUrl().toExternalForm()+'/apex/AdviserView?id='+a.id);


            return pref;
            }

      public pageReference Cancel()
        {
        return page.AdviserView;
        }


            static testmethod void CodeCoverageTests()
            {
            Adviser__c AB = new Adviser__c();
            ApexPages.StandardController sc = new ApexPages.StandardController(AB);
            A1 ctrl = new A1(sc);

            account aa = new account();
            aa.name ='name';
            insert aa;

            contact cc = new contact();
            cc.account__c =aa.id;
         
   cc.name =aa.name;   //  It doesnt seem to like this line it says Field is Not Writable : Contact.Name is not Writable
            insert cc;

                
            Adviser__c ac= new Adviser__c();
            ac.name =cc.firstname+' '+cc.lastname;
            ac.contact__c = cc.id;
            insert ac;
            ctrl.Save();
            ctrl.Cancel();

            }

}

 

Please get back to me please,

 

 

Regards,


chris.noechris.noe

Correct, Name is not a field you can set.  Try the following:

 

contact cc = new contact();
cc.account__c =aa.id;
cc.FirstName=aa.name;
cc.LastName=aa.name;
insert cc;

 

 

TeddyAbleTeddyAble

Hello Chris.

 

I have made changes based on your response and unfortunately when the Test was run .. it ran with Test Failures 

 

Error Message:

System.DmlException: Insert failed. First exception on row 0; first error:

REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]

 

Class.A1.Save: line 20, column 1 Class.A1.CodeCoverageTests: line 64, column 1


TeddyAbleTeddyAble

The final testcase that ends up working, after making few changes

Thanks to Chris and AnKit

Code below is the final solution just incase anybody needs to know how the end product was constructed.

 

static testmethod void CodeCoverageTests()
            {
                
            PageReference current_page = Page.AdviserView;
            Test.setCurrentPage(current_page);
                
                
                
            Adviser__c AB = new Adviser__c();
            ApexPages.StandardController sc = new ApexPages.StandardController(AB);
           // A1 ctrl = new A1(sc);
                
                               
            contact cc = new contact(adviser__c ='a0JQ0000002a7g1', FirstName='Charlie', LastName='Harper');
            insert cc;
                
                ApexPages.currentPage().getParameters().put('id',cc.id);
                
                ApexPages.StandardController ctrl = new Apexpages.Standardcontroller(cc);
               
            Adviser__c ac= new Adviser__c(name ='Charlie'+' '+'Harper', contact__c = '003Q000000U836P');
            insert ac;                
                
                ApexPages.currentPage().getParameters().put('id',ac.id);
                ApexPages.StandardController trl = new Apexpages.Standardcontroller(ac);
                                     
           //update cc;
          //  pageReference pref=New Pagereference(URL.getSalesforceBaseUrl().toExternalForm()+'/apex/AdviserView?id='+cc.id);
                
 
            
            ctrl.Save();
                trl.Save();
           
         
         
            }

This was selected as the best answer