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
ShannuShannu 

System.AssertException: Assertion Failed on MassReassignopportunity Class

Code is fine but during deployment Test class is showing faulre.

System.assert(controller.optyList.size()>0);
        
        controller.optyList[0].selected = true;
        controller.helperRecord.Assign_to__c = UserInfo.getUserId();
        controller.Assign();

Could some one help me on this
SubratSubrat (Salesforce Developers) 
Hello ,

The System.AssertException you are encountering suggests that the assertion System.assert(controller.optyList.size()>0) is failing in your test class. This means that the optyList in the controller does not have any records, and therefore, the size is not greater than zero.

To address this issue, you need to ensure that the optyList in the controller is populated with at least one record before executing the Assign() method. Here's an updated version of your test class with an example of how to create and add an opportunity to the optyList:
@isTest
public class MassReassignopportunity_Test {
    @isTest
    static void testAssign() {
        // Create test data
        List<Opportunity> opportunityList = new List<Opportunity>();
        
        // Create and insert an opportunity
        Opportunity opportunity = new Opportunity(
            Name = 'Test Opportunity',
            StageName = 'Prospecting',
            CloseDate = Date.today() + 10,
            // Add other required fields as needed
        );
        insert opportunity;
        
        // Add the opportunity to the opportunity list
        opportunityList.add(opportunity);
        
        // Create an instance of the controller and set the opportunity list
        MassReassignopportunityController controller = new MassReassignopportunityController();
        controller.optyList = opportunityList;
        
        // Assign the opportunity
        controller.optyList[0].selected = true;
        controller.helperRecord.Assign_to__c = UserInfo.getUserId();
        controller.Assign();
        
        // Verify the results
        // Add assertions as needed to validate the expected outcome
        // For example: System.assertEquals(expectedValue, actualValue);
    }
}
In this updated test class, we first create an Opportunity record and insert it into the database. Then, we add the Opportunity to the opportunityList in the controller. This ensures that the optyList has at least one record, satisfying the assertion System.assert(controller.optyList.size()>0).

Hope this helps !
Thank you.
Arun Kumar 1141Arun Kumar 1141
Hi shanu,

The System.AssertException is thrown when an assertion in your test class fails. In your case, the assertion `System.assert(controller.optyList.size()>0)` is failing, indicating that the size of the `optyList` is not greater than zero as expected.

To troubleshoot this issue, you need to identify why the `optyList` is empty in your test scenario. Here are a few possible reasons:

1. Test Data: Ensure that you have properly created test data in your test class. It's possible that there are no opportunities created or associated with the test data, resulting in an empty `optyList`. Make sure you have relevant test data created before invoking the code under test.

2. Filtering or Criteria: Check if there are any filters or criteria in the code that restrict the opportunities retrieved for the `optyList`. Review the code in your `MassReassignopportunity` class to see if there are any conditions that could cause the `optyList` to be empty. Adjust the test data or code accordingly to meet the required conditions.

3. Dependencies: If the opportunities in the `optyList` are dependent on other records or data, ensure that you have properly created and associated the required data in your test class. Sometimes, missing or incorrect data dependencies can cause the `optyList` to be empty.

Review the above possibilities and adjust your test class accordingly to ensure that the `optyList` is populated with the expected opportunities. Additionally, you can add debug statements or use the Salesforce Developer Console to inspect the state of the data and variables during the test execution to identify any potential issues.

if this helps you please mark it as  a best answer,
Thanks!