+ Start a Discussion
Gdev20000Gdev20000 

Help with test class 0% covered

It's almost my first test class, please anyone can help me.

I get 0% covered by test class for my custom controller:

My class:

 

public with sharing class MyCustomLookupController {

   public Influencer__c Influencer { get; set; }

   public Id accountplanid { get; set; }
      
   public MyCustomLookupController () {
       accountplanid = System.currentPageReference().getParameters().get('id');
       Influencer = new Influencer__c(Account_Plan__c = accountplanid );
   }
    

 
 

    public PageReference save() {
      
        Contact c = [SELECT Job_title__c,name FROM Contact WHERE Id = :Influencer.Contact__c];
        
        Influencer.Title__c = c.Job_title__c;
        influencer.name=c.name;
        insert Influencer;
        PageReference acctPage = new PageReference('/' + Influencer.id);
        acctPage.setRedirect(true);
        return acctPage;
    }
    
    
       public PageReference cancel() {
      
        PageReference aPage = new PageReference('/' + accountplanid);
        aPage.setRedirect(true);
        return aPage;
    }
       
 
}

 

My test class:

@isTest 
private class MyCustomLookupControllerTestClass {
    static testMethod void validate() {


    PageReference pageRef = Page.MyCustomLookup; 
    Test.setCurrentPage(pageRef);
    
    // Create a new account object.
    Account testAccount = new Account(Name = 'TestAccount');
    testAccount.Red_Account_End_Date__c=date.today();
    testAccount.Red_Account_Reason__c='reason';
    testAccount.Red_Account__c= true;
    insert testAccount;
  
    
    //CREATE TEST ACCOUNT PLAN
     Account_Plan__c accountplan=new Account_Plan__c ();
     accountplan.account__c=testaccount.id;
     insert accountplan;
    
    //CREATE TEST CONTACT
     Contact contact = new contact(lastname='lname');
     contact.Job_title__c='salesrep';
     insert contact; 
     Contact contact2 = new contact(lastname='lname');
     insert contact2;  
    
    //CREATE TEST INFLUENCER
       Influencer__C influencertest = new Influencer__c();
       influencertest.Account_Plan__c= accountplan.id;
       Influencertest.Title__c = contact.Job_title__c;
        influencertest.contact__c=contact2.id;
       insert influencertest;     
         
        
   }

     }

 

 

Sorry, I have to learn a lots of things.

Thank you,Br.

jhuggerjhugger

You need to initiate the controller.

 

Try This:

 

@isTest 
private class MyCustomLookupControllerTestClass {
    static testMethod void validate() {
    
    // Create a new account object.
    Account testAccount = new Account(Name = 'TestAccount');
    testAccount.Red_Account_End_Date__c=date.today();
    testAccount.Red_Account_Reason__c='reason';
    testAccount.Red_Account__c= true;
    insert testAccount;
  
    
    //CREATE TEST ACCOUNT PLAN
    Account_Plan__c accountplan=new Account_Plan__c ();
    accountplan.account__c=testaccount.id;
    insert accountplan;
    
    //CREATE TEST CONTACT
    Contact contact = new contact(lastname='lname');
    contact.Job_title__c='salesrep';
    insert contact; 
    Contact contact2 = new contact(lastname='lname');
    insert contact2;  
    
    //CREATE TEST INFLUENCER
    Influencer__C influencertest = new Influencer__c();
    influencertest.Account_Plan__c= accountplan.id;
    Influencertest.Title__c = contact.Job_title__c;
    influencertest.contact__c=contact2.id;
    insert influencertest;    

	Test.startTest();
		PageReference pageRef = Page.MyCustomLookup;
		Test.setCurrentPage(pageRef);

		ApexPages.currentPage().getParameters().put('id', accountplan.Id);
		MyCustomLookupController controller = new MyCustomLookupController();  	 	
controller.save(); Test.stopTest(); } }

 

Gdev20000Gdev20000

Thank you so much, you are very kind.

Now i'm getting this error:

System.QueryException: List has no rows for assignment to SObject

 

please can you have a look.

Thanks again.

Avidev9Avidev9

I am seeing a problem with your controller class

(You never queried for Contact__c and you were using the same in save method)

public MyCustomLookupController () {
       accountplanid = System.currentPageReference().getParameters().get('id');
       //Influencer = new Influencer__c(Account_Plan__c = accountplanid );// You never queried for Contact__c and you were using the same in save method
Influencer = [SELECT Id,Contact__c FROM Influencer__c WHERE Account_Plan__c =:accountplanid LIMIT 1]; }
Gdev20000Gdev20000

No,it isn't a problem.

I'm creating a new influencer's record in the controller getting the id from the url.

 

I query for the contact with:

Contact c = [SELECT Job_title__c,name FROM Contact WHERE Id = :Influencer.Contact__c];

 

 

Thanks for your reply.

 

jhuggerjhugger

Your select is not returning any results.

 

Why:

 

When you initiate the controller in the test it creating a new influncer and not utilizing the one in your test code. In the one you are creating in your conroller it does not have a contact associated to it.

 

The actual code running in your test is this:

 

SELECT Job_title__c,name FROM Contact WHERE Id = ''

 

Try this before controller.save();  : 

 

controller.Influencer = influencertest;
controller.save();

 

This should override the new influencer with the one your test created and the save correctly.

Gdev20000Gdev20000

Thanks jhugger i have updated my code but now i get this error:

System.DmlException: Insert failed. First exception on row 0 with id a4AM0000000DSU1MAO; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

 the line interessed is that with controller.save();