How to cover if (serviceCatalogueRecord.RecordType.Name != 'Service 3') in the test class? I have used serviceCatalogueRecord.RecordType.Name != 'Service Level 3'; in my test class which is in the get property, but it throwing the following error:

expressions cannot be statement

apex class:

public class ServiceCatalogueWrap {           
    public Service_Catalogue__c serviceCatalogueRecord { get; set; }

    public List<Service_Catalogue__c> childServices {
        get {
            if (serviceCatalogueRecord.RecordType.Name != 'Service Level 3') childServices = [SELECT Id, Parent_Contracted__c, Name, Description__c,RecordType.Name FROM Service_Catalogue__c WHERE Parent__c =: serviceCatalogueRecord.Id OR Level__c =: serviceCatalogueRecord.Id];

            return childServices;

Test class:

ServiceCatalogueTreeViewCtrl.ServiceCatalogueWrap scWrap = new ServiceCatalogueTreeViewCtrl.ServiceCatalogueWrap(serCat2);  
serviceCatalogueRecord.RecordType.Name != 'Service 3';
  • 2
    Hi sfdclearner, welcome to SFSE. Please take a moment to visit the help center, scroll through the tour, and read How to Ask. This is not a question, since you haven't asked anything. If you would like to learn the basics of unit testing, the Trailhead platform may be a good place to start. – Adrian Larson Jun 9 '17 at 13:50
  • @AdrianLarson sure will check it.I have modified the question where the problem is – sfdclearner Jun 9 '17 at 14:20

You have to actually instantiate the record with a RecordType of some other name, then call the getter. For example:

Service_Catalogue__c record = new Service_Catalogue__c();
record.RecordType = [
    SELECT Name FROM RecordType
    WHERE Name != 'Blacklisted Value'
    LIMIT 1
// create child data to be queried

    MyWrapper wrapper = new MyWrapper(record);
    List<Service_Catalogue__c> children = wrapper.childServices;

// assert proper children were queried
  • // create child data to be queried - means we need to insert the the data for Service_Catalogue__c – sfdclearner Jun 9 '17 at 14:50
  • @sfdclearner Yes, the query you are trying to get coverage for. You will get code coverage for that line either way, but will not be able to verify behavior without creating the data. – Adrian Larson Jun 9 '17 at 14:51
  • It covered ,For else part do we need to write in a separate method? and how to get '(List<Service_Catalogue__c>) con.getRecords()' in test class .I have List<Service_Catalogue__c> but the above part didnt get covered in the class – sfdclearner Jun 9 '17 at 15:04
  • 1
    @sfdclearner Yes, unit tests should each cover one unit, try to make them atomic. So you would create separate test where you use = '<blacklisted_value>' instead of != '<blacklisted_value>'. – Adrian Larson Jun 9 '17 at 15:06

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.