You need to sign in to do that
Don't have an account?
llisa
Hello , some one please help me writing a test class on this.
public with sharing class KHistoryController{
//Protected Members
private static final DescribeSObjectResult oSomeObjectSchema = Schema.SObjectType.K_Document__c;
private static final Map<string, Schema.SObjectField> mapFields = oSomeObjectSchema.fields.getMap();
//Properties
public Id SomeObjectId {get;set;}
public integer PageSize {get;set;}
public boolean AllowShowMore {get;set;}
public List<SomeObjectHistory> SomeObjectHistories {
get { return getSomeObjectHistory(SomeObjectId); }
}
//Constructors
/**
* Default Constructor
*/
public KHistoryController() {
PageSize = 5;
AllowShowMore = true;
}
//Public Methods
public void showMore() {
PageSize += 5;
}
//Private Methods
/**
* Returns SomeObject History records associated to the current SomeObject
*
* @param SomeObjectId the SomeObject__c record id to retrieve
* @return a list of SomeObjectHistory objects
*/
private List<SomeObjectHistory> getSomeObjectHistory(Id SomeObjectId) {
List<SomeObjectHistory> listSomeObjectHistory = new List<SomeObjectHistory>();
if (SomeObjectId != null) {
DateTime dLastCreatedDate = null;
integer limitPlusOne = PageSize + 1;
List<K_Document__History> listEntityHistory = [SELECT Id, Field, NewValue, OldValue, CreatedDate, CreatedById, CreatedBy.Name FROM K_Document__History WHERE ParentId = :SomeObjectId ORDER BY CreatedDate DESC, Id DESC LIMIT :limitPlusOne];
AllowShowMore = (listEntityHistory.size() == limitPlusOne);
for (K_Document__History oHistory : listEntityHistory) {
SomeObjectHistory oSomeObjectHistory = new SomeObjectHistory(oHistory);
if (mapFields.containsKey(oHistory.Field)) {
oSomeObjectHistory.FieldLabel = mapFields.get(oHistory.Field).getDescribe().Label;
}
if (dLastCreatedDate == oHistory.CreatedDate) {
oSomeObjectHistory.ShowDateAndUser = false;
}
else {
oSomeObjectHistory.ShowDateAndUser = true;
}
listSomeObjectHistory.add(oSomeObjectHistory);
dLastCreatedDate = oHistory.CreatedDate;
if (listSomeObjectHistory.size() == PageSize) break;
}
}
return listSomeObjectHistory;
}
//Internal Classes
/**
* Data structure representing a SomeObject History record for display
*/
public class SomeObjectHistory {
//Properties
public boolean ShowDateAndUser {get;set;}
public string FieldLabel {get;set;}
public K_Document__History History {get; private set;}
public string ActionFormat {
get { return getActionFormat(); }
}
public SomeObjectHistory(K_Document__History oHistory) {
History = oHistory;
}
//Constructors
public SomeObjectHistory() {
showDateAndUser = true;
}
//Private Methods
private string getActionFormat() {
string sActionFormat = '';
if (History != null) {
sActionFormat = 'Record {0}.';
if (History.newValue != null && History.oldValue == null) {
sActionFormat = 'Changed <strong>{1}</strong> to <strong>{3}</strong>.';
}
else if (History.newValue != null && History.oldValue != null) {
sActionFormat = 'Changed <strong>{1}</strong> from {2} to <strong>{3}</strong>.';
}
else if (History.Field != null && History.Field.equalsIgnoreCase('created')) {
sActionFormat = 'Created.';
}
}
return sActionFormat;
}
}
}
//Protected Members
private static final DescribeSObjectResult oSomeObjectSchema = Schema.SObjectType.K_Document__c;
private static final Map<string, Schema.SObjectField> mapFields = oSomeObjectSchema.fields.getMap();
//Properties
public Id SomeObjectId {get;set;}
public integer PageSize {get;set;}
public boolean AllowShowMore {get;set;}
public List<SomeObjectHistory> SomeObjectHistories {
get { return getSomeObjectHistory(SomeObjectId); }
}
//Constructors
/**
* Default Constructor
*/
public KHistoryController() {
PageSize = 5;
AllowShowMore = true;
}
//Public Methods
public void showMore() {
PageSize += 5;
}
//Private Methods
/**
* Returns SomeObject History records associated to the current SomeObject
*
* @param SomeObjectId the SomeObject__c record id to retrieve
* @return a list of SomeObjectHistory objects
*/
private List<SomeObjectHistory> getSomeObjectHistory(Id SomeObjectId) {
List<SomeObjectHistory> listSomeObjectHistory = new List<SomeObjectHistory>();
if (SomeObjectId != null) {
DateTime dLastCreatedDate = null;
integer limitPlusOne = PageSize + 1;
List<K_Document__History> listEntityHistory = [SELECT Id, Field, NewValue, OldValue, CreatedDate, CreatedById, CreatedBy.Name FROM K_Document__History WHERE ParentId = :SomeObjectId ORDER BY CreatedDate DESC, Id DESC LIMIT :limitPlusOne];
AllowShowMore = (listEntityHistory.size() == limitPlusOne);
for (K_Document__History oHistory : listEntityHistory) {
SomeObjectHistory oSomeObjectHistory = new SomeObjectHistory(oHistory);
if (mapFields.containsKey(oHistory.Field)) {
oSomeObjectHistory.FieldLabel = mapFields.get(oHistory.Field).getDescribe().Label;
}
if (dLastCreatedDate == oHistory.CreatedDate) {
oSomeObjectHistory.ShowDateAndUser = false;
}
else {
oSomeObjectHistory.ShowDateAndUser = true;
}
listSomeObjectHistory.add(oSomeObjectHistory);
dLastCreatedDate = oHistory.CreatedDate;
if (listSomeObjectHistory.size() == PageSize) break;
}
}
return listSomeObjectHistory;
}
//Internal Classes
/**
* Data structure representing a SomeObject History record for display
*/
public class SomeObjectHistory {
//Properties
public boolean ShowDateAndUser {get;set;}
public string FieldLabel {get;set;}
public K_Document__History History {get; private set;}
public string ActionFormat {
get { return getActionFormat(); }
}
public SomeObjectHistory(K_Document__History oHistory) {
History = oHistory;
}
//Constructors
public SomeObjectHistory() {
showDateAndUser = true;
}
//Private Methods
private string getActionFormat() {
string sActionFormat = '';
if (History != null) {
sActionFormat = 'Record {0}.';
if (History.newValue != null && History.oldValue == null) {
sActionFormat = 'Changed <strong>{1}</strong> to <strong>{3}</strong>.';
}
else if (History.newValue != null && History.oldValue != null) {
sActionFormat = 'Changed <strong>{1}</strong> from {2} to <strong>{3}</strong>.';
}
else if (History.Field != null && History.Field.equalsIgnoreCase('created')) {
sActionFormat = 'Created.';
}
}
return sActionFormat;
}
}
}
I have updated the test class :
Monalisa, It is very hard to guess and write a code for a class which we dont know what it is performing.
So i am just assuming that the code provided above will increase coverage of your code.
If it does not help you than i can help you by debugging your code in your Organisation.
So if it is possible for you than please share your org credentials.
You can also reach me at abhishek.bansal@metacube.com OR SkypeId : abhishek.bansal2790 if you need any help.
Regards,
Abhishek.
All Answers
Please use the below test class :
Please make sure to run it by clicking "Run Test" button after saving the test class.
Regards,
Abhishek.
these underline lines are not covered
public with sharing class KHistoryController{
//Protected Members
private static final DescribeSObjectResult oSomeObjectSchema = Schema.SObjectType.K_Document__c;
private static final Map<string, Schema.SObjectField> mapFields = oSomeObjectSchema.fields.getMap();
//Properties
public Id SomeObjectId {get;set;}
public integer PageSize {get;set;}
public boolean AllowShowMore {get;set;}
public List<SomeObjectHistory> SomeObjectHistories {
get { return getSomeObjectHistory(SomeObjectId); }
}
//Constructors
/**
* Default Constructor
*/
public KHistoryController() {
PageSize = 5;
AllowShowMore = true;
}
//Public Methods
public void showMore() {
PageSize += 5;
}
//Private Methods
/**
* Returns SomeObject History records associated to the current SomeObject
*
* @param SomeObjectId the SomeObject__c record id to retrieve
* @return a list of SomeObjectHistory objects
*/
private List<SomeObjectHistory> getSomeObjectHistory(Id SomeObjectId) {
List<SomeObjectHistory> listSomeObjectHistory = new List<SomeObjectHistory>();
if (SomeObjectId != null) {
DateTime dLastCreatedDate = null;
integer limitPlusOne = PageSize + 1;
List<K_Document__History> listEntityHistory = [SELECT Id, Field, NewValue, OldValue, CreatedDate, CreatedById, CreatedBy.Name FROM K_Document__History WHERE ParentId = :SomeObjectId ORDER BY CreatedDate DESC, Id DESC LIMIT :limitPlusOne];
AllowShowMore = (listEntityHistory.size() == limitPlusOne);
for (K_Document__History oHistory : listEntityHistory) {
SomeObjectHistory oSomeObjectHistory = new SomeObjectHistory(oHistory);
if (mapFields.containsKey(oHistory.Field)) {
oSomeObjectHistory.FieldLabel = mapFields.get(oHistory.Field).getDescribe().Label;
}
if (dLastCreatedDate == oHistory.CreatedDate) {
oSomeObjectHistory.ShowDateAndUser = false;
}
else {
oSomeObjectHistory.ShowDateAndUser = true;
}
listSomeObjectHistory.add(oSomeObjectHistory);
dLastCreatedDate = oHistory.CreatedDate;
if (listSomeObjectHistory.size() == PageSize) break;
}
}
return listSomeObjectHistory;
}
//Internal Classes
/**
* Data structure representing a SomeObject History record for display
*/
public class SomeObjectHistory {
//Properties
public boolean ShowDateAndUser {get;set;}
public string FieldLabel {get;set;}
public K_Document__History History {get; private set;}
public string ActionFormat {
get { return getActionFormat(); }
}
public SomeObjectHistory(K_Document__History oHistory) {
History = oHistory;
}
//Constructors
public SomeObjectHistory() {
showDateAndUser = true;
}
//Private Methods
private string getActionFormat() {
string sActionFormat = '';
if (History != null) {
sActionFormat = 'Record {0}.';
if (History.newValue != null && History.oldValue == null) {
sActionFormat = 'Changed <strong>{1}</strong> to <strong>{3}</strong>.';
}
else if (History.newValue != null && History.oldValue != null) {
sActionFormat = 'Changed <strong>{1}</strong> from {2} to <strong>{3}</strong>.';
}
else if (History.Field != null && History.Field.equalsIgnoreCase('created')) {
sActionFormat = 'Created.';
}
}
return sActionFormat;
}
}
}
Change your test class with below code : Please run the test class again and see the code coverage.
Regards,
Abhishek
To : List<KHistoryController.SomeObjectHistory> testMethodCalling = testController.SomeObjectHistories;
If that also does not work than please let me know i will provide you another solution.
Abhishek.
It is working now and it is giving 42% code coverage.
those codes are not yet covered-
/**
* Data structure representing a SomeObject History record for display
*/
public class SomeObjectHistory {
//Properties
public boolean ShowDateAndUser {get;set;}
public string FieldLabel {get;set;}
public K_Document__History History {get; private set;}
public string ActionFormat {
get { return getActionFormat(); }
}
public SomeObjectHistory(K_Document__History oHistory) {
History = oHistory;
}
//Constructors
public SomeObjectHistory() {
showDateAndUser = true;
}
//Private Methods
private string getActionFormat() {
string sActionFormat = '';
if (History != null) {
sActionFormat = 'Record {0}.';
if (History.newValue != null && History.oldValue == null) {
sActionFormat = 'Changed <strong>{1}</strong> to <strong>{3}</strong>.';
}
else if (History.newValue != null && History.oldValue != null) {
sActionFormat = 'Changed <strong>{1}</strong> from {2} to <strong>{3}</strong>.';
}
else if (History.Field != null && History.Field.equalsIgnoreCase('created')) {
sActionFormat = 'Created.';
}
}
return sActionFormat;
}
}
}
Abhishek
it will give the same 42% code coverage.
by adding that line 14 it is giving 0% coverage, here the code is-
@isTest
private class test_KHistoryControllerTest
{
static testMethod void test_KHistoryController1()
{
K_Document__c k1 = new K_Document__c();
k1.note__c = 'abcde';
k1.Order_Number__c = '123';
insert k1;
k1.note__c = 'ABCD';
update k1;
//K_Document__History testHistroyrecord = new K_Document__History(id = '123');
//List<K_Document__History> testHistroyrecord = new List<K_Document__History>();//Please add any required field if it shows error
//testHistroyrecord.Name = 'Test History';
//insert testHistroyrecord;
Test.startTest();
KHistoryController testController = new KHistoryController();
testController.SomeObjectId = k1.id;
testController.showMore();
List<KHistoryController.SomeObjectHistory> testMethodCalling = testController.SomeObjectHistories;
Test.stopTest();
}
}
I have updated the test class :
Monalisa, It is very hard to guess and write a code for a class which we dont know what it is performing.
So i am just assuming that the code provided above will increase coverage of your code.
If it does not help you than i can help you by debugging your code in your Organisation.
So if it is possible for you than please share your org credentials.
You can also reach me at abhishek.bansal@metacube.com OR SkypeId : abhishek.bansal2790 if you need any help.
Regards,
Abhishek.