+ Start a Discussion
Gaurav Agnihotri 11Gaurav Agnihotri 11 

Test Class "Constructor not defined"

Hey Gurus, 
I am trying to create a test class for a non-static method in a class. The class is as follows:
public class ServiceDeskCreateOpp
{

	public string oppRecordType { get; set; }
	public string oppName { get; set; }
	public date OppCloseDate { get; set; }
	public Id serviceDeskId;
	public Id OppAccountId;
	public List<account> SDAccount;
	//private final ServiceDesk__c serviceDesk;
	public ServiceDeskCreateOpp(ApexPages.StandardController stdController)
	{
		//this.serviceDesk = (ServiceDesk__c) stdController.getRecord();
		id serviceDeskId = apexpages.currentpage().getparameters().get('id');
		System.debug('serviceDeskId='+serviceDeskId);
		List<ServiceDesk__c> newServiceDesk = [select Client_Company_Name_Organization__c from ServiceDesk__c where Id = :serviceDeskId];
		if (newServiceDesk.size() > 0)
		{
			 OppAccountId = newServiceDesk[0].Client_Company_Name_Organization__c;
		}
		SDAccount = [select id, name from Account where id = :OppAccountId];
		if (SDAccount.size()>0 )
		{
			oppName = SDAccount[0].Name;
		}
		System.debug('OppAccountId='+OppAccountId);
		System.debug('newServiceDesk='+newServiceDesk);
		System.debug('oppName='+oppName);
	}

	Public PageReference OppCreate()
	{

		Opportunity op = new opportunity();


		op.Name = oppName;
		System.debug(oppName);
		op.z_Date_Last_non_USD_Hist_Rate_Check__c = System.today();
		op.StageName = 'Suspect';
		op.CloseDate = OppCloseDate;
		System.debug(OppCloseDate);
		System.debug(SDAccount[0].Id);
		op.AccountId = SDAccount[0].Id;
		op.recordTypeId = [SELECT Id, Name, SobjectType FROM RecordType WHERE SobjectType = 'Opportunity' AND Name = :oppRecordType].Id;
		op.Service_Desk_Requests__c = serviceDeskId;
		insert op;

		System.debug(System.Label.Opportunity_URL);
		PageReference pageRef = new PageReference(System.Label.Opportunity_URL + '?id=' + Op.id + '&sfdc.override=1');

		System.debug(pageRef);

		return pageRef;
	}

	public List<SelectOption> getSelectOpptyRecordType()
	{
		List<SelectOption> options = new List<SelectOption> ();

		options.add(new SelectOption('Client Renewal', 'Client Renewal'));
		options.add(new SelectOption('Default', 'Default'));
		options.add(new SelectOption('Partner Services', 'Partner Services'));
		options.add(new SelectOption('SMB', 'SMB'));
		options.add(new SelectOption('SMB Client Renewal', 'SMB Client Renewal'));
		options.add(new SelectOption('Sub-Contracted Services', 'Sub-Contracted Services'));
		options.add(new SelectOption('Technical Project', 'Technical Project'));

		return options;
	}
}

 and test class:
@isTest
private class ServiceDeskCreateOpp_Test  
{

@TestSetup
	static void setup()
	{

        Account testAccount = TestDataFactory_Account.Execute();
		insert testAccount;

		ServiceDesk__c testServiceDesk = TestDataFactory_ServiceDesk.Execute();
		testServiceDesk.Client_Company_Name_Organization__c= testAccount.id;
		insert testServiceDesk;
		
		Opportunity testOpporutnity = TestDataFactory_Opportunity.Execute();
		testOpporutnity.AccountId =testAccount.Id;
		insert testOpporutnity;

		system.debug('testing class:Account' + testAccount);
		system.debug('testing class:Service Desk' + testServiceDesk);
		system.debug('testing class:Opportunity' + testOpporutnity);

	}

	static testMethod void testServiceDeskCreateOppy()
	{
	  Account newAcc = [Select Id, Name from Account];
	  ServiceDesk__c newSD = [select id from ServiceDesk__c];
	  Opportunity newOpp = [select id,Name from Opportunity];

	  	PageReference pageRef = Page.ServiceDeskCreateOpp;
		Test.setCurrentPage(pageRef);
		ApexPages.currentPage().getParameters().put('id', newSD.id);

		ServiceDeskCreateOpp newSDOppty = new ServiceDeskCreateOpp();

        newSDOppty.OppCreate();

	  //ServiceDeskCreateOpp newOppty = new ServiceDeskCreateOpp();
	  //newOppty.OppCreate();
	  //OppCreate.OppCreate(newSD.id);
//	  ServiceDeskCreateOpp


	}
}

