+ 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)




     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];


            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
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;
			Opportunity_Product__c oppProd = new Opportunity_Product__c();
			// add all required field
			insert oppProd;


Let us know if this will help you