You need to sign in to do that
Don't have an account?
Epic Fail on Test Method... Urgent help needed
I have cobbled together a Test Method (my first one!) by reading what I could from here and other places. I have no idea why it isnt giving me higher code coverage, but I am guessing someone out there can help me! With what I have listed below, I get only 2% coverage. I highlighted the only purple line in my code coverage result, all the other lines are either comments or red.
Here is my test code as is:
@isTest
private with sharing class TestgetAllRecords {
static testMethod void myTest(){
List<Opportunity> oList = new List<Opportunity>{};
test.startTest();
date testdate = date.parse('12/31/2020');
Account testaccount = new Account();
testaccount.Name = 'test';
insert testaccount;
Contact testcontact = new Contact();
testcontact.LastName = 'test';
testcontact.AccountId = testaccount.Id;
insert testcontact;
Opportunity testrenewal = new Opportunity();
testrenewal.accountid = testaccount.Id;
testrenewal.Amount = 100.00;
testrenewal.Closedate = testdate;
testrenewal.Description = 'test';
testrenewal.Name = 'test';
testrenewal.Stagename = 'Baseline';
testrenewal.Probability = 100;
testrenewal.OwnerId = UserInfo.getUserId();
testrenewal.Coverage_Start_Date__c = testdate;
testrenewal.Coverage_End_Date__c = testdate;
insert testrenewal;
Asset_Information__c testasset = new Asset_Information__c();
testasset.Name = 'test';
testasset.Maintenance_Renewal__c = testrenewal.Id;
testasset.Coverage_Start_Date__c = testdate;
testasset.Coverage_End_Date__c = testdate;
insert testasset;
oList = [SELECT Name, Contract_Owner__c, Amount, CloseDate FROM Opportunity WHERE Name = 'test'];
test.stopTest();
ApexPages.StandardController sc1 = new ApexPages.StandardController(testrenewal);
getAllRecords controller1 = new getAllRecords(sc1);
}
}
and here is my actual class:
public class getAllRecords {
public getAllRecords(ApexPages.StandardController Controller) {
}
//method to get all maint. renewals
public Opportunity[] getRecordDetail(){
//declare and instantiate recordList
Opportunity[]recordList = new List<Opportunity>();
//SOQL call to get all the records
recordList = [SELECT Name,
Contract_Owner__c,
Amount,
CloseDate
FROM Opportunity
WHERE AccountId IN
(SELECT AccountId
From User
WHERE username=:UserInfo.getUsername())
AND (Stagename = 'Baseline' OR Stagename = 'Review' OR Stagename = 'Execution')
ORDER BY CloseDate];
return recordList;
}
//method to strip out pastdue to 5 days from close_date
public Opportunity[] getDuePastDue(){
List<Opportunity> recordList = getRecordDetail();
List<Opportunity> pastDueList = new List<Opportunity>();
for(Opportunity obj:recordList) {
if(obj.CloseDate < (system.today() +5)) {
obj.Name = obj.Name.toUpperCase();
if(obj.Contract_Owner__c != Null) {
obj.Contract_Owner__c = obj.Contract_Owner__c.toUpperCase();
}
pastDueList.add(obj);
}
}
return pastDueList;
}
//method to strip out 5-30 days from close_date
public Opportunity[] getFiveThirty(){
List<Opportunity> recordList = getRecordDetail();
List<Opportunity> fiveThirtyList = new List<Opportunity>();
for(Opportunity obj4:recordList) {
if((obj4.CloseDate > (system.today() + 5)) && (obj4.CloseDate <= (system.today() + 30))) {
fiveThirtyList.add(obj4);
}
}
return fiveThirtyList;
}
//method to strip out over 60 days from close date
public Opportunity[] getOverSixty(){
List<Opportunity> recordList = getRecordDetail();
List<Opportunity> overSixtyList = new List<Opportunity>();
for(Opportunity obj2:recordList) {
if(obj2.CloseDate > (system.today() + 60)) {
overSixtyList.add(obj2);
}
}
return overSixtyList;
}
//method to strip out 30-60 days from close date
public Opportunity[] getThirtySixty(){
List<Opportunity> recordList = getRecordDetail();
List<Opportunity> thirtySixtyList = new List<Opportunity>();
for(Opportunity obj3:recordList) {
if((obj3.CloseDate <= (system.today() + 60)) && (obj3.CloseDate > (system.today() + 30))) {
thirtySixtyList.add(obj3);
}
}
return thirtySixtyList;
}
// get a company logo: based on user log in, strip spaces, return image name
public String getImageName(){
String file = [SELECT Name
FROM Account
WHERE Id IN
(SELECT AccountId
FROM user
WHERE username=:UserInfo.getUsername()
)
LIMIT 1
].Name;
file = file.replace(' ', '');
String foldername = 'Logos/';
String extension = '.jpg';
String fullImageName = foldername + file + extension;
return fullImageName;
}
}
Thank you!!!
Hi
U didin't pass any method in test class which are in class try to pass them with controller1.
Regards,
Rajesh.
Rajesh-
Thank you. But HOW do I do that? Can you provide a sample syntax? How do I add the four missing methods to the test? From a syntax standpoint, I am completely lost.
Thanks again!
Ah! Here I am at 63% covered! I just need a little more help!
My Test Method:
@isTest
private with sharing class TestgetAllRecords {
static testMethod void myTest(){
List<Opportunity> oList = new List<Opportunity>{};
date testdate = date.parse('12/31/2020');
Account testaccount = new Account();
testaccount.Name = 'test';
insert testaccount;
Contact testcontact = new Contact();
testcontact.LastName = 'test';
testcontact.AccountId = testaccount.Id;
insert testcontact;
Opportunity testrenewal = new Opportunity();
testrenewal.accountid = testaccount.Id;
testrenewal.Amount = 100.00;
testrenewal.Closedate = testdate;
testrenewal.Description = 'test';
testrenewal.Name = 'test';
testrenewal.Stagename = 'Baseline';
testrenewal.Probability = 100;
testrenewal.OwnerId = UserInfo.getUserId();
testrenewal.Coverage_Start_Date__c = testdate;
testrenewal.Coverage_End_Date__c = testdate;
insert testrenewal;
ApexPages.StandardController sc1 = new ApexPages.StandardController(testrenewal);
getAllRecords controller1 = new getAllRecords(sc1);
controller1.getRecordDetail();
controller1.getDuePastDue();
controller1.getFiveThirty();
controller1.getOverSixty();
controller1.getThirtySixty();
controller1.getImageName();
}
}
And my class:
public class getAllRecords {
public getAllRecords(ApexPages.StandardController Controller) {
}
//method to get all maint. renewals
public Opportunity[] getRecordDetail(){
//declare and instantiate recordList
Opportunity[]recordList = new List<Opportunity>();
//SOQL call to get all the records
recordList = [SELECT Name,
Contract_Owner__c,
Amount,
CloseDate
FROM Opportunity
WHERE AccountId IN
(SELECT AccountId
From User
WHERE username=:UserInfo.getUsername())
AND (Stagename = 'Baseline' OR Stagename = 'Review' OR Stagename = 'Execution')
ORDER BY CloseDate];
return recordList;
}
//method to strip out pastdue to 5 days from close_date
public Opportunity[] getDuePastDue(){
List<Opportunity> recordList = getRecordDetail();
List<Opportunity> pastDueList = new List<Opportunity>();
for(Opportunity obj:recordList) {
if(obj.CloseDate < (system.today() +5)) {
obj.Name = obj.Name.toUpperCase();
if(obj.Contract_Owner__c != Null) {
obj.Contract_Owner__c = obj.Contract_Owner__c.toUpperCase();
}
pastDueList.add(obj);
}
}
return pastDueList;
}
//method to strip out 5-30 days from close_date
public Opportunity[] getFiveThirty(){
List<Opportunity> recordList = getRecordDetail();
List<Opportunity> fiveThirtyList = new List<Opportunity>();
for(Opportunity obj4:recordList) {
if((obj4.CloseDate > (system.today() + 5)) && (obj4.CloseDate <= (system.today() + 30))) {
fiveThirtyList.add(obj4);
}
}
return fiveThirtyList;
}
//method to strip out over 60 days from close date
public Opportunity[] getOverSixty(){
List<Opportunity> recordList = getRecordDetail();
List<Opportunity> overSixtyList = new List<Opportunity>();
for(Opportunity obj2:recordList) {
if(obj2.CloseDate > (system.today() + 60)) {
overSixtyList.add(obj2);
}
}
return overSixtyList;
}
//method to strip out 30-60 days from close date
public Opportunity[] getThirtySixty(){
List<Opportunity> recordList = getRecordDetail();
List<Opportunity> thirtySixtyList = new List<Opportunity>();
for(Opportunity obj3:recordList) {
if((obj3.CloseDate <= (system.today() + 60)) && (obj3.CloseDate > (system.today() + 30))) {
thirtySixtyList.add(obj3);
}
}
return thirtySixtyList;
}
public String getImageName(){
String file = [SELECT Name
FROM Account
WHERE Id IN
(SELECT AccountId
FROM user
WHERE username=:UserInfo.getUsername()
)
LIMIT 1
].Name;
file = file.replace(' ', '');
String foldername = 'Logos/';
String extension = '.jpg';
String fullImageName = foldername + file + extension;
return fullImageName;
}
}
My failure alert:
System.QueryException: List has no rows for assignment to SObject Class.getAllRecords.getImageName
and I am at 63% covered.
PLEASE ... any help is appreciated.
Thank you all very much.
Without looking at your code coverage I would guess that some of the lines inside of an if statement are not being covered. What you need to do is make sure that you have test data that ensures that the if statement evaluates to true, then the lines will be covered. This may mean that you need to have two or more sets of data and/or function calls. If you have any questions or this is not the case, let me know.
You are spot on with the IF statements not being covered. I scanning the posts now trying to find some sample code to work with, but I am coming up with nada. Here is the uncovered code:
and here is my test method:
does this help?
Thanks!
OK, I looked a little closer and I realized that you are getting an error. From what I can tell this is throwing the error:
From the error message I would assume that the SOQL query there is not returning any records so when you try to dereference it (with .name) there is nothing there and it throws an error. You need to specifically set the AccountID on a user to your ID I think. I am pretty sure that there is a way to run the test in the context of another user, but I have never done that so you will have to look it up. You can confirm all my assumptions in the debug log if you want. It should show you which SOQL statement is returning 0 records and that would be the one that is causing you trouble, I do think that it is this one though as that is what the error message implies
Hi Jake.
I think you are pointing me in the right direction. Here is my debug log:
Debug Log:
I am not sure what I am looking for exactly. So I amended my test data as such:
adding the green line to solve the id issue, and I got this failure warning:
TestGetAllRecords: Field is not writeable:Account Id
Any suggestions?
Thanks!
Yes, you are trying to write to the account ID field that is not the field that I believe is causing you problems. In the query:
You are looking for user.AccountID and checking for an account with that ID. From the sales force developer docs that field is:
"ID of the Account associated with a Customer Portal user.
This field is null for Salesforce users."
What that means is that the field is null since tests are generally run in system mode without a specific user context. If you are sure that that is the field that you need to use, then you will have to run the test in the context of another user. See here:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_tools_runas.htm
Then you can create a new portal user, associate it with the account that you are creating in the test class and then run the test as that user. Let me know what happens.
I added the code as suggested by the link you provided.
It changed nothing.
I agree that there is an issue with my data not being present, therefore there is nothing for the IF statements to evaluate.
The method in question is indeed directed to Partner Portal users to capture the name of the company, strip out the white spaces of their company name and sandwich it between Logos/(added variable from query).jpg so I can add an image to my VF page based on the log in of the partner portal user. It works when tested in the sandbox, but I cant deploy this to my production org without the Test method working.
I am at my wits end. I have been working on this for two weeks.
Do you have any syntax that you can add to help me out?
Thank you!
It sucks, but you just have to dig through it. For Example:
You can see in the query that it is looking at HubSpot_Intelligence__c so you may have to insert a record into there so that there is a contact__c that matches your newly created contact. Essentially, you just look at things like that till your querys are returning data, then if your if's still are not covered, you have to check and make sure that the data you are entering evualates to TRUE so that the code inside the statement will run. It is hard to give you exact code though because I do not even know what that object is or does or even if I am on the right track on where contact__c comes from. I can show you one shortcut though that will make your code easier to read. This:
can be rewritten as this:
It is good practice as well as it will save you statements towards your governor limits in longer pieces of code. If you have specific questions on this, let me know and I will do what I can to help.
Hi
In ur test class try to add the opputunity like below
Opportunity testrenewal = new Opportunity();
testrenewal.accountid = testaccount.Id;
testrenewal.Amount = 100.00;
testrenewal.name='test';
testrenewal.Contract_Owner__c= //here u put the owner of the contract id.
testrenewal.Closedate = testdate;
testrenewal.Description = 'test';
testrenewal.Name = 'test';
testrenewal.Stagename = 'Baseline';
testrenewal.Probability = 100;
testrenewal.OwnerId = UserInfo.getUserId();
testrenewal.Coverage_Start_Date__c = testdate;
testrenewal.Coverage_End_Date__c = testdate;
insert testrenewal;
Regards,
Rajesh.