2

I have written a simple trigger which does some validation. I am trying to understand, if it's really necessary to write Test Class for this. The code coverage is 0%, hence please someone help to understand how to write Test Class for the below code.

trigger Duplicate_Prospect_Check on Account (before insert)

{    
list <Account> Acc = [Select id from Account where Account.name=:trigger.new[0].name and Account.Siebel_Account_ID__c=:trigger.new[0].Siebel_Account_ID__c];

    if(Acc.size()>0)
    {
        trigger.new[0].name.addError('Prospect Account with same Name and Account Id already exists');
     }
}

So I added below code in my Test Class and it ran successfully. But my actual trigger is still not giving 100% code coverage. It's not covering 'addError' statement.

private class Duplicate_Prospect_Check_TriggerTest

 {

static testmethod void test()

    {
        List<Account> acts = new List<Account>();
        for(Integer i=0; i<2; i++)
        {
            Account a = new Account(Name='Duptest',ShippingCity='Boston',ShippingCountry='United States',ShippingState='MA'); 
            acts.add(a);
        }  
        try
        {      
           test.startTest();
           insert acts;
           test.stopTest();
         }  
         catch(Exception e)
        {
            System.Assert(e.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION'));
        }
    }
}
  • 1
    Why do you need a trigger , can you do other means for example with a use a validation rule with Vlookup ? – addy May 24 '16 at 13:43
  • Because we have 2 different concepts like Profiled and Non Profiled Accounts. For Non Profiled, just Name is Unique Identifier and for Profiled, it's combination of Name and Account Id. Let's say we have a Non profiled Account with Name = Test and I am trying to create 2nd Account which is Profiled with same name as Test and some Account Id value. When it tries to create 3rd Account which is also Profiled with same Account Id and same Name, it should error. I couldn't implement it via Validation Rules, so went for Triggers. – Amulya May 24 '16 at 14:05
2

To test a trigger all you need is for your test method to perform the operations for which the trigger is declared. In this case you declared a trigger for insertions. So an insertion of an Account will do. More or less like:

@isTest
static testMethod testInsert() {
  Account account = new Account();
  // set account fields according to your needs
  insert account;
  // your assertions
}
  • Thanks a lot. So i hardcoded some values and Inserted Account in my Test code and it ran successfully. So when we run the Test, does it actually Insert the record into Database? Because i didn't find that record. Or what actually happens when we run Tests? – Amulya May 24 '16 at 14:44
  • 1
    No. Your test records are never stored permanently. – Pedro Otero May 24 '16 at 14:46
1

Hi @Amulya i shared a sample test class for you.As per i understood about your problem there are two types of users.So,create a user with your required profile and try to create account with user.And always try to bulk testing it is also one of the best practice.

@isTest
public class TestFssTeamUpdateoppo
{
static testMethod void testStandardUseropp() 
{
    // Create a new user with the Standard User profile
    Profile standardProf = [select id from profile where name='Standard User']; 
    User su = new User(alias = 'standt', email='standarduser@testorg.com',emailencodingkey='UTF-8',FirstName='opp',
                       lastname='testing',languagelocalekey='en_US',localesidkey='en_US',profileid = standardProf.Id,
                       timezonesidkey='America/Los_Angeles',username='sample@opptest.com');
    // Switch current user to Standard User
    System.runAs(su)
    {

        List<account> Accounts = new List<account> {};
        for(Integer i = 0; i < 200; i++)
        {
                Account a = new Account();
                Accounts.add(a);
        }
        test.startTest();
        // Insert the Account records that cause the trigger to execute.
        insert Accounts;
      // Stop the test, this changes limit context back to test fromtrigger.
        test.stopTest();

    }
}
} 
  • Thanks for the code. We have 2 types of Accounts not users. But yeah your code will be really useful. Thanks again. – Amulya May 24 '16 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.