You need to sign in to do that
Don't have an account?
How to write a TestCase for ApexClass
Hi ,Friends and Apex Developers please help me . I created a Apex Class and i write a test Case but it covers only 25% please help me that i have to reach this Coverage level in to 80% at least , it is very urgent for me please do need fully
Base: I create a Customfield in Case reply__c and i create a CustomObject Reply __c then i created a visualforce page in this i created a Submit button then the reply go to the cocerned customer .
here is the class
public class Reply
{
public Case objCase
{ get; set; }
public CaseReply__c objCaseReply
{ get; set; }
public User objUser
{ get; set; }
public String EmailBody
{ get; set; }
public Reply()
{
objCase = [Select Subject,Requestor_Email__c, OwnerId, Id, Description,Priority,CreatedDate, Contact.Email,Contact.Name, CustomerOf__c, Contact_Email_Alias__c, ContactId, CaseNumber,ReplyStatus__c, CaseReply__c From Case where id = :ApexPages.currentPage().getParameters().get('id')];
EmailBody = 'Add your reply here!!!';
}
public void save()
{
//As per email review logic, check manual approval or auto
Case oCase = [Select Id,Subject,ReplyStatus__c From Case where id = :ApexPages.currentPage().getParameters().get('id')];
CaseReply__c oCaseReply = new CaseReply__c();
oCaseReply.AgentReply__c = emailBody ;
oCaseReply.Name = oCase.Subject;
oCaseReply.rCase__c = oCase.Id;
insert oCaseReply;
Boolean checkFlag = ReviewAgentEmail();
if(checkFlag == true){
oCase.ReplyStatus__c = 0;
update oCase;
}else{
oCase.ReplyStatus__c = 2;
update oCase;
//send direct email and mark case as replied
sendEmail();
}
updateEmailCount();
}
public boolean ReviewAgentEmail()
{
User oUser =[Select EmailCount__c from User where Id =:UserInfo.getUserId()];
Double currentEmailcount;
if(oUser.EmailCount__c == null)
{
currentEmailcount = 0;
}
else
{
currentEmailcount = oUser.EmailCount__c;
}
if(currentEmailCount == 10)
{
currentEmailcount = 1;
}
else
{
currentEmailcount = currentEmailcount + 1;
}
EmailReview__c emailRev = [Select DoReview__c, EmailNum__c from EmailReview__c where EmailNum__c =:currentEmailCount and Name =:UserInfo.getUserId()];
if(emailRev.DoReview__c == 1)
{
return true;
}
else
{
return false;
}
}
public void updateEmailCount()
{
User oUser = [Select EmailCount__c from User WHERE id=:UserInfo.getUserId()];
if(oUser.EmailCount__c == 10){
oUser.EmailCount__c = 1;
}else{
oUser.EmailCount__c = oUser.EmailCount__c + 1;
}
update oUser;
}
public void sendEmail()
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] to_Address = new String[]{};
String[] cc_Address = new String[]{};
String[] bcc_Address = new String[]{};
if(objCase.Contact.Email != null)
to_Address.add(objCase.Contact.Email);
if(objCase.Requestor_Email__c != null)
to_Address.add(objCase.Requestor_Email__c);
to_Address.add('ganeshd@cybage.com');
mail.setToAddresses(to_Address);
if(objCase.Contact_Email_Alias__c != null)
cc_Address.add(objCase.Contact_Email_Alias__c);
mail.setCcAddresses(cc_Address);
User objUser = [Select Email from User where Id =:UserInfo.getUserId()];
if(objUser.Email != null)
mail.setReplyTo(objUser.Email);
mail.setSaveAsActivity(true);
mail.setSubject(objCase.Subject);
mail.setHTMLBody(emailBody);
mail.setPlainTextBody(emailBody);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
Advance thanks to all
Can you post your test case? Its a bit difficult to suggest changes when we don't know which 25% is covered :smileywink:
@isTest
private class TestPostReply {
static testMethod void TestPostReply() {
contact objcontact = new contact();
objcontact.firstname = 'testCon';
insert objcontact;
case objecase = new case();
objcase.subject = "test";
objcase.Requestor_Email__c = "test@test.com";
objcase.Description = 'test';
objcase.priority = 'high';
objcase.CreatedDate = datetime.now();
objcase.ReplyStatus__c = 0.0;
objCase.
objcase.CaseReply__c = "test";
objcase.contactid = objcontact.id;
insert objcase;
AgentReply clsAgentReplay = new AgentReply();
clsAgentReplay.EmailBody = "Test";
clsAgentReplay.objCase = objcase;
clsAgentReplay.save();
User oUser = new User();
oUser.EmailCount__c = 1;
oUser.Email='testuser@test.com';
..........;
..........;
...........;
...........;
insert oUser;
Double currentEmailcount;
Boolean ReviewAgentEmail;
if(oUser.EmailCount__c == null)
{
currentEmailcount = 0;
}
else
{
currentEmailcount = oUser.EmailCount__c;
}
if(currentEmailCount == 10)
{
currentEmailcount = 1;
}
else
{
currentEmailcount = currentEmailcount + 1;
}
EmailReview__c emailRev = new EmailReview__c();
emailRev.DoReview__c = 0;
emailRev.EmailNum__c = 1;
.......................;
.......................;
Insert emailRev;
if(emailRev.DoReview__c == 1)
{
return true;
}
else
{
return false;
}
Boolean checkFlag = ReviewAgentEmail;
if(checkFlag == true){
objCase.ReplyStatus__c = 0;
update objCase;
}else{
objCase.ReplyStatus__c = 2;
update objCase;
//send direct email and mark case as replied
// sendEmail();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] to_Address = new String[]{};
String[] cc_Address = new String[]{};
String[] bcc_Address = new String[]{};
if(objCase.Contact.Email != null)
to_Address.add(objCase.Contact.Email);
if(objCase.Requestor_Email__c != null)
to_Address.add(objCase.Requestor_Email__c);
to_Address.add('ganeshd@cybage.com');
mail.setToAddresses(to_Address);
if(objCase.Contact_Email_Alias__c != null)
cc_Address.add(objCase.Contact_Email_Alias__c);
mail.setCcAddresses(cc_Address);
if(objUser.Email != null)
mail.setReplyTo(objUser.Email);
mail.setSaveAsActivity(true);
mail.setSubject(objCase.Subject);
mail.setHTMLBody(emailBody);
mail.setPlainTextBody(emailBody);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
//updateEmailCount();
if(oUser.EmailCount__c == 10)
{
oUser.EmailCount__c = 1;
}
else
{
oUser.EmailCount__c = oUser.EmailCount__c + 1;
}
update oUser;
}
}