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
Andrew Hoban 14Andrew Hoban 14 

Compile Error on Test Class

Hi, 

I have a test class that has a compile error when I try to save. I was wondering if anybody could help.
Many thanks

Class:
public class MatchReadyImage {

public Match_Day_Check_List__c obj {get; set; }


public MatchReadyImage(){
           obj = [Select Id, Match_Day_Ready_Status__c  From Match_Day_Check_List__c Where Name = 'Everton V West Ham United Goodison Park EPL 2013-05-12'];
    }
}

Test Class:
@isTest(seeAllData = true)
private class MatchReadyImageTest {

    private static void MatchReadyImage() {
        Match_Day_Check_List__c mdckl = new Match_Day_Check_List__c(
            name = 'Everton V West Ham United Goodison Park EPL 2013-05-12',
             Fixture__c = [select id from CS_Fixture__c limit 1].id,
             Match_Plan__c = [select id from Pre_Match__c limit 1].id);

        insert mdckl;       
      
        System.assert((new MatchReadyImage).obj != null);
    }

}

 
Best Answer chosen by Andrew Hoban 14
James LoghryJames Loghry
Because you need the Ids for the relationships, you have to insert the CS_Fixture record and the Pre_Match__c record before you construct your Match_Day_Check_List__c record.  
 
static testMethod void MatchReadyImage() {
        CS_Fixture__c fff = new CS_Fixture__c();
        insert fff;

        Pre_Match__c ppp = new Pre_Match__c();
        insert ppp;

        Match_Day_Check_List__c mmm = new Match_Day_Check_List__c(
            Fixture__c = fff.Id
            ,Match_Plan__c = ppp.Id
            ,Name = 'Everton V West Ham United Goodison Park EPL 2013-05-12'
        );
         
        insert mmm; 
        
        Test.startTest(); 
        MatchReadyImage con = new MatchReadyImage();
        Test.stopTest();
        
        System.assert(con.obj != null);
    }
}

 

All Answers

Andrew Hoban 14Andrew Hoban 14
The error message I am recieving is "System.QueryException: List has more than 1 row for assignment to SObject"

Stack Trace: Class.MatchReadyImage.<init>: line 7, column 1
Class.MatchReadyImageTest.MatchReadyImage: line 12, column 1
James LoghryJames Loghry
That's not a compile error, that's a runtime error :)

You're using SeeAllData=true.  This means your test class will see existing data on your org.  It looks like the query in your constructor is returning multiple records because of this.

The best practice is to remove any dependency on existing data (Remove the SeeAllData=true annotation), and instead, create any records you may need in your unit test.  In your case, you'll want to create records for CS_Fixture__c and Pre_Match__c prior to creating the  "Match_Day_Check_List__c" record in your unit test.
John PipkinJohn Pipkin
Your test method needs to say "static testMethod void MatchReadyImage()" and you need () when calling the constructor. Like this:
 
static testMethod void MatchReadyImage() {

        Match_Day_Check_List__c mdckl = new Match_Day_Check_List__c(
            name = 'Everton V West Ham United Goodison Park EPL 2013-05-12',
              Fixture__c = [select id from CS_Fixture__c limit 1].id,
             Match_Plan__c = [select id from Pre_Match__c limit 1].id);

        insert mdckl;      
        MatchReadyImage con = new MatchReadyImage();
        System.assert(con.obj != null);

    }

 
John PipkinJohn Pipkin
Ah, I didn't see the runtime error. (I really have to reffresh my page before answering). 

James' answer is the way to go.
Andrew Hoban 14Andrew Hoban 14
Thanks for both of your replies.

I have made an attempt at this, however the class is still failing. I am having trouble inserting the ID of both the CS_Fixture__c and Pre_Match__c (i think)

Thanks
@isTest
public class MatchReadyImageTest {

static testMethod void MatchReadyImage() {

        CS_Fixture__c fff = new CS_Fixture__c();
        Pre_Match__c ppp = new Pre_Match__c();
        Match_Day_Check_List__c mmm = new Match_Day_Check_List__c();
        
        mmm.Fixture__c = [select id from CS_Fixture__c limit 1].id;
        mmm.Match_Plan__c = [select id from Pre_Match__c limit 1].id;
        mmm.name = 'Everton V West Ham United Goodison Park EPL 2013-05-12';
             
        insert mmm;  
        insert fff; 
        insert ppp;    
        
        MatchReadyImage con = new MatchReadyImage();
        System.assert(con.obj != null);

    }
}

 
John PipkinJohn Pipkin
Andrew, you need to insert fff and ppp before you can retrieve the ID
James LoghryJames Loghry
Because you need the Ids for the relationships, you have to insert the CS_Fixture record and the Pre_Match__c record before you construct your Match_Day_Check_List__c record.  
 
static testMethod void MatchReadyImage() {
        CS_Fixture__c fff = new CS_Fixture__c();
        insert fff;

        Pre_Match__c ppp = new Pre_Match__c();
        insert ppp;

        Match_Day_Check_List__c mmm = new Match_Day_Check_List__c(
            Fixture__c = fff.Id
            ,Match_Plan__c = ppp.Id
            ,Name = 'Everton V West Ham United Goodison Park EPL 2013-05-12'
        );
         
        insert mmm; 
        
        Test.startTest(); 
        MatchReadyImage con = new MatchReadyImage();
        Test.stopTest();
        
        System.assert(con.obj != null);
    }
}

 
This was selected as the best answer
Andrew Hoban 14Andrew Hoban 14
Thank you both that has worked.