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
SKriLLSKriLL 

Why does this test class succeed?

Hi guys and gals,

Why does this test succeed?  I would expect it to fail...

@isTest(SeeAllData=false)
public class RepoBatchTest2
{
   static testMethod void testMethod1()
   {
       List<Account> a1list = [select id from account where name = 'asdf1234asdf1234asdf1234asdf1234asdf1234asdf1234'];
       integer i1 = 0;
       for(account a : a1list)
       {
           i1++;
       }
       Account a = new Account();
       a.name='asdf1234asdf1234asdf1234asdf1234asdf1234asdf1234';
       insert a;
       List<Account> a2list = [select id from account where name = 'asdf1234asdf1234asdf1234asdf1234asdf1234asdf1234'];
       integer i2 = 0;
       for(account a1 : a1list)
       {
           i2++;
       }
       System.assertEquals(i1,i2); //i1 should = 0 and i2 should = 1... both = 0??
   }
}
Best Answer chosen by SKriLL
James LoghryJames Loghry

TLDR Version:

You have a typo.  Change this:

 

for(account a1 : a1list) {
    i2++;
}

To:

for(account a2 : a2list) {
    i2++;
}

All Answers

AshlekhAshlekh
Hi,

According to code it will run fine.

first SOSL will try to fetch the data from account but it will not found and return a new instance of List of account so a1list has 0 size.

so i1 will be 0

Now you have inserted the account.

In second query soql it will fetch a single record and store in a2list.

now you have initlize i2=0;

And you try for loop on a1list but it has no any record so i2 will be zero.

and in last system assert will check that i1 and i2 both have zero value,


If you change the second for loop by a2list then it will give you error.
James LoghryJames Loghry

TLDR Version:

You have a typo.  Change this:

 

for(account a1 : a1list) {
    i2++;
}

To:

for(account a2 : a2list) {
    i2++;
}

This was selected as the best answer
James LoghryJames Loghry
Also, why are you looping through and incrementing i1 and i2, instead of just using a1list.size() and a2list.size() ?