+ Start a Discussion
sonali  vermasonali verma 

System.Assert failing

Hello friends

I have wrriten apex class code to prevent duplicate contacts from being entered.
My apex class code and trigger code is working but my test class is failing.
Can anyone pls help me out?

apex code:
public class No_Account_Duplicates
{
   public void Display(list<Account> acc)
   {
      for(Account a:acc)
      {
         list<Account> myList=[select Name from Account where Name=:a.Name];
              if (myList.size()>0)
                 a.Name.addError('Account Name Already Exists');
      }
   }
}

apex trigger code:
trigger trg_NoDuplicates on Account (before Insert)
{
            if ((trigger.IsBefore) || (trigger.isInsert))
            {
                     No_Account_Duplicates obj=new No_Account_Duplicates();
                     obj.Display(trigger.new); //correct
            }    
    }


apex test class:
@istest(SeeAllData=false)
private class NoAccountDuplicates_Test
{
    private static testmethod void validatetest()
    {
          Account a1=new Account();
          a1.Name='Contact0';
          insert a1;
          
          Account a2=new Account();
           a2.Name='Contact0';
           insert a2;
           
           try 
           {
                        insert a2;
           }
           catch (DmlException e)
            {
                        //Assert Error Message
                   System.assert( e.getMessage().contains('Insert failed. First exception on ' + 'row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, ' +
                                  'Account Name Already Exists'), 
                                  e.getMessage() );
                                   e.getDmlStatusCode(0);
                 }
               }
 }


I am getting error message as:
Error Message    System.AssertException: Assertion Failed: Insert failed. First exception on row 0 with id 0019000001OrsvvAAB;
first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Regards
sonali verma
 
Best Answer chosen by sonali verma
surasura
your trigger is not according to best practice  as it results in querying inside a for loop .

you can do above without any code.

create custom field on Account name it as Unique Check

check its unique property (check box true)

create workflow rule to update the Unique Check field with account Name 


also you can use duplicate rules https://help.salesforce.com/apex/HTViewHelpDoc?id=duplicate_rules_create.htm&language=en_US (https://help.salesforce.com/apex/HTViewHelpDoc?id=duplicate_rules_create.htm&language=en_US)
 

All Answers

surasura
you have inserted a2 in two places 
 
@istest(SeeAllData=false)
private class NoAccountDuplicates_Test
{
    private static testmethod void validatetest()
    {
          Account a1=new Account();
          a1.Name='Contact0';
          insert a1;
          
          Account a2=new Account();
           a2.Name='Contact0';
          // insert a2;
           
           try 
           {
                        insert a2;
           }
           catch (DmlException e)
            {
                        //Assert Error Message
                   System.assert( e.getMessage().contains('Insert failed. First exception on ' + 'row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, ' +
                                  'Account Name Already Exists'), 
                                  e.getMessage() );
                                   e.getDmlStatusCode(0);
                 }
               }
 }

 
surasura
your trigger is not according to best practice  as it results in querying inside a for loop .

you can do above without any code.

create custom field on Account name it as Unique Check

check its unique property (check box true)

create workflow rule to update the Unique Check field with account Name 


also you can use duplicate rules https://help.salesforce.com/apex/HTViewHelpDoc?id=duplicate_rules_create.htm&language=en_US (https://help.salesforce.com/apex/HTViewHelpDoc?id=duplicate_rules_create.htm&language=en_US)
 
This was selected as the best answer
Ajay K DubediAjay K Dubedi
When matching wrong Boolean values using System.AssertEquals, sometimes it fails with "Assertion Failed" error message when test class is executed using. So The best way to use System.Assert and System.Assert Equals while validating positive and negative test cases are System.Assert accepts two parameters one which is mandatory which is the condition to test for and the other a message which is optional.
next System.AssertEquals and System.AssertNotEquals both accepts three parameters; the first two (mandatory) are the variables that will be tested for in/equality and the third (optional) is the message to display if the assert results in false. Both are used in Test Class for “Positive Case Testing” and “Negative Case Testing” with single and multiple records.
Let me know if this helps!