0

I am new in salesforce,

I tried to write test code for my apex code, but it does not work....

this is my apex code...

public with sharing class GetIssueRefInfo {

public Issue__C iss {get;set;}

public GetIssueRefInfo(ApexPages.StandardController controller) {
    iss = (Issue__C)controller.getRecord();

    // instantiate the list with a single attachment
    newAttachments=new List<Attachment>{new Attachment()};
    newAttachments.add(new Attachment());
    newAttachments.add(new Attachment());
    newAttachments.add(new Attachment());
}


//Product__C
public List<Product__C> ProductTemp = new List<Product__C>();  
public List<SelectOption> ProductList
{
    get
    {
        ProductTemp = [SELECT Id,Name FROM Product__C WHERE Product_show_in_issue_sheet__c = true ORDER BY Name ASC ];
        ProductList= new List<SelectOption>();
        for(Product__C t: ProductTemp )
        {
            ProductList.add(new SelectOption(t.Id, t.Name));
        }
        return ProductList;
    }
    set;
}


  public Attachment attachment {
      get {
          if (attachment == null)
            attachment = new Attachment();
          return attachment;
        }
      set;
  }


// list of new attachments to add
public List<Attachment> newAttachments {get; set;}

// the number of new attachments to add to the list when the user clicks 'Add More'
public static final Integer NUM_ATTACHMENTS_TO_ADD=1;

// Add more attachments action method
public void addMore()
{
    // append NUM_ATTACHMENTS_TO_ADD to the new attachments list
    for (Integer idx=0; idx<NUM_ATTACHMENTS_TO_ADD; idx++)
    {
        newAttachments.add(new Attachment());
    }
}      



public pagereference Save()
{    
    try{
        insert iss ;
    }catch(Exception e) {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Issue could not be saved')); 
        return null;
    }


     //multi
     List<Attachment> toInsert=new List<Attachment>();
     for (Attachment newAtt : newAttachments)
      {
          if (newAtt.Body!=null)
          {
              newAtt.ParentId=iss.Id;
              newAtt.IsPrivate = false;
              newAtt.OwnerId = UserInfo.getUserId();
              toInsert.add(newAtt);
          }
      }

      insert toInsert;
      newAttachments.clear();
      newAttachments.add(new Attachment());

     PageReference p = Page.ThankYou;
     p.setRedirect(true);
     return p;   
}
}

When I tried to write test code which start with....

@IsTest public with sharing class GetIssueRefInfoTest {
 @IsTest(SeeAllData=true) public static void testGetIssueRefInfo() {
 GetIssueRefInfo info = new GetIssueRefInfo();              
}    
}

The error message is...

Constructor not defined: [GetIssueRefInfo].()

What should I do?

2
  • 2
    Your constructor signature is different. It accepts standard controller
    – Raul
    Commented Feb 3, 2017 at 10:13
  • 1
    Try to avoid use of (SeeAllData=true) if not necessary. Insert test data from within test class.
    – Reshma
    Commented Feb 3, 2017 at 10:16

1 Answer 1

3

Because the class has a parameterized constructor you can not call the default (non-parameterized) constructor.

The code should look like:

@IsTest 
public with sharing class GetIssueRefInfoTest 
{
    @IsTest(SeeAllData=true) // not a good idea
    public static void testGetIssueRefInfo() 
    {
       Issue__C iss = // define it here with proper data and insert it
       ApexPages.StandardController sc = new ApexPages.StandardController(iss);
       GetIssueRefInfo info = new GetIssueRefInfo(sc);              
  }    
}

Refer to following post also: How to Write Test Class for Standard Controller Along With Extensions

Also, it is highly not recommended to use seeAllData=true

0

You must log in to answer this question.

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