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
Anjana Sharma 9Anjana Sharma 9 

How to write test classes with assert for this method public static void myFirstMethod() { List<String>listOfStrings = new List<String>{'Anjana','Sharma'}; listOfStrings.add('Test'); listOfStrings.add('Test1');

Best Answer chosen by Anjana Sharma 9
mukesh guptamukesh gupta
Hi Anjana,

you can follow below code:-
 
@isTest
private class CommonTestSetup {
 
    @testSetup static void setup() {
        // Create common test accounts
        List<Account> testAccts = new List<Account>();
        for(Integer i=0;i<2;i++) {
            testAccts.add(new Account(Name = 'TestAcct'+i));
        }
        insert testAccts;        
    }
    
    @isTest static void testMethod1() {
        // Get the first test account by using a SOQL query
        Account acct = [SELECT Id FROM Account WHERE Name='TestAcct0' LIMIT 1];
        // Modify first account
        acct.Phone = '555-1212';
        // This update is local to this test method only.
        update acct;
        
        // Delete second account
        Account acct2 = [SELECT Id FROM Account WHERE Name='TestAcct1' LIMIT 1];
        // This deletion is local to this test method only.
        delete acct2;
        
        // Perform some testing
    }
 
    @isTest static void testMethod2() {
        // The changes made by testMethod1() are rolled back and 
        // are not visible to this test method.        
        // Get the first account by using a SOQL query
        Account acct = [SELECT Phone FROM Account WHERE Name='TestAcct0' LIMIT 1];
        // Verify that test account created by test setup method is unaltered.
        System.assertEquals(null, acct.Phone);
        
        // Get the second account by using a SOQL query
        Account acct2 = [SELECT Id FROM Account WHERE Name='TestAcct1' LIMIT 1];
        // Verify test account created by test setup method is unaltered.
        System.assertNotEquals(null, acct2);
        
        // Perform some testing
    }
 
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

All Answers

mukesh guptamukesh gupta
Hi Anjana,

Please use below code:-
public static void myFirstMethod() { 
List<String>listOfStrings = new List<String>{'Anjana','Sharma'}; 
listOfStrings.add('Test'); 
listOfStrings.add('Test1');


System.assertNotEquals(null, listOfStrings, 'Success')

}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
Anjana Sharma 9Anjana Sharma 9
Actually i have to define my method  as a test use @isTest OR "static testMethod void"  and also add  Test.startTest() and Test.stopTest() in this Also i only have to add simple assert: System.assertEquals(2, listOfStrings.size()); ..how can i do these things
 
mukesh guptamukesh gupta
Hi Anjana,

Now your list size is 4
 
static testMethod void myFirstMethod() { 
List<String>listOfStrings = new List<String>{'Anjana','Sharma'}; 
listOfStrings.add('Test'); 
listOfStrings.add('Test1');

Test.StartTest();
System.assertEquals(4, listOfStrings.size());
Test.StopTest()

}

if you change the size  like  
System.assertEquals(2, listOfStrings.size());

then you will receive error in debug log.

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
Anjana Sharma 9Anjana Sharma 9
Thanku ..can you please help me that how can i write more test classes for different method
mukesh guptamukesh gupta
Hi Anjana,

you can follow below code:-
 
@isTest
private class CommonTestSetup {
 
    @testSetup static void setup() {
        // Create common test accounts
        List<Account> testAccts = new List<Account>();
        for(Integer i=0;i<2;i++) {
            testAccts.add(new Account(Name = 'TestAcct'+i));
        }
        insert testAccts;        
    }
    
    @isTest static void testMethod1() {
        // Get the first test account by using a SOQL query
        Account acct = [SELECT Id FROM Account WHERE Name='TestAcct0' LIMIT 1];
        // Modify first account
        acct.Phone = '555-1212';
        // This update is local to this test method only.
        update acct;
        
        // Delete second account
        Account acct2 = [SELECT Id FROM Account WHERE Name='TestAcct1' LIMIT 1];
        // This deletion is local to this test method only.
        delete acct2;
        
        // Perform some testing
    }
 
    @isTest static void testMethod2() {
        // The changes made by testMethod1() are rolled back and 
        // are not visible to this test method.        
        // Get the first account by using a SOQL query
        Account acct = [SELECT Phone FROM Account WHERE Name='TestAcct0' LIMIT 1];
        // Verify that test account created by test setup method is unaltered.
        System.assertEquals(null, acct.Phone);
        
        // Get the second account by using a SOQL query
        Account acct2 = [SELECT Id FROM Account WHERE Name='TestAcct1' LIMIT 1];
        // Verify test account created by test setup method is unaltered.
        System.assertNotEquals(null, acct2);
        
        // Perform some testing
    }
 
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
This was selected as the best answer
Anjana Sharma 9Anjana Sharma 9
Thanku sir..
Anjana Sharma 9Anjana Sharma 9
I have a cusom object of name Error log and Field name of auto-number i.e (EL-{0000}-1) & Custom Fields: 1. Parent Record Id: Type: Text 2. Error Details: Type: Long Text 3. Parent Record Name: Type: Text and i have to apply database dml on it
I have a method with two arguments List<Account>, List<Contact>  with return type void and in the list<Account > i have to do that some records in this list will have Id field populated and some of the records will not have Id field populated .and then Update the records where Id is populated and insert the records where Id is not populated. (Use Database.upsert for this) .Check the Database.upsert result and if there is any error with any of the record then create a new record of Error Log(in my custom object) for each error.  Now how can i cause error in this and how i hold that error in database.saveresult .

And in second argument of contact i have to hold only the new Contact Records. and like i have 2 contact then these 2 contacts were add to each account details which give success not error