+ Start a Discussion
Gaurav AgnihotriGaurav Agnihotri 

Test Class for wrapper. Error : Constructor not defined

I am unable to create a test class for wrapper.
Class:
global class ProductWrapper {
public Boolean isSelected {get;set;}
public PricebookEntry PBE{get;set;} 
    //creating a constructor
    public ProductWrapper(PricebookEntry PBE,Boolean isSelected)
    {
        this.PBE= PBE;
        this.isSelected= isSelected;
    }
    
}
Test class:
@istest
public class TestProductWrapper 
{
    static testMethod void productWrapper1() 
    {
        /*********************
        //insert Quote Line Item
        *********************/
        Product2 prod = new Product2(
			 Name = 'ENGEH16-2P',
			 ProductCode = 'ENGEH16-2P',
			 Item__c = 'ENGEH16-2P',
             Discount_Code__c='A',
			 isActive = true,
            Dealer_Price__c=100 
		);
		insert prod;
        system.debug('Product2 Id='+prod.id);
        /************************
         * Get standard pricebook
         * ************************/
         Id priceBookId2 = Test.getStandardPricebookId();
        /******************************
        //create price book entry
        ***************************/
        PricebookEntry PBE= new PricebookEntry();
        //PBE.Name='ENGEH16-2P';
        //PBE.Pricebook2Id=pb.Id;
        PBE.Pricebook2Id=priceBookId2;
        PBE.Product2Id=prod.id;
        PBE.UnitPrice=100;
        PBE.UseStandardPrice=false;
        PBE.isActive = true;
        Insert PBE; 
        boolean isSelected =True;
        /*******************************
         * Creating a constructor
         * *****************************/
		ProductWrapper TestPW = new ProductWrapper();
        TestPW.ProductWrapper(PBE, isSelected);
        
    }
}
I am getting an error message on second last line
ProductWrapper TestPW = new ProductWrapper();
Constructor not defined: [ProductWrapper].<Constructor>()
Any suggestions?



 
Best Answer chosen by Gaurav Agnihotri
Amit Chaudhary 8Amit Chaudhary 8
Please try below code.
@istest
public class TestProductWrapper 
{
    static testMethod void productWrapper1() 
    {
        /*********************
        //insert Quote Line Item
        *********************/
        Product2 prod = new Product2(
			 Name = 'ENGEH16-2P',
			 ProductCode = 'ENGEH16-2P',
			 Item__c = 'ENGEH16-2P',
             Discount_Code__c='A',
			 isActive = true,
            Dealer_Price__c=100 
		);
		insert prod;
        system.debug('Product2 Id='+prod.id);
        /************************
         * Get standard pricebook
         * ************************/
         Id priceBookId2 = Test.getStandardPricebookId();
        /******************************
        //create price book entry
        ***************************/
        PricebookEntry PBE= new PricebookEntry();
        //PBE.Name='ENGEH16-2P';
        //PBE.Pricebook2Id=pb.Id;
        PBE.Pricebook2Id=priceBookId2;
        PBE.Product2Id=prod.id;
        PBE.UnitPrice=100;
        PBE.UseStandardPrice=false;
        PBE.isActive = true;
        Insert PBE; 
        boolean isSelected =True;
        /*******************************
         * Creating a constructor
         * *****************************/
		ProductWrapper TestPW = new ProductWrapper(PBE, isSelected);

    }
}

NOTE :- You are using parametrize construtor. 

ProductWrapper TestPW = new ProductWrapper();
TestPW.ProductWrapper(PBE, isSelected);

You need to call like below code
ProductWrapper TestPW = new ProductWrapper(PBE, isSelected);

Please let us know if this will help you

Thanks
Amit Chaudhary

All Answers

BharathimohanBharathimohan
Hi Gaurav,

Try this line instead by removing line 39 and 40.


ProductWrapper TestPW = new ProductWrapper(PBE,isSelected);

Reason is in line # 5, the constructor was declared with arguments. Hence this fix should work for you or you need modify the constructor in apex class itself without arguments

Regards,
Bharathimohan Ramaurthy
Salesforce For All (http://salesforceforall.blogspot.com/)
 
Amit Chaudhary 8Amit Chaudhary 8
Please try below code.
@istest
public class TestProductWrapper 
{
    static testMethod void productWrapper1() 
    {
        /*********************
        //insert Quote Line Item
        *********************/
        Product2 prod = new Product2(
			 Name = 'ENGEH16-2P',
			 ProductCode = 'ENGEH16-2P',
			 Item__c = 'ENGEH16-2P',
             Discount_Code__c='A',
			 isActive = true,
            Dealer_Price__c=100 
		);
		insert prod;
        system.debug('Product2 Id='+prod.id);
        /************************
         * Get standard pricebook
         * ************************/
         Id priceBookId2 = Test.getStandardPricebookId();
        /******************************
        //create price book entry
        ***************************/
        PricebookEntry PBE= new PricebookEntry();
        //PBE.Name='ENGEH16-2P';
        //PBE.Pricebook2Id=pb.Id;
        PBE.Pricebook2Id=priceBookId2;
        PBE.Product2Id=prod.id;
        PBE.UnitPrice=100;
        PBE.UseStandardPrice=false;
        PBE.isActive = true;
        Insert PBE; 
        boolean isSelected =True;
        /*******************************
         * Creating a constructor
         * *****************************/
		ProductWrapper TestPW = new ProductWrapper(PBE, isSelected);

    }
}

NOTE :- You are using parametrize construtor. 

ProductWrapper TestPW = new ProductWrapper();
TestPW.ProductWrapper(PBE, isSelected);

You need to call like below code
ProductWrapper TestPW = new ProductWrapper(PBE, isSelected);

Please let us know if this will help you

Thanks
Amit Chaudhary
This was selected as the best answer
Gaurav AgnihotriGaurav Agnihotri
Thanks Bharathimohan and Amit.
The test script seems to work. I also learned something new. Thanks again for explaining it to me.

Best Regards, 
Gaurav