0

I have the below test but I can't figure out why the opportunity won't query. When I run this test class, the error I keep getting is "System.QueryException: List has no rows for assignment to SObject" that happens at the Opportunity test method

Specific line:

opportunity o = [SELECT Name, id, StageName, LastApexUpdate__c FROM Opportunity WHERE Name='Test Opp' LIMIT 1];

Entire test:

@isTest
public class testAutomationTrigger{

    @testSetup 
    static void setup() {
        Account a = new Account(Name = 'Test Account');
        insert a;

        opportunity o = new Opportunity();
        o.name = 'Test Opp';
        o.Practice_Name__c = 'Test Practice';
        o.StageName = 'Demo Scheduled';
        o.CloseDate = date.today().addDays(10);
        o.AccountId = a.id;
        insert o;

        Contact con = new Contact(LastName='TestContact', 
                                  Email='testContact@test.com', 
                                  AccountId = a.Id, 
                                  LeadSource='Inbound Call');
        insert con;

        Case cs = new Case(AccountId = a.id,
                           ContactId = con.id,
                           Description = 'test',
                           Status = 'New',
                           Subject = 'testSubject');
        insert cs;

        Product2 product = new Product2(); 
        product.name = 'Test Product';
        insert product;

        zqu__QuoteAmendment__c qa = new zqu__QuoteAmendment__c();
        qa.name = 'AH Test Test';
        qa.zqu__Quote__c = q.id;
        insert qa;

        zqu__QuoteRatePlan__c qrp = new zqu__QuoteRatePlan__c();
        qrp.Name = 'Service Plan';
        qrp.zqu__QuoteAmendment__c = qa.id; 
        qrp.zqu__QuoteProductName__c = 'test';
        insert qrp;        

    }


    @isTest
    static void testReputation(){
        opportunity o = [SELECT Name, id, StageName, LastApexUpdate__c FROM Opportunity WHERE Name='Test Opp' LIMIT 1];

        zqu__QuoteRatePlan__c qrp = [SELECT Id, zqu__QuoteProductName__c FROM zqu__QuoteRatePlan__c WHERE Name='Service Plan' LIMIT 1];
        qrp.zqu__QuoteProductName__c = 'Reputation Accelerator';

        Test.startTest();

        o.stageName = 'Closed Won';
        o.LastApexUpdate__c = DateTime.Now();
        update o;

        Test.stopTest();

    }
}
  • 3
    Do you have any triggers or workflows which might be changing the name? What API Version are you using? You could try system.assert(false, [SELECT Name FROM Opportunity]) and see what the actual value is. – Adrian Larson Nov 30 '17 at 1:48
  • If you know that's the only opp created by testSetup (i.e. no opps created by triggers), then you don't really need to specify Name in your SOQL where clause, just do SELECT Name, StageName, LastApexUpdate__c FROM Opportunity LIMIT 1 – David Cheng Nov 30 '17 at 6:03
  • In the @testSetup, on the line after you insert the opportunity, add system.assert(o.Id != null);. If you run the test and it fails with the following message System.AssertException: Assertion Failed, then that means your Opportunity is not being inserted. You will need to look through your debug logs to figure out why the Opp is not being inserted. – Bob Lopez Jan 4 '18 at 5:41

Your Answer

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

Browse other questions tagged or ask your own question.