You need to sign in to do that
Don't have an account?
Test method fails
Hi,
I am facing error while running the test class in a function saveTask().
the query fails and returns no value. please suggest ur opinions. the code is below
test class:
@isTest(seeAllData = false)
public class TransferLeadsTests {
public static testMethod void testTransferLeads() {
Profile p = [SELECT Id,Name FROM Profile Limit 1];
User u = new User(Alias = 'test', Email='test@gmail.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='test@testorg.com');
insert u;
User Userid = [Select id,Name,Email from User where Alias = 'test' and Email = 'test@gmail.com'];
User u1 = [SELECT Id,Name,Email FROM User Where Id = :UserInfo.getUserId()];
RecordType RecordtypeName = [Select Id ,RecordType.Name from RecordType where RecordType.Name =:'Transfer Lead Task' and SObjectType=:'Task' Limit 1];
TransferLeads controller = new TransferLeads() ;
controller.getUser1();
List<Lead> ldlist = new List<Lead>();
Lead ld = new Lead ();
ld.OwnerId = u1.Id;
ld.FirstName= 'testing';
ld.Lastname='test';
ld.Company = 'test';
ld.LTC_Returned_Date__c = system.Today();
ld.RTL_Lead_Assigned__c =system.Now();
ldlist.add(ld);
insert ldlist;
system.debug('AAA'+ldlist.size());
System.AssertEquals(ldlist.size()==1, true);
apexpages.currentpage().getparameters().put('id', ld.Id);
controller.getLead();
apexpages.currentpage().getparameters().put('lecord.OwnerId',Userid.id);
controller.getLeadId();
User unamename = [Select id,Name,Email from User where id=:Userid.id Limit 1];
//User u3 = [Select id, Name from User where id = :Userid.Id Limit 1];
Task t = new Task();
t.Subject ='test';
t.RecordTypeId = RecordtypeName.Id;
t.WhoId =ld.Id;
t.ActivityDate= system.Today();
t.Transfer_agent_s_name__c=ld.Owner.Name;
t.RI_Date_of_Creation__c=system.now();
t.Status='Completed';
ld.OwnerId=unamename.id;
datetime dT =ld.RTL_Lead_Assigned__c.addDays(179);
Date returnDate = date.newinstance(dT.year(), dT.month(), dT.day());
ld.LTC_Returned_Date__c = returnDate ;
//Lead leads = [Select id,Name,LTC_Returned_Date__c,RTL_Lead_Assigned__c from Lead where id =:ld.Id Limit 1];
ld.RTL_Lead_Assigned__c =system.Now();
//ld.OwnerId=Userid.Id;
try{
insert t;
update ld;
system.debug('CCC'+ld.LTC_Returned_Date__c);
System.AssertEquals(ld.LTC_Returned_Date__c!=Null,true);
}
catch(exception e){
system.debug('ZZZ'+ld.LTC_Returned_Date__c);
}
List<task> insertedtask = new List<task> ();
insertedtask = [Select id, Subject,WhoId from Task where WhoId = :ld.id and RecordTypeId =:RecordtypeName.Id Limit 1] ;
system.debug ('GGG' +insertedtask[0].id);
controller.saveTask();
controller.cancel();
}
}
Class:
public with sharing class TransferLeads{
public Lead leadRecord{get;set;}
public User usertransfering{get;set;}
public RecordType RecordtypeName{get;set;}
public String Taskt{get;set;}
public String UId{get;set;}
public string Uname {get;set;}
public String Usertype{get;set;}
public String searchText{get;set;}
public Lead lecord {get;set;}
public User userRecord{get;set;}
public string NewUserID{get;set;}
//public string searchText{get;set;}
public List<User> results{get;set;}
public String NewOwner{get;set;}
public TransferLeads()
{
usertransfering = [Select Name, Id from User Where Id = :UserInfo.getUserId()];
RecordtypeName = [Select Id ,RecordType.Name from RecordType where RecordType.Name =:'Transfer Lead Task' and SObjectType=:'Task' Limit 1];
lecord = new Lead();
}
public Lead getLead()
{
leadRecord =[SELECT Id,Name,OwnerId,FirstName,Lastname,Owner.Name,Owner.Email,LTC_Returned_Date__c,RTL_Lead_Assigned__c FROM Lead WHERE Id =:ApexPages.currentPage().getParameters().get('Id')];
return leadRecord ;
}
public User getUser1(){
return usertransfering;
}
public PageReference cancel()
{
PageReference pageRef = new PageReference('/'+leadRecord.Id);
pageRef.setRedirect(true);
return pageRef;
}
public PageReference saveTask()
{
system.debug('XXXXX'+Uname);
User unamename = [Select id,Name,Email from User where id=:lecord.ownerid Limit 1];
system.debug('RRRRR'+unamename.id);
Task Transfertask = new Task(Subject = Taskt,RecordTypeId = RecordtypeName.Id,WhoId =leadRecord.Id,Transfer_agent_s_name__c=leadRecord.Owner.Name,RI_Date_of_Creation__c=system.now(),ActivityDate= system.Today(),Status='Completed',Receiving_Agent_s_Name__c =unamename.Name);
leadRecord.LTC_Returned_Date__c = Date.today() + 179;
leadRecord.OwnerId=unamename.id;
system.debug('Transfertask.Subject-----------------------------------:' + Transfertask.Subject);
if(Taskt==null || Taskt.length() == 0 )
{
system.debug('Error validation');
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Subject is required'));
return null;
}
try{
insert Transfertask;
update leadRecord;
}
catch (DmlException e) {
System.debug('The following exception has occurred: ' + e.getMessage());
}
PageReference pageRef = new PageReference('/'+leadRecord.Id);
pageRef.setRedirect(true);
return pageRef;
}
}
VF Page:
<apex:page Controller="TransferLeads" sidebar="false" id="page">
<apex:form id="formid">
<apex:pageBlock title="New Task" id="thePageBlock" mode="edit">
<apex:pageMessages ></apex:pageMessages>
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!saveTask}" />
<apex:commandButton value="Cancel" action="{!cancel}" immediate="true"/>
</apex:pageBlockButtons>
<apex:actionRegion >
<apex:pageBlockSection id="pageBlockSection" title="Task Information" columns="1">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Lead Name"/>
<apex:OutputField value="{!Lead.Name}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Current owner"/>
<apex:OutputField value="{!Lead.Owner.Name}"/>
</apex:pageBlockSectionItem>
<apex:inputField value="{!lecord.ownerid}" required="true" / >
<apex:pageblockSectionItem id="taskid" >
<apex:outputLabel value="Subject" />
<apex:outputPanel id="panel" >
<div class="requiredInput">
<div class="requiredBlock"></div>
<apex:inputText label="Subject" value="{!Taskt}" />
</div>
</apex:outputPanel>
</apex:pageblockSectionItem>
<apex:pageblockSectionItem >
<apex:outputLabel value=" Who is Transfering" />
<apex:inputField value="{!User1.Name}"/>
</apex:pageblockSectionItem>
</apex:pageBlockSection>
</apex:actionRegion>
</apex:pageBlock>
</apex:form>
</apex:page>
I am getting below error when i run the test class.
System.QueryException: List has no rows for assignment to SObject
the below line is the cause of error
User unamename = [Select id,Name,Email from User where id=:lecord.ownerid Limit 1];
and the Savetask() call in test class causes the error.
Help me to solve this and achieve 75 %. As of now without SaveTask() function i am bale to achieve 65%.
can anyone help me solve this issue.