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
BigpermBigperm 

Apex Class: lines not being tested

I'm currently trying to add this Apex class:
public with sharing class FileUploadController {
	
	public Document document {
		get {
			if (document == null)
			document = new Document(); 
			return document;
		}
		set;
	}
	public PageReference upload() {
		document.AuthorId = UserInfo.getUserId();
		document.FolderId = UserInfo.getUserId(); // put it in running user's folder
		
		try {
			insert document;
			}catch (DMLException e) {
				ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file'));
				return null;
			}finally {
				document.body = null; // clears the viewstate 
				document = new Document(); 
			}
			ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully')); 
			return null;
	}
}

 

 

When I try to test the class, it will say that it's skipped 17 of the 27 lines. I'm obviously a little new to this. What could I have done wrong?

I'm trying to create an upload button based on this helpful tool here: http://developer.force.com/cookbook/recipe/uploading-a-document-using-visualforce-and-a-custom-controller
What I hope to do in the end is add an upload button on a "contacts'" page.
Again, any help would be greatly appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
Kevin SwiggumKevin Swiggum

It sounds like you don't have any test code included in your class. In order to get test code coverage you'll need to write a testmethod that specifically runs you're class.

 

the test method can be included right within your FileUploadController class and would look something like this.

 

 

static testMethod void testFileUpload() {
	//Instantiate the controller
	FileUploadController ctl = new FileUploadController();
	
	//Grab the reference to the document and populate it
	Document doc = ctl.document;
	doc.Name = 'TESTDOC123456789';
	doc.body = Blob.valueOf('I am a test');
	doc.Description = 'Test Description';
		
	//Run the upload method
	ctl.upload();
		
	//Make sure it worked by querying for the 
	//document that was just created.
	List<Document> docs = [SELECT ID 
				FROM Document 
				WHERE NAME = 'TESTDOC123456789'];
	System.assertEquals(1, docs.size());
		
}

Paste the above code into your class and re-run unit tests. You should have coverage once the test method is in place.

 

All Answers

Kevin SwiggumKevin Swiggum

Can you share the test code you're using to test the class? ...and are you able to see which lines of code aren't getting covered? 

BigpermBigperm

I'm currently using Force IDE, so it's the test that comes with that from what I can understand.

 

Yes it gives a list of them in the error code: lines: 1,3,5,6,7,11,12,13,15,16,17,18,19,21,22,24,25

 

BigpermBigperm

I tried it once more. I basically started from scratch using: http://developer.force.com/cookbook/recipe/uploading-a-document-using-visualforce-and-a-custom-controller as a guide. It gives me another error now. FileUploadController:file name mismatch with class name: FileUploadController.

Where FileUploadController is the name of the class.

Kevin SwiggumKevin Swiggum

It sounds like you don't have any test code included in your class. In order to get test code coverage you'll need to write a testmethod that specifically runs you're class.

 

the test method can be included right within your FileUploadController class and would look something like this.

 

 

static testMethod void testFileUpload() {
	//Instantiate the controller
	FileUploadController ctl = new FileUploadController();
	
	//Grab the reference to the document and populate it
	Document doc = ctl.document;
	doc.Name = 'TESTDOC123456789';
	doc.body = Blob.valueOf('I am a test');
	doc.Description = 'Test Description';
		
	//Run the upload method
	ctl.upload();
		
	//Make sure it worked by querying for the 
	//document that was just created.
	List<Document> docs = [SELECT ID 
				FROM Document 
				WHERE NAME = 'TESTDOC123456789'];
	System.assertEquals(1, docs.size());
		
}

Paste the above code into your class and re-run unit tests. You should have coverage once the test method is in place.

 

This was selected as the best answer