Creating a test class for a Select Statement

2020-05-07 04:59发布

I was wondering if you could help me. I am strugging to create a test class for the code below. Any help would be appreciated.

Many thanks

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'
      ];
   }
}

2条回答
叼着烟拽天下
2楼-- · 2020-05-07 05:32

I'm confused what's the real requirement of having this class. May be you have posted very short version of it. Anyway you can use below test class(untested) for this.

@isTest
private class TestMatchReadyImage {

    @isTest
    static testMethod void testConstructor() {
        Match_Day_Check_List__c mdckl = new Match_Day_Check_List__c()            
        mdckl.Name = 'Everton V West Ham United Goodison Park EPL 2013-05-12';
        // populate if any other fields you need to
        insert mdckl;

        // make assertions for the unit test
        System.assert((new MatchReadyImage()).obj != null);
    }
}
查看更多
地球回转人心会变
3楼-- · 2020-05-07 05:38

You just need to create a test data which will be selected by your code, because the data from Org is not available in test context. After that you have to instantiate MatchReadyImage class and validate that obj has a correct value

@isTest
private class MatchReadyImageTest {

    @isTest
    private static void test1() {
        Match_Day_Check_List__c mdckl = new Match_Day_Check_List__c(
            name = 'Everton V West Ham United Goodison Park EPL 2013-05-12';
            // other required fields 
        );
        insert mdckl;
        // you can add assertions which you want 
        System.assert((new MatchReadyImage).obj != null);
    }
}
查看更多
登录 后发表回答