+ Start a Discussion
HelloSanHelloSan 

I need a test class for the below trigger for code coverage,both account and opportunity have record types

trigger newTrg on Opportunity (before insert) {
for(opportunity opp:trigger.new)
{
Set<Id> s1 = new Set<Id>();
if(opp.accountId != null){
s1.add(opp.accountid);
}
if(s1.size()>0)
{
Map<Id,Id> m1 = new Map<id,Id>();
for(Account acc : [Select id, ownerId from Account where id in:s1])
{
m1.put(acc.id,acc.ownerId);
}
for(Opportunity opp1:trigger.new){
//Custom Field
opp1.TestUser__c = m1.get(opp.accountId);
}
}
}
}
Best Answer chosen by HelloSan
Swayam  AroraSwayam Arora
Hi,
Below is the code that you are looking for:-
@isTest
public class MyTest {
	public testMethod void verifyAccountDescriptionsWhereOverwritten(){
    List<Account> accounts = new List<Account>{};
	List<Opportunity> accounts = new List<Opportunity>{};
    
    for(Integer i = 0; i < 5; i++){
        Account a = new Account(Name = 'Test Account ' + i);
        accounts.add(a);
    }

    test.startTest();		    
    insert accounts; 		
    test.stopTest();
	
	for(Integer i = 0; i < 5; i++){
        Opportunity opp = new Opportunity(Name = 'Test Opportunity ' + i, AccountId = accounts[i].id);
        opportunities.add(opp);
    }
	
	test.startTest();	
    insert opportunities; 	
    test.stopTest();
}

Please close the thread marking this answer as Best Answer if it really helped. Closing the thread help others finding the correct answer.

Regards,
Swayam Arora
 

All Answers

Swayam  AroraSwayam Arora
Hi,
Below is the code that you are looking for:-
@isTest
public class MyTest {
	public testMethod void verifyAccountDescriptionsWhereOverwritten(){
    List<Account> accounts = new List<Account>{};
	List<Opportunity> accounts = new List<Opportunity>{};
    
    for(Integer i = 0; i < 5; i++){
        Account a = new Account(Name = 'Test Account ' + i);
        accounts.add(a);
    }

    test.startTest();		    
    insert accounts; 		
    test.stopTest();
	
	for(Integer i = 0; i < 5; i++){
        Opportunity opp = new Opportunity(Name = 'Test Opportunity ' + i, AccountId = accounts[i].id);
        opportunities.add(opp);
    }
	
	test.startTest();	
    insert opportunities; 	
    test.stopTest();
}

Please close the thread marking this answer as Best Answer if it really helped. Closing the thread help others finding the correct answer.

Regards,
Swayam Arora
 
This was selected as the best answer
Swayam  AroraSwayam Arora
Please add 'static' before testMethod in line No 3
David ZhuDavid Zhu
I reedited your trigger as below for clearer view.

trigger newTrg on Opportunity (before insert) 
{
    for(opportunity opp:trigger.new)
    {
        Set<Id> s1 = new Set<Id>();
        if(opp.accountId != null)
        {
            s1.add(opp.accountid);
        }

        if(s1.size()>0)
        {
            Map<Id,Id> m1 = new Map<id,Id>();
            for(Account acc : [Select id, ownerId from Account where id in:s1])
            {
                m1.put(acc.id,acc.ownerId);
            }

            for(Opportunity opp1:trigger.new)
            {
                //Custom Field
                opp1.TestUser__c = m1.get(opp.accountId);
            }
        }
    }
}


It seems you want to put last available opp's accountid to all opp's TestUser__c field.

For your reference, I came up with following test case. It only contains positive test case and I didn't confirm if there is any syntax issue.

@isTest
public class Opportunity_newTrg_Test
{
    public static testMethod void PositiveTest()
    {
        List<Account> accounts = new List<Account>{};
        List<Opportunity> opportunities = new List<Opportunity>{};

        //build test account and insert
        for(Integer i = 0; i < 5; i++)
        {
            Account a = new Account(Name = 'Test Account ' + i);
            accounts.add(a);
        }
        insert accounts;       
     
        //build test opps and insert
        for(Integer i = 0; i < 5; i++)
        {
            Opportunity opp = new Opportunity(Name = 'Test Opportunity ' + i, AccountId = accounts[i].id);
            opportunities.add(opp);
        }
        insert opportunities;  
            
        //get the last opp's accountid
        string lastaccid = accounts[4].id;
     

        //retrieve the inserted opps
        opportunities = [select TestUser__c from opportunity where name like 'Test Opportunity%'];

        //assert        
        For (Opportunity opp : Opportunities)
        {
            system.asserteaquals(lastaccid,opp.TestUser__c);
        }
        
    }

}