I Have Batch apex which will send emails when a article was published yesterday(we have scheduled to run daily). I have written testclass for it,but iam getting only 6% coverage.Please help me in getting max coverage. below is the batch and test class. Test class:
@isTest(SeeAllData=false)
public class Publishedarticaltest {
public static testMethod void testPublishedarticaltest() {
Account Acc = new Account(
Name = 'TestAccount3',
RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Organization').getRecordTypeId());
insert Acc;
Contact con = new Contact(FirstName = 'Test1',Lastname = 'McTesty',AccountId =Acc.Id,Email ='[email protected]');
insert con;
Profile p = [Select Id from Profile where name = 'xyz'];
User u = new User(
Username = '[email protected]',
ProfileId = p.Id,
Alias = 'test123',
Email = '[email protected]',
EmailEncodingKey = 'UTF-8',
LastName = 'McTesty',
TimeZoneSidKey = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
LanguageLocaleKey = 'en_US',
contactid =con.id
);
insert u;
Project__c p1 = new Project__c();
p1.Name='Cessation';
p1.Full_Project_Name__c='health';
insert p1;
HVC__kav kv = new HVC__kav();
kv.Title='test';
kv.urlname='testurl';
kv.Alert_All_Portal_Users__c = true;
kv.Summary= 'test';
kv.IsVisibleInPkb=true;
kv.IsVisibleInCsp=true;
kv.IsVisibleInPrm =true;
kv.project__c=p1.id;
kv.language='en_US';
insert kv;
HVC__kav k = [Select KnowledgeArticleId from HVC__kav where ID = :kv.Id];
KbManagement.PublishingService.publishArticle(k.KnowledgeArticleId, true);
HVC__DataCategorySelection dc= new HVC__DataCategorySelection();
dc.DataCategoryName='Project_Specific_Contracting_Webinars';
dc.ParentId=kv.id;
dc.DataCategoryGroupName='All_HVC_Articles';
insert dc;
Test.startTest();
//Datetime dtBefore24 = System.now().addDays(-1);
// Date dtBeforeday = dtBefore24.date();
// Date dtBeforeday = System.now().date();
// String query='SELECT Id,Title,ArticleNumber,ArticleType,CreatedDate,FirstPublishedDate,LastPublishedDate,OwnerId,Project__r.Name,'+
//'IsDeleted,IsLatestVersion,IsVisibleInApp,IsVisibleInPkb,IsVisibleInPrm,KnowledgeArticleId,Language,'+
// 'Project__c,PublishStatus,UrlName,VersionNumber FROM MHVC__kav WHERE PublishStatus=\'Online\' AND language =\'en_US\' AND IsDeleted = false '+
//'AND IsLatestVersion = true AND Alert_All_Portal_Users__c =true';
PublishedArticleNotificationBatch pBatch = new PublishedArticleNotificationBatch();
Database.executeBatch(pBatch,100);
Test.StopTest();
}
}
Batch class:
global class PublishedArticleNotificationBatch implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext bc) {
Datetime dtBefore24 = System.now().addDays(-1);
Date dtBeforeday = dtBefore24.date();
// Date dtBeforeday = System.now().date();
String query='SELECT Id,Title,ArticleNumber,ArticleType,CreatedDate,FirstPublishedDate,LastPublishedDate,OwnerId,Project__r.Name,'+
'IsDeleted,IsLatestVersion,IsVisibleInApp,IsVisibleInPkb,IsVisibleInPrm,KnowledgeArticleId,Language,'+
'Project__c,PublishStatus,UrlName,VersionNumber FROM HVC__kav WHERE PublishStatus=\'Online\' AND language =\'en_US\' AND IsDeleted = false '+
'AND IsLatestVersion = true AND Alert_All_Portal_Users__c =true AND DAY_ONLY(LastPublishedDate) =: dtBeforeday';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, list<HVC__kav> scope){
List<HVC__kav> publishedArticleList = new List<HVC__kav>();
for(HVC__kav artcle:scope){
publishedArticleList.add(artcle);
}
Map<Id,HVC__DataCategorySelection> dataCatMap = new Map<Id,HVC__DataCategorySelection>([SELECT Id,DataCategoryName,ParentId FROM HVC__DataCategorySelection where ParentId in:publishedArticleList]);
Map<Id,String> catMap = new Map<Id,String>();
for(HVC__DataCategorySelection cat:dataCatMap.values()){
if(catMap.containsKey(cat.ParentId)==false){
catMap.put(cat.ParentId, cat.DataCategoryName);
}
}
if(!publishedArticleList.isEmpty()){
string portalProfile = ApplicationLiterals.protalProfile;
List<string> emailIds =new List<string>();
for(User userObj : [select id,name,FirstName,email from user where profile.Name=:portalProfile and isActive=true]){
emailIds.add(userObj.Email);
}
sendMailToContact(emailIds,publishedArticleList,catMap);
}
}
global void finish(Database.BatchableContext BC){}
public void sendMailToContact(list<string> emailIDs, List<HVC__kav> publishedArticleList, Map<Id,String> catMap){
List<Messaging.singleEmailMessage> mails = new List<Messaging.singleEmailMessage>();
List<OrgWideEmailAddress> owaList = [select id, Address from OrgWideEmailAddress where DisplayName like 'HVC'];
Document LogoDoc = [Select ID from Document where Folder.Name = 'xyz' and DeveloperName = 'xrt' limit 1];
//______Sending email code goes here________//
}
}
}