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
dhruv aroradhruv arora 

Please help me in writting test class for this trigger

trigger Primaryopp on Opportunity_Product__c (after insert,after update)
 {

     set<id> Oppid = new set<id>();

     for(Opportunity_Product__c Oppproduct : trigger.new)

{

            Oppid.add(Oppproduct.abcd__lookup__c);

      }

     list<opportunity> Opplst =  [SELECT Name,id,(select abcd__Primary__c from Opportunity_Products__r where abcd__Primary__c=True) FROM Opportunity WHERE id in:Oppid];

         system.debug('##lst'+Opplst[0].Opportunity_Products__r.size());

            for(opportunity opp: Opplst)

{

                if(opp.Opportunity_Products__r.size()>1 )

{

                trigger.new[0].addError('you can not check multiple primary opportunity');

                }
               
            }
}
 
Best Answer chosen by dhruv arora
Amit Chaudhary 8Amit Chaudhary 8

You write a test class for this the same way that you would any other:

- Set up some data for the for Opportunity_Product__c object
- Verify the behaviour with asserts.

The fact that the class is used as a controller is fairly immaterial.

Please try below test class as sample one
@isTest 
public class PrimaryoppTest 
{
    static testMethod void testMethod1() 
	{
		Account acc = new Account();
		acc.Name ='Test';
		insert acc;
		
		Opportunity oppt = new Opportunity( Name ='New mAWS Deal',
                            AccountID = acc.ID,
                            StageName = 'Negotiations',
                            Amount = 3000,
                            CloseDate = System.today()
							);

		insert oppt;
	
		Test.startTest();
		
			Opportunity_Product__c oppProd = new Opportunity_Product__c();
			// add all required field
			insert oppProd;

		Test.stopTest();
		
	}
}

Let us know if this will help you