You need to sign in to do that
Don't have an account?
Test Class For this Controller class?
can any one help me for writing test class for this..
public class vW_Contract_Attachment{
public vW_Contracts_and_Legal_Attachment__c vWDoc{get;set;}
public Attachment attachContract {get;set;}
List<Account> acc;
public vW_Contract_Attachment(ApexPages.StandardController stdController) {
acc = [Select Id, Name From Account Limit 1];
attachContract = new Attachment();
vWDoc = (vW_Contracts_and_Legal_Attachment__c)stdController.getRecord();
}
public PageReference saveAttachment(){
vWDoc.Size__c = String.valueOf(attachContract.bodyLength/1024)+'KB';
insert vWDoc;
attachContract.ParentId =vWDoc.Id;
insert attachContract;
PageReference nextPage = new PageReference('/' + vWDoc.Id);
return nextPage;
}
}
@isTest
Private class Test_Contract_Attachments{
static testMethod void Test_Contract_Attachments()
{
Account acc=new Account(Name='Test Account',recordTypeId='012E00000001vtF');
insert acc;
Contracts_and_Legal_Attachment__c Doc = new Contracts_and_Legal_Attachment__c();
Doc.Account_Name__c=acc.Id;
ApexPages.StandardController sc = new ApexPages.StandardController(Doc);
Contract_Attachment vW = new Contract_Attachment(sc);
vW.attachContract.parentId=Doc.Id;
vW.attachContract.Name='Test Document';
vW.attachContract.Body=Blob.ValueOf('Test Data');
vW.saveAttachment();
List<Attachment> attachments = [Select Id, Name from Attachment where Parent.Id=:vWDoc.Id Limit 1];
System.assertEquals(1,attachments.size());
}
}
conroller class
public class Contract_Attachment{
public Contracts_and_Legal_Attachment__c vWDoc{get;set;}
public Attachment attachContract {get;set;}
public Contract_Attachment(ApexPages.StandardController stdController) {
Doc = (vContracts_and_Legal_Attachment__c)stdController.getRecord();
attachContract = new Attachment();
}
public PageReference saveAttachment(){
if(Test.isRunningTest()){
upsert Doc;
attachContract.parentId = Doc.Id;
insert attachContract;
Attachment attch = [Select Id,bodyLength from Attachment WHERE Id = :attachContract.Id Limit 1];
Doc.Size__c = String.valueOf(attch.bodyLength/1024)+'KB';
update Doc;
}
else{
Doc.Size__c = String.valueOf(attachContract.bodyLength/1024)+'KB';
upsert Doc;
attachContract.parentId = Doc.Id;
insert attachContract;
}
PageReference nextPage = new PageReference('/' + Doc.Id);
return nextPage;
}
}
All Answers
1. Creates an instance of vW_Contacts_and_Legal_Attachment__c and inserts it
2. Instantiates a standard controller, passing the sobject created in step 1 as the constructor argument.
3. Instantiates the vW_Contact_Attachment class, passing the standard controller created in step 2 as the constructor argument.
4. Creates an attachment and assigns it to the attachContract property
5. Execute the saveAttachment method and verifies it was saved correctly to the database.
As you have been able to write the apex code for the class, I'm sure you'll have no trouble with the test class. If you do be sure to post back.
@isTest
Private class TestvW_Contract_Attachments{
static testMethod void TestvW_Contract_Attachments()
{
Account acc=new Account(Name='Test vWAccount',recordTypeId='012E00000001vtF');
insert acc;
Account acc1 = [select Id, Name from Account where Name='Test vWAccount'];
vW_Contracts_and_Legal_Attachment__c vWDoc = new vW_Contracts_and_Legal_Attachment__c();
vWDoc.Account_Name__c=acc1.Id;
vWDoc.File_Name__c='Test File';
vWDoc.Size__c='50KB';
vWDoc.Type__c='MSA';
insert vWDoc;
ApexPages.StandardController sc = new ApexPages.StandardController(vWDoc);
vW_Contract_Attachment vW = new vW_Contract_Attachment(sc);
Attachment attachContract = new Attachment();
attachContract.ParentId =vWDoc.Id;
insert attachContract;
vW.saveAttachment();
}
}
Yes I am trying to execute this test and I have changed the test code. I am stuck with this test class need some advise. I am getting a Null Pointer excetion caz my attachContract object is expecting a document to be attached but my test class is not providng that. can you please what's wrong wth my code below.
public class vW_Contract_Attachment{
public vW_Contracts_and_Legal_Attachment__c vWDoc{get;set;}
public Attachment attachContract {get;set;}
List<Account> acc;
public vW_Contract_Attachment(ApexPages.StandardController stdController) {
acc = [Select Id, Name From Account Limit 1];
attachContract = new Attachment();
vWDoc = (vW_Contracts_and_Legal_Attachment__c)stdController.getRecord();
}
public PageReference saveAttachment(){
vWDoc.Size__c = String.valueOf(attachContract.bodyLength/1024)+'KB';
insert vWDoc;
attachContract.ParentId =vWDoc.Id;
insert attachContract;
PageReference nextPage = new PageReference('/' + vWDoc.Id);
return nextPage;
}
}
@isTest
Private class TestvW_Contract_Attachments{
static testMethod void TestvW_Contract_Attachments()
{
Account acc=new Account(Name='Test vWAccount',recordTypeId='012E00000001vtF');
insert acc;
vW_Contracts_and_Legal_Attachment__c vWDoc = new vW_Contracts_and_Legal_Attachment__c();
vWDoc.Account_Name__c=acc.Id;
insert vWDoc;
ApexPages.StandardController sc = new ApexPages.StandardController(vWDoc);
vW_Contract_Attachment vW = new vW_Contract_Attachment(sc);
Attachment attach = new Attachment();
attach.ParentId = vWDoc.Id;
insert attach;
vW.saveAttachment();
vW_Contracts_and_Legal_Attachment__c vWDoc1 = [Select Id,Account_Name__c,Size__c,Type__c,File_Name__c from vW_Contracts_and_Legal_Attachment__c where Id=:vWDoc.Id];
System.assertEquals(vW.saveAttachment(),null);
}
}
4. Creates an attachment and assigns it to the attachContract property
You have created an attachment, inserted it and not assigned it to the controller property.
Change your code from:
to:
Actually I am getting error in " vWDoc.Size__c = String.valueOf(attachContract.bodyLength/1024)+'KB'; " Because it is not getting attachContract.bodyLength from the test class. I don't know how to pass this from the test class. I am having a blob body but how to calucate the body length there and pass to the test class? Can you please suggest me..
Main Controller class: vWDoc.Size__c = String.valueOf(attachContract.bodyLength/1024)+'KB'
Test Class : Blob b = Blob.valueOf('Test Data');
vW.attachContract.parentId=vWDoc.Id;
vW.attachContract.Name='Test Document';
vW.attachContract.Body = b;
If I have to use any other thing instead of blob to get the body length of the attachment or I have to attache a file itself in the test class. Need Your help.
"Field is not writeable: Attachment.BodyLength". Is there any other way to attach a file to the custom object in the controller class and get the size of the file?
@isTest
Private class Test_Contract_Attachments{
static testMethod void Test_Contract_Attachments()
{
Account acc=new Account(Name='Test Account',recordTypeId='012E00000001vtF');
insert acc;
Contracts_and_Legal_Attachment__c Doc = new Contracts_and_Legal_Attachment__c();
Doc.Account_Name__c=acc.Id;
ApexPages.StandardController sc = new ApexPages.StandardController(Doc);
Contract_Attachment vW = new Contract_Attachment(sc);
vW.attachContract.parentId=Doc.Id;
vW.attachContract.Name='Test Document';
vW.attachContract.Body=Blob.ValueOf('Test Data');
vW.saveAttachment();
List<Attachment> attachments = [Select Id, Name from Attachment where Parent.Id=:vWDoc.Id Limit 1];
System.assertEquals(1,attachments.size());
}
}
conroller class
public class Contract_Attachment{
public Contracts_and_Legal_Attachment__c vWDoc{get;set;}
public Attachment attachContract {get;set;}
public Contract_Attachment(ApexPages.StandardController stdController) {
Doc = (vContracts_and_Legal_Attachment__c)stdController.getRecord();
attachContract = new Attachment();
}
public PageReference saveAttachment(){
if(Test.isRunningTest()){
upsert Doc;
attachContract.parentId = Doc.Id;
insert attachContract;
Attachment attch = [Select Id,bodyLength from Attachment WHERE Id = :attachContract.Id Limit 1];
Doc.Size__c = String.valueOf(attch.bodyLength/1024)+'KB';
update Doc;
}
else{
Doc.Size__c = String.valueOf(attachContract.bodyLength/1024)+'KB';
upsert Doc;
attachContract.parentId = Doc.Id;
insert attachContract;
}
PageReference nextPage = new PageReference('/' + Doc.Id);
return nextPage;
}
}