0

I'm encountering this kind of error

System.QueryException: List has no rows for assignment to SObject

when try to run the test

@isTest   private class AccExt2Test {  @isTest  private static void testExt2() {
    Account acc = [select id,name from account where name like 'Edg%' limit 1];
    Test.startTest();
    Pagereference pageRef = Page.FunWithControllers;
    pageRef.getParameters().put('id', acc.id);
    Test.setCurrentPage(pageRef);
    AccExt2 controller = new AccExt2(new ApexPages.StandardController(acc));
    Test.stopTest();
    system.assertEquals(controller.acc.id, acc.id );
}

}

What's wrong with my code?

Account acc = [select id,name from account where name like 'Edg%' limit 1]; 

system.debug(acc);

this code gets me Account

1

Most records in an org are not visible to tests. You need to insert your own records in your test.

Why? This makes tests self-contained rather than them relying on pre-existing data. It also usually makes assertions easier to write because the data values added are close at hand in the test.

See e.g. Create Test Data for Apex Tests.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.