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
sfdc newbiesfdc newbie 

System.AssertException: Assertion Failed

Could anyone help me as i have created Test class:

@isTest
private class ChatterAnswersCreateAccountTest {
    static testMethod void validateAccountCreation() {
        Profile[] p = [SELECT Id FROM Profile WHERE UserType = 'Standard'];
        User[] user = [SELECT Id, Firstname, Lastname FROM User WHERE IsActive = true and ProfileId =: p[0].Id];
        // We cannot create account without a user.
        if (user.size() == 0) { return; }
         String firstName = user[0].FirstName;
        String lastName = user[0].LastName;
        String userId = user[0].Id;
        Account oAccount = new Account();
        oAccount.name = 'TestAccc';
        oAccount.Industry = 'Other';
        oAccount.Type = 'Previous Customer';
        oAccount.Account_Intergraph_Line_of_Business__c = 'MPM';
        oAccount.Street_Address__c = 'ABC';
        oAccount.City__c = 'CA';
        insert oAccount;
       List <Account> account = [SELECT name, Industry, ownerId from Account where Id =: oAccount.Id];
      System.assertEquals(firstName + ' ' + lastName, oAccount.name);
        System.assertEquals(userId, oAccount.ownerId);

    }
}

and getting errors like

Error Message System.AssertException: Assertion Failed: Expected: Jhon Tony, Actual: TestAccc
Stack Trace Class.ChatterAnswersCreateAccountTest.validateAccountCreation: line 20, column 1
bob_buzzardbob_buzzard
This is because you are asserting that the full name of the user is the same as the name of an account, which is incredibly unlikely to be the case:

String firstName = user[0].FirstName;
String lastName = user[0].LastName;
...
oAccount.Name='TestAcc';
...
System.assertEquals(firstName + ' ' + lastName, oAccount.name);

In fact I'd say its impossible, given the account name is testAcc and you introduce a space in your concatenation. 

What is the purpose of this assert?
Bhawani SharmaBhawani Sharma
I am confused what this assert means:
System.assertEquals(firstName + ' ' + lastName, oAccount.name);

Du youhave a trigger which populates the account name based on the logged in user?
If yes, then you need to check the account trigger.
If no, then either you can removethis assert or you can update it to :
System.assertEquals('TestAcc' oAccount.name);

But honestly, looks like you have a trigger but you are not asserting correct values.