You need to sign in to do that
Don't have an account?
Nishant Shrivastava 30
i am not able to see my code coverage
I have two classes and i am quite puzzle to write their test class and not getting any coverage for them
first Apex Class
global class WS_ZipUtil {
/**
* Receive Attachments info from Attachment ParentId
*/
webService static String getAttachmentById( String sfdcId ){
if( String.isEmpty( sfdcId ) ) return WS_Util.errorJson('Parameter sfdcId is required.');
List<Attachment> attachmentList = [SELECT Id, Name, Body, ContentType FROM Attachment WHERE Id = :sfdcId];
if( attachmentList == null || attachmentList.size() == 0 ) return WS_Util.errorJson('Attachment not found.');
return wrapAttachmentList( attachmentList );
}
//Format JSON String from AttachmentList
private static String wrapAttachmentList( List<Attachment> attachmentList ){
List<Object> dataList = new List<Object>();
for( Attachment at : attachmentList ){
Map<String, String> atMap = new Map<String, String>();
//atMap.put( 'Id', at.Id );
atMap.put( 'Name', at.Name );
atMap.put( 'Body', EncodingUtil.base64Encode( at.Body ) );
//atMap.put( 'ContentType', at.ContentType );
dataList.add( atMap );
}
return WS_Util.normalJson( dataList );
}
}
Second Apex Class
public class WS_Util {
//Normal Status Code
private static String API_STATUS_NORMAL = '200';
//Error Status Code
private static String API_STATUS_ERROR = '400';
/**
* Normal JSON Response
*/
public static String normalJson( Object respData ) {
Map<String, Object> response = new Map<String, Object>();
response.put('status', API_STATUS_NORMAL);
if( respData != null ) response.put('data', respData);
return JSON.serialize( response );
}
/**
* Error JSON Response
*/
public static String errorJson( String message ) {
Map<String, Object> response = new Map<String, Object>();
response.put('status', API_STATUS_ERROR);
if( message != null ) response.put('error', message);
return JSON.serialize( response );
}
}
first Apex Class
global class WS_ZipUtil {
/**
* Receive Attachments info from Attachment ParentId
*/
webService static String getAttachmentById( String sfdcId ){
if( String.isEmpty( sfdcId ) ) return WS_Util.errorJson('Parameter sfdcId is required.');
List<Attachment> attachmentList = [SELECT Id, Name, Body, ContentType FROM Attachment WHERE Id = :sfdcId];
if( attachmentList == null || attachmentList.size() == 0 ) return WS_Util.errorJson('Attachment not found.');
return wrapAttachmentList( attachmentList );
}
//Format JSON String from AttachmentList
private static String wrapAttachmentList( List<Attachment> attachmentList ){
List<Object> dataList = new List<Object>();
for( Attachment at : attachmentList ){
Map<String, String> atMap = new Map<String, String>();
//atMap.put( 'Id', at.Id );
atMap.put( 'Name', at.Name );
atMap.put( 'Body', EncodingUtil.base64Encode( at.Body ) );
//atMap.put( 'ContentType', at.ContentType );
dataList.add( atMap );
}
return WS_Util.normalJson( dataList );
}
}
Second Apex Class
public class WS_Util {
//Normal Status Code
private static String API_STATUS_NORMAL = '200';
//Error Status Code
private static String API_STATUS_ERROR = '400';
/**
* Normal JSON Response
*/
public static String normalJson( Object respData ) {
Map<String, Object> response = new Map<String, Object>();
response.put('status', API_STATUS_NORMAL);
if( respData != null ) response.put('data', respData);
return JSON.serialize( response );
}
/**
* Error JSON Response
*/
public static String errorJson( String message ) {
Map<String, Object> response = new Map<String, Object>();
response.put('status', API_STATUS_ERROR);
if( message != null ) response.put('error', message);
return JSON.serialize( response );
}
}
Use the below test class you will get the 100% for both the Classes:
@istest
public class WS_ZipUtil_Test {
static testmethod void TestAttachmentById()
{
Account a = new Account(Name='newAcct');
Test.startTest();
insert a;
Attachment attach=new Attachment();
attach.Name='Unit Test Attachment';
Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
attach.body=bodyBlob;
attach.parentId=a.Id;
attach.ContentType = 'application/msword';
attach.IsPrivate = false;
attach.Description = 'Test';
insert attach;
String resp = WS_ZipUtil.getAttachmentById(attach.Id);
}
static testmethod void TestAttachmentByNullId()
{
Id sfdcId;
String resp = WS_ZipUtil.getAttachmentById(sfdcId);
}
}
Thanks,
Maharajan.C