+ Start a Discussion
chanti kchanti k 

List has no rows for assignment to SObject in our test class

Hi ,

I have written a test class nad  I am getting this error in test class (System.QueryException: List has no rows for assignment to SObject), please help me . below is my test class and apex class 
 
public without sharing class CountCon {
    Integer count = 0;
    public Account a {get;set;}        
    
    public CountCon()
    {
        Account a = new Account();
        a = [Select NumberOfEmployees from Account where id = '0019000000DNnwo'];
        if(a.NumberOfEmployees != null)
            count = a.NumberOfEmployees;
    }           
    public PageReference incrementCounter() {
            count++;
            try
            {
                a = [Select id from Account where id = '0019000000DNnwo'];
                a.NumberOfEmployees = count;
                update a;
            }
            catch(exception e)
            {
                ApexPages.addMessages(e);
            }
            return null;
    }
       
                       
    public Integer getCount() {
        return count;
    }
}


Test class



@istest
public class CountCon_test
{ 
 static testmethod void test()
 {
 Account acc= new Account();
 Acc.name='test';
 insert Acc;
 
 CountCon cn = new CountCon ();
 cn.incrementCounter();
 cn.getCount();
 
 }
}

User-added image
Thanks,
Chanti.
arpit vijayvergiyaarpit vijayvergiya
Hello Chanti,

As I see you are giving hardcode Id in your class code. If you want to keep this id then following code will help you.
 
public without sharing class CountCon {
    Integer count = 0;
    public Account a {get;set;}        
    public String accId = '0019000000DNnwo'
    public CountCon()
    {
        Account a = new Account();
        a = [Select NumberOfEmployees from Account where id =: accId];
        if(a.NumberOfEmployees != null)
            count = a.NumberOfEmployees;
    }           
    public PageReference incrementCounter() {
            count++;
            try
            {
                a = [Select id from Account where id =: accId ];
                a.NumberOfEmployees = count;
                update a;
            }
            catch(exception e)
            {
                ApexPages.addMessages(e);
            }
            return null;
    }
       
                       
    public Integer getCount() {
        return count;
    }
}


Test class



@istest
public class CountCon_test
{ 
 static testmethod void test()
 {
 Account acc= new Account();
 Acc.name='test';
 insert Acc;
 
 CountCon cn = new CountCon ();
 cn.accId = acc.id;
 cn.incrementCounter();
 cn.getCount();
 
 }
}

If it helps you. Mark it as best answer.
And let me know if you any doubt.

Thanks
Arpit vijayvergiya​
SaurabhDuaSaurabhDua
HI Chanti,

As you have hardcoded the id of the account the apex class did not get a result on line 8 when the test class is run. 
Test classes create their own data and the account created in test class does not have the same Id which you have referenced in your class. Thus the error is received.

If you expect your test class to rely on the data which is created in the org then use @isTest(SeeAllData=true) at the top of your test class instead of @isTest annotation and this would remove the error. 


If it helps you. Mark it as the best answer.

Thanks
Saurabh
Anil KamisettyAnil Kamisetty
Hi

There are multiple errors in the Class, they need to be corrected first.

1. Row Id should not be hard coded in the SOQL (line #8, 16)
2. In the SOQL, List variable is getting assigned to Sobject Variable. It should be the first element, not the list variable (a = [Select Id from Account][0];)
3. Instead of hardcoding Row Id, make it a parameter in the method (IncrmentCounter) or define a public variable in the countCon Public class (as referred by arpit in the above post)
4. Same SOQL is fired twice, once in the Constructor and second time in the IncrementCounter Method. IT is redundant. Instead keep the constructor clean, have your incrementcounter changed like below.

a = [Select Id, NumberOfEmployees from Account where Id = :accid][0];
 if(a.NumberOfEmployees != null)
      count = a.NumberOfEmployees;
count++;
a.NumberOfEmployees = count;
update a;

Above simplified code should help u achieve what you want, everything in IncremnetCounter method (offcourse exception handling needs to be added).

Please mark this if it solves your issue.