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];
    Pagereference pageRef = Page.FunWithControllers;
    pageRef.getParameters().put('id', acc.id);
    AccExt2 controller = new AccExt2(new ApexPages.StandardController(acc));
    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]; 


this code gets me Account


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.

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.