0

I am having 2 issues with my Test Class. Coverage of 3 lines which I have comented on in the code below. And the insert is failing saying that the account a.id is null. The controller is used in a Visualforce page.
Do I need to create another test Account in the Save?

public with sharing class MerchandisingController {

public Merchandising__c merch{get;set;}
public string message {get;set;} <u><b> ----no coverage</b></u>

public MerchandisingController (ApexPages.StandardController controller) 
{
    merch = new Merchandising__c();
    merch.Account__c= ApexPages.CurrentPage().getparameters().get('someId');
}

  public void save() {
  insert merch; 
  message = 'Record Created Successfully.Please add pictures of the disply to the Account. Thank you!'; ----no coverage
  ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,message )); ----no coverage
}  
}

Test Class

@isTest
private class TestMerchandiseController {
static testmethod void testM(){

Test.StartTest(); 

    Account a = new Account();
    a.Name  = 'Test Account';
    insert a;

    ApexPages.StandardController sc = new ApexPages.StandardController(a);
    MerchandisingController testMerch = new MerchandisingController (sc);

   PageReference pageRef = Page.Merchandising_V2; 
   pageRef.getParameters().put('id',a.Id);
   Test.setCurrentPage(pageRef);

   testMerch.save();
   Merchandising__c m = new Merchandising__c();
   m.Account__c= a.Id;
   m.Merchandise__c= 'Trex Rod Rail Display';
   m.Merchandise_Category__c= 'Full Size(6ft)';
   m.Merchandise_SubCategory__c = 'Black/Black';
   insert m ; 

   System.assert(ApexPages.hasMessages(ApexPages.SEVERITY.CONFIRM));
   Test.StopTest();

}   
}
1
  • I would recomend to create a variable call error string and do assertion on that not on the severity. You are putting id as parameter and getting someid that is why it is null Commented May 31, 2018 at 14:02

2 Answers 2

2

You main controller is already instantiating Mechandising__C , you dont have to do that in you test class. Your test class would be clean.

@isTest
private class TestMerchandiseController {
    static testmethod void testM(){



        Account a = new Account();
        a.Name  = 'Test Account';
        insert a;

        ApexPages.StandardController sc = new ApexPages.StandardController(a);
        MerchandisingController testMerch = new MerchandisingController (sc);

        PageReference pageRef = Page.Merchandising_V2; 
        pageRef.getParameters().put('id',a.Id);
        Test.setCurrentPage(pageRef);


        testMerch.merch.Merchandise__c= 'Trex Rod Rail Display';
        testMerch.merch.Merchandise_Category__c= 'Full Size(6ft)';
        testMerch.merch.Merchandise_SubCategory__c = 'Black/Black';

        Test.StartTest(); 
        testMerch.save(); //Doing DML
        Test.StopTest();


       System.assert(testMerch.merch.ID!=null,'Mechandise was not inserted');
       System.assert(testMerch.merch.Account__c!=a.Id,'Mechandise account does not match with what provided as ID to the controller');


    }   
}

Unit tests are written to test the behaviour, Not the error messages.

That being said, your line insert merch; is giving an error, because you might not have provided a mandatory field or there is an Apex trigger validation or validation which is breaking. Check the test execution debug logs. They will indicate the proper error.

1

Some reordering need to be done

@isTest
private class TestMerchandiseController {
static testmethod void testM(){

Test.StartTest(); 

    Account a = new Account();
    a.Name  = 'Test Account';
   insert a;
   PageReference pageRef = Page.Merchandising_V2; 
   pageRef.getParameters().put('someId',a.Id);
   Test.setCurrentPage(pageRef);
   ApexPages.StandardController sc = new ApexPages.StandardController(a);
   MerchandisingController testMerch = new MerchandisingController (sc);
   testMerch.save();
   System.assert(ApexPages.hasMessages(ApexPages.SEVERITY.CONFIRM));
   Test.StopTest();

}   
}

public with sharing class MerchandisingController {

public Merchandising__c merch{get;set;}
public string message {get;set;} 

public MerchandisingController (ApexPages.StandardController controller) 
{
    merch = new Merchandising__c();
    merch.Account__c= ApexPages.CurrentPage().getparameters().get('someId');
}

  public void save() {
  insert merch; 
  message = 'Record Created Successfully.Please add pictures of the disply to the Account. Thank you!'; 
  ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,message )); 
}  
}
4
  • Pranay, the error is missing a required field(Account). That is why I was wondering why the insert was failing since I was already creating an Account in the Test Class.
    – Phuc
    Commented May 31, 2018 at 14:22
  • Also, I tried t put a debug message in the Save method and it does not show in the Developer Console while other debug outside the Save does.
    – Phuc
    Commented May 31, 2018 at 14:23
  • The "System.assert(testMerch.merch.Account__c!=a.Id,'Mechandise account does not match with what provided as ID to the controller');" casued the test to fail but once removed Test passed and 100% coverage.
    – Phuc
    Commented May 31, 2018 at 16:13
  • Test Class by Avijet also gave 100% coverage.
    – Phuc
    Commented May 31, 2018 at 16:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .