You need to sign in to do that
Don't have an account?
keerthana chowdhary
test class failed any one help should be appreciated
public class uploadattachedfiles
{
Public Attachment myfile;
Public Attachment getmyfile()
{
myfile = new Attachment();
return myfile;
}
Public Pagereference Savedoc()
{
String accid = System.currentPagereference().getParameters().get('id');
Attachment a = new Attachment();
a.parentId='00128000005yJi7';
a.name=myfile.name;
a.body = myfile.body;
insert a;
return NULL;
}
}
-----------------------------------------------------
@isTest
public class testuploadattachment {
static testmethod void testattached()
{
// account ac=new account();
//ac.name='sanfransico';
//ac.phone='899899898';
//insert ac;
//opportunity op=new opportunity();
//op.description='hello world';
//op.Name='sundhar';
//op.StageName='discriminating';
//op.CloseDate=system.today();
//insert op;
//Attachment acd = new Attachment();
//acd.name='atach';
//Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
//acd.body=bodyblob;
//acd.parentId=ac.id;
//insert acd;
Test.startTest();
// string businessName = ApexPages.currentPage().getParameters().put('businessValue', lstLineItems[0].name);
//PageReference ref = page.uploadattachemntss;
//Test.setCurrentPage(ref);
uploadattachedfiles upd = new uploadattachedfiles ();
upd.getmyfile();
upd.Savedoc();
list<Attachment> ad=new list<Attachment>();
ad=[select id,name,body from Attachment];
// upd.myfile=ad;
test.stopTest();
}
}
----------------------
error
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name, Body]: [Name, Body]
{
Public Attachment myfile;
Public Attachment getmyfile()
{
myfile = new Attachment();
return myfile;
}
Public Pagereference Savedoc()
{
String accid = System.currentPagereference().getParameters().get('id');
Attachment a = new Attachment();
a.parentId='00128000005yJi7';
a.name=myfile.name;
a.body = myfile.body;
insert a;
return NULL;
}
}
-----------------------------------------------------
@isTest
public class testuploadattachment {
static testmethod void testattached()
{
// account ac=new account();
//ac.name='sanfransico';
//ac.phone='899899898';
//insert ac;
//opportunity op=new opportunity();
//op.description='hello world';
//op.Name='sundhar';
//op.StageName='discriminating';
//op.CloseDate=system.today();
//insert op;
//Attachment acd = new Attachment();
//acd.name='atach';
//Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
//acd.body=bodyblob;
//acd.parentId=ac.id;
//insert acd;
Test.startTest();
// string businessName = ApexPages.currentPage().getParameters().put('businessValue', lstLineItems[0].name);
//PageReference ref = page.uploadattachemntss;
//Test.setCurrentPage(ref);
uploadattachedfiles upd = new uploadattachedfiles ();
upd.getmyfile();
upd.Savedoc();
list<Attachment> ad=new list<Attachment>();
ad=[select id,name,body from Attachment];
// upd.myfile=ad;
test.stopTest();
}
}
----------------------
error
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name, Body]: [Name, Body]
public class uploadattachedfiles
{
Public Attachment myfile;
Public Attachment getmyfile()
{
myfile = new Attachment();
return myfile;
}
Public Pagereference Savedoc()
{
String accid = System.currentPagereference().getParameters().get('id');
Attachment a = new Attachment();
a.parentId= accid; // please dnt pass hard code id. I guess id should come URL
a.name=myfile.name;
a.body = myfile.body;
insert a;
return NULL;
}
}
Then try below test class.
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html
http://amitsalesforce.blogspot.in/search/label/Test%20Class
Please let us know if this will help you
Thanks
AMit Chaudhary
All Answers
before calling upd.Savedoc(); method
you do something like below and mention the
@isTest(seeAllData=true)
public class testuploadattachment {
static testmethod void testattached()
{
Test.startTest();
upd.getmyfile();
Attachment ad=[select id,name,body from Attachment LIMIT 1];
uploadattachedfiles.myfile = ad;
upd.Savedoc();
test.stopTest();
}
}
public class uploadattachedfiles
{
Public Attachment myfile;
Public Attachment getmyfile()
{
myfile = new Attachment();
return myfile;
}
Public Pagereference Savedoc()
{
String accid = System.currentPagereference().getParameters().get('id');
Attachment a = new Attachment();
a.parentId= accid; // please dnt pass hard code id. I guess id should come URL
a.name=myfile.name;
a.body = myfile.body;
insert a;
return NULL;
}
}
Then try below test class.
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html
http://amitsalesforce.blogspot.in/search/label/Test%20Class
Please let us know if this will help you
Thanks
AMit Chaudhary