I am getting an error message that  "Error    1    Constructor not defined: [ServiceDeskCreateOpp].<Constructor>()    C:\Users\gagnihotri\Documents\The Welkin Suite\Projects\FullSand-New\FullSand-New\src\classes\ServiceDeskCreateOpp_Test.cls    36    1  "

Not sure what am I doing wrong
  
 
Best Answer chosen by Gaurav Agnihotri 11
Steven NsubugaSteven Nsubuga
You used the wrong constructor.  Try this
@isTest
private class ServiceDeskCreateOpp_Test  
{

@TestSetup
	static void setup()
	{

        Account testAccount = TestDataFactory_Account.Execute();
		insert testAccount;

		ServiceDesk__c testServiceDesk = TestDataFactory_ServiceDesk.Execute();
		testServiceDesk.Client_Company_Name_Organization__c= testAccount.id;
		insert testServiceDesk;
		
		Opportunity testOpporutnity = TestDataFactory_Opportunity.Execute();
		testOpporutnity.AccountId =testAccount.Id;
		insert testOpporutnity;

		system.debug('testing class:Account' + testAccount);
		system.debug('testing class:Service Desk' + testServiceDesk);
		system.debug('testing class:Opportunity' + testOpporutnity);

	}

	static testMethod void testServiceDeskCreateOppy()
	{
	  Account newAcc = [Select Id, Name from Account];
	  ServiceDesk__c newSD = [select id from ServiceDesk__c];
	  Opportunity newOpp = [select id,Name from Opportunity];

	  	PageReference pageRef = Page.ServiceDeskCreateOpp;
		Test.setCurrentPage(pageRef);
		ApexPages.currentPage().getParameters().put('id', newSD.id);

        ApexPages.StandardController sc = new ApexPages.StandardController(newSD);
		ServiceDeskCreateOpp newSDOppty = new ServiceDeskCreateOpp(sc);

        newSDOppty.OppCreate();

	  //ServiceDeskCreateOpp newOppty = new ServiceDeskCreateOpp();
	  //newOppty.OppCreate();
	  //OppCreate.OppCreate(newSD.id);
//	  ServiceDeskCreateOpp


	}
}


 

All Answers

Steven NsubugaSteven Nsubuga
You used the wrong constructor.  Try this
@isTest
private class ServiceDeskCreateOpp_Test  
{

@TestSetup
	static void setup()
	{

        Account testAccount = TestDataFactory_Account.Execute();
		insert testAccount;

		ServiceDesk__c testServiceDesk = TestDataFactory_ServiceDesk.Execute();
		testServiceDesk.Client_Company_Name_Organization__c= testAccount.id;
		insert testServiceDesk;
		
		Opportunity testOpporutnity = TestDataFactory_Opportunity.Execute();
		testOpporutnity.AccountId =testAccount.Id;
		insert testOpporutnity;

		system.debug('testing class:Account' + testAccount);
		system.debug('testing class:Service Desk' + testServiceDesk);
		system.debug('testing class:Opportunity' + testOpporutnity);

	}

	static testMethod void testServiceDeskCreateOppy()
	{
	  Account newAcc = [Select Id, Name from Account];
	  ServiceDesk__c newSD = [select id from ServiceDesk__c];
	  Opportunity newOpp = [select id,Name from Opportunity];

	  	PageReference pageRef = Page.ServiceDeskCreateOpp;
		Test.setCurrentPage(pageRef);
		ApexPages.currentPage().getParameters().put('id', newSD.id);

        ApexPages.StandardController sc = new ApexPages.StandardController(newSD);
		ServiceDeskCreateOpp newSDOppty = new ServiceDeskCreateOpp(sc);

        newSDOppty.OppCreate();

	  //ServiceDeskCreateOpp newOppty = new ServiceDeskCreateOpp();
	  //newOppty.OppCreate();
	  //OppCreate.OppCreate(newSD.id);
//	  ServiceDeskCreateOpp


	}
}


 
This was selected as the best answer
Gaurav Agnihotri 11Gaurav Agnihotri 11
Could you please explain why it worked? i am a bit rusty in apex
 
Steven NsubugaSteven Nsubuga
Firstly because the constructor in the class had an argument, whereas the one you used in the test did not have any argument.
Then the nature of the argument in the constructor is a StandardController, and it is instantiated the way I did in the test.