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
Shane Jay HayesShane Jay Hayes 

Little help with test class "Invalid type" error. I'm ALMOST there!

I have a couple of nearly identical Apex classes that I am trying to get test classes done for. Each uses a different object.  I was able to the the one that calls on the Account to work perfectly.  Now I am just trying to tweak it to get the other one that calls on a custom object to work equally perfect.  I keep getting an "invalid type" error.  Okay so here is the class I am trying to write the test class for:
 
public class CauseLossControllerEXT {
public blob picture { get; set; }
    public String errorMessage { get; set; }
    public Attachment	newAttach {get; set;}
    private final Claims__c claim;
    private final String  ERROR_IMG_TYPE    = 'The image must be in .jpg, .gif or .png format';   
    private Set<String> imagesTypes         = new Set<String> {'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png', 'image/gif'};
    private Set<String> notAllowedTypes     = new Set<String> {'application/octet-stream'};
    private ApexPages.StandardController stdController;

    public CauseLossControllerEXT(ApexPages.StandardController stdController) {
        this.claim = (Claims__c)stdController.getRecord();
        this.stdController = stdController;
    }

    public PageReference save() {
        errorMessage = '';
        try {
            upsert claim;
            if (picture != null) {
                Attachment attachment = new Attachment();
                attachment.body = picture;
                attachment.name = 'causeofloss_' + claim.id;
                attachment.Description = 'Cause of Loss Photo';
                attachment.parentid = claim.id;
                insert attachment;
                claim.CauseofLossURL__c = '/servlet/servlet.FileDownload?file='
                                          + attachment.id;
                update claim;
            }
            return new ApexPages.StandardController(claim).view();
        } catch(System.Exception ex) {
            errorMessage = ex.getMessage();
            return null;
        }
    }
}

Here is the class I successfully wrote the test class for:
 
public class CompanyLogoControllerEXT {
public blob picture { get; set; }
    public String errorMessage { get; set; }
    public Attachment	newAttach {get; set;}
    private final Account account;
    private final String  ERROR_IMG_TYPE    = 'The image must be in .jpg, .gif or .png format';   
    private Set<String> imagesTypes         = new Set<String> {'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png', 'image/gif'};
    private Set<String> notAllowedTypes     = new Set<String> {'application/octet-stream'};
    private ApexPages.StandardController stdController;

    public CompanyLogoControllerEXT(ApexPages.StandardController stdController) {
        this.account = (Account)stdController.getRecord();
        this.stdController = stdController;
    }

    public PageReference save() {
        errorMessage = '';
        try {
            upsert account;
            if (picture != null) {
                Attachment newAttach = new Attachment();
                newAttach.body = picture;
                newAttach.name = 'companylogo_' + account.id;
                newAttach.Description = 'Company Logo';
                newAttach.parentid = account.id;
                insert newAttach;
                account.CompLogoURL__c = '/servlet/servlet.FileDownload?file='
                                          + newAttach.id;
                update account;
            }
            return new ApexPages.StandardController(account).view();
        } catch(System.Exception ex) {
            errorMessage = ex.getMessage();
            return null;
        }
    }
}

Here is the SUCCESSFUL test class (100% coverage)
 
@isTest
public class CompanyLogoControllerEXTTest {
	@isTest
    static void testCompanyLogo() {
        Account acc=new Account();
        acc.Name='testAcc';
        ApexPages.StandardController sc = new ApexPages.StandardController( acc );
    	CompanyLogoControllerEXT comp=new CompanyLogoControllerEXT(sc);       
        String str='testblob';
        comp.picture=Blob.valueof(str);
        comp.save();
        delete acc;
        comp.save();
    }
}

I am trying to tweak that successful test class for the first class above (the CauseLossControllerEXT)

I got this far, below, but I know I have something wrong with the tweak.  Getting "invalid type" error on line 5
 
@isTest
public class CauseLossControllerEXTtest {
	@isTest
    static void testCauseLoss() {
        Claims__C acc=new claim();
        acc.Name='testAcc';
        ApexPages.StandardController sc = new ApexPages.StandardController( acc );
    	CauseLossControllerEXT comp=new CauseLossControllerEXT(sc);       
        String str='testblob';
        comp.picture=Blob.valueof(str);
        comp.save();
        delete acc;
        comp.save();
    }
}

Any help is greatly appreciated.
Best Answer chosen by Shane Jay Hayes
Shane Jay HayesShane Jay Hayes
Balaji that did solve that. Now the issue is that the Name on the custom object is an autonumber, so line 6 is causing a "field is not writeable" error. Current code looks like:
 
@isTest
public class CauseLossControllerEXTtest {
	@isTest
    static void testCauseLoss() {
        Claims__C acc=new claims__c();
        acc.Name='testAcc';
        ApexPages.StandardController sc = new ApexPages.StandardController( acc );
    	CauseLossControllerEXT comp=new CauseLossControllerEXT(sc);       
        String str='testblob';
        comp.picture=Blob.valueof(str);
        comp.save();
        delete acc;
        comp.save();
    }
}

 

All Answers

Balaji Chowdary GarapatiBalaji Chowdary Garapati
Change the line:


Claims__C acc=new claim();


to

Claims__C acc=new claim__c();
 
Shane Jay HayesShane Jay Hayes
Thank you for looking at this Balaji.  It didn't like that either, same error on line 5.  "invalid type: claim__c"
Balaji Chowdary GarapatiBalaji Chowdary Garapati
oops., looks like spell mistake:

Try:
Claims__C acc=new claims__c(); // it should be the object api name
 
Shane Jay HayesShane Jay Hayes
Balaji that did solve that. Now the issue is that the Name on the custom object is an autonumber, so line 6 is causing a "field is not writeable" error. Current code looks like:
 
@isTest
public class CauseLossControllerEXTtest {
	@isTest
    static void testCauseLoss() {
        Claims__C acc=new claims__c();
        acc.Name='testAcc';
        ApexPages.StandardController sc = new ApexPages.StandardController( acc );
    	CauseLossControllerEXT comp=new CauseLossControllerEXT(sc);       
        String str='testblob';
        comp.picture=Blob.valueof(str);
        comp.save();
        delete acc;
        comp.save();
    }
}

 
This was selected as the best answer
Balaji Chowdary GarapatiBalaji Chowdary Garapati
Hello Shane,

Try the below version
 
@isTest
public class CauseLossControllerEXT_IsTest{
	
	public static  testMethod void testCauseLossControllerExt(){

		CauseLossControllerEXT causeLossCntlr=new CauseLossControllerEXT(new ApexPages.StandardController( new Claims__c() ));   
        causeLossCntlr.picture=Blob.valueof('Test String');
        causeLossCntlr.save();
		 try{
		 delete [select id from  Claims__c];
		 }catch(Exception e){
			System.debug('Exception **'+e.getMessage());
		 }
        causeLossCntlr.save();
		
	}
	
}

If you see any errors, due to lack of data, try to accomodate whats missing and you should be fine


Let me know if it fails, and you cant figure it out why!

Thanks,
Balaji