function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mario CMario C 

Test class for simple component controller class

Hi all,

I need to write a test class for a simple LEX component which I built, but I am struggling to understand how to write it. Can you please help me?
 
public class MyOLIController {
    
    @AuraEnabled
    public static List<OpportunityLineItem> getProduct(List<Id> opportunityIds){
        Case cs = [Select Enquiry__c from Case where Id=:opportunityIds];
        List<OpportunityLineItem> productList = [SELECT Id, Property__c, OpportunityId, Start_Date_Time__c,End_Date_Time__c,Status__c  
                                                 FROM OpportunityLineItem 
                                                 WHERE OpportunityId=:cs.Enquiry__c
                                                 AND (Status__c='confirmed' OR Status__c='pending' 
                                                                             OR Status__c='booked' 
                                                                             OR Status__c='stayed')];
        return productList;
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm

You write a test class for this the same way that you would any other:
- Set up some data for the controller to access (in this case it looks like Case ,Opportunity and OpportunityLineItem)
- Instantiate the controller -
- Execute a method/methods
- Verify the behaviour with asserts.

Smaple test class for you
@isTest
private class MyOLIControllerTest {

    static TestMethod void myUnitTest()
	{
		Account acc = new Account();
			acc.Name ='Test';
			// Add all required field
		insert acc;
		
		Contact cont = new Contact();
			cont.LastName ='Test';
			cont.accountId = acc.id;
		insert cont;
		
		Opportunity testOpportunity = new Opportunity(
	            Name = 'Test Opportunity Triggers',
	            CloseDate = '2016-01-01',        
	            StageName = 'Sourcing Demand',
	            accountId = acc.id
        );
		insert testOpportunity;
		
		List<ID> lstOppID = new List<Id>();
		lstOppID.add(testOpportunity.id);
			
		OpportunityLineItem oli = new OpportunityLineItem();
		// Add all required field here
		insert 	oli;
		
		Case caseObj = new Case();
		// all requried field here
		insert caseObj;
		
		Test.startTest();
				
			MyOLIController.getProduct(lstOppID);
			
		Test.stopTest();
    }
}



NOTE:- Look like some issue in code as well , How opportunityIds can be equals to Case ID ?
            Case cs = [Select Enquiry__c from Case where Id=:opportunityIds];

Let us know if this will help you
 
Raj VakatiRaj Vakati
Try this code
 
@isTest
private class MyOLIControllerTest {

    static TestMethod void myUnitTest()
	{
		Account acc = new Account();
			acc.Name ='Test';
			// Add all required field
		insert acc;
		
		Contact cont = new Contact();
			cont.LastName ='Test';
			cont.accountId = acc.id;
		insert cont;
		
		Opportunity testOpportunity = new Opportunity(
	            Name = 'Test Opportunity Triggers',
	            CloseDate = System.today()+10,
                  Amount=123,				
	            StageName = 'Closed Won',
	            accountId = acc.id
        );
		insert testOpportunity;
		
		List<ID> lstOppID = new List<Id>();
		lstOppID.add(testOpportunity.id);
		 Pricebook2 pb22 = new Pricebook2(Name='testNonDIE');
 insert pb22;

Product2 pro2 = new Product2( Name='BXCDXXX', isActive=true);
insert pro2;

PricebookEntry pbe2 =new PricebookEntry(unitprice=1,Product2Id=pro2.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                         isActive=true,UseStandardPrice = false);
 insert pbe2;

 OpportunityLineItem OPplineitem2 = new OpportunityLineItem (Quantity=25, OpportunityId=oppr.Id,UnitPrice=1,PriceBookEntryId=pbe2.Id,
                                   Status__c='confirmed');
 insert OPplineitem2;
 
  OpportunityLineItem OPplineitem21 = new OpportunityLineItem (Quantity=25, OpportunityId=oppr.Id,UnitPrice=1,PriceBookEntryId=pbe2.Id,
                                   Status__c='booked');
 insert OPplineitem21;
 
   Case c = new Case(Status='New',
                        
                        Subject='Feedback - Something' , Enquiry__c=testOpportunity.id);
    insert c;
  
		
		Test.startTest();
				
			MyOLIController.getProduct(lstOppID);
			
		Test.stopTest();
    }
}

 
Mario CMario C
I managed to complete the test class but I still get the following error: System.QueryException: List has no rows for assignment to SObject

Here is my code:
@isTest
private class MyOlIControllerTest {

    static TestMethod void myUnitTest()
	{
		Account acc = new Account();
			acc.RecordTypeId = '01220000000KFUXAA4';	
        	acc.Name ='TestAcc';
        	insert acc;
        

		
		Contact cont = new Contact();
			cont.LastName ='Test';
			cont.accountId = acc.id;
        	cont.Newsletter__c = 'false';
        	cont.Email = 'test@test.ukic';
        	cont.LeadSource = 'Email';
        	cont.Source_Type__c = 'Salesforce';
        	cont.Country_USE_ME__c = 'United Kingdom';
        	cont.phone = '+44783335646477';
        	insert cont;
		
		Opportunity testOpportunity = new Opportunity();
	            testOpportunity.Name = 'Test Opportunity Triggers';
	            testOpportunity.CloseDate =  date.today();
	            testOpportunity.StageName = 'Closed Won';
	            testOpportunity.accountId = acc.id;
        		insert testOpportunity;
        
        System.debug('Here is my opportunity ' + testOpportunity);
		
		List<ID> lstOppID = new List<Id>();
			lstOppID.add(testOpportunity.Id);
        System.debug('Here is my lstOppID ' + lstOppID);
        	
        Pricebook2 pb22 = new Pricebook2(
            Name='testNonDIE');
 			insert pb22; 
         System.debug('Here is my pb22 ' + pb22);
        
        Product2 pro2 = new Product2(
            Name='BXCDXXX', 
            isActive=true);
        	insert pro2; 
        System.debug('Here is my pro2 ' + pro2);
        
        PricebookEntry pbe2 =new PricebookEntry(
            unitprice=1,
            Product2Id=pro2.Id,
            Pricebook2Id=Test.getStandardPricebookId(),
            isActive=true,
            UseStandardPrice = false);
 			insert pbe2; 
        System.debug('Here is my pbe2 ' + pbe2);
        
        OpportunityLineItem OPplineitem2 = new OpportunityLineItem (
             Quantity=1, 
             OpportunityId=testOpportunity.Id, 
             Start_date_Time__c=date.today(),
        	 End_date_Time__c=date.today(),
             UnitPrice=1,
             PriceBookEntryId=pbe2.Id,
             Property__c=acc.Id,
             Status__c='confirmed');
 			 insert OPplineitem2; 
        System.debug('Here is my OPplineitem2 ' + OPplineitem2);
 
    OpportunityLineItem OPplineitem21 = new OpportunityLineItem (
      Quantity=1, 
      OpportunityId=testOpportunity.Id,
      UnitPrice=1,
      PriceBookEntryId=pbe2.Id,
      Property__c=acc.Id,
      Start_date_Time__c=date.today(),
	  End_date_Time__c=date.today(),
      Status__c='booked');
      insert OPplineitem21; 
        System.debug('Here is my OPplineitem21 ' + OPplineitem21);
			
	
		Case caseObj = new Case();
         caseObj.Case_Reason__c = 'Couple';
            caseObj.Status = 'New';
		insert caseObj;
         System.debug('Here is my caseObj ' + caseObj);
		
		Test.startTest();
				
			MyOLIController.getProduct(lstOppID);
			
		Test.stopTest();
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8

NOTE:- Look like some issue in code as well , How opportunityIds can be equals to Case ID ?
            Case cs = [Select Enquiry__c from Case where Id=:opportunityIds];

Please change the query filter