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
keerthana chowdharykeerthana 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]
Best Answer chosen by keerthana chowdhary
Amit Chaudhary 8Amit Chaudhary 8
I found two issue in your test class and main class

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.
 
@isTest
public class testuploadattachment {
    
static testmethod void testattached()
{
    account ac=new account();
    ac.name='sanfransico';
    ac.phone='899899898';
    insert ac;
    
    Test.startTest();

		PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
		pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
		Test.setCurrentPage(pageRef);
	   
		uploadattachedfiles testAccPlan = new uploadattachedfiles();
		Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body');
		Attachment Att = testAccPlan.getmyfile();
		testAccPlan.myfile.name ='atach';
		testAccPlan.myfile.body =bodyBlob;
		testAccPlan.Savedoc();
    test.stopTest();
   }
}
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

sharathchandra thukkanisharathchandra thukkani
Your upd.getmyfile(); line in test class is returning the attachment new nstance which does not have body and text.
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();
}
}
 
Amit Chaudhary 8Amit Chaudhary 8
I found two issue in your test class and main class

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.
 
@isTest
public class testuploadattachment {
    
static testmethod void testattached()
{
    account ac=new account();
    ac.name='sanfransico';
    ac.phone='899899898';
    insert ac;
    
    Test.startTest();

		PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
		pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
		Test.setCurrentPage(pageRef);
	   
		uploadattachedfiles testAccPlan = new uploadattachedfiles();
		Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body');
		Attachment Att = testAccPlan.getmyfile();
		testAccPlan.myfile.name ='atach';
		testAccPlan.myfile.body =bodyBlob;
		testAccPlan.Savedoc();
    test.stopTest();
   }
}
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

 
This was selected as the best answer
keerthana chowdharykeerthana chowdhary
awesome my test class passed with 100% code coverage love u ...thank u soo much for ur help