You need to sign in to do that
Don't have an account?
in my test Class i am not getting coverage for Soql query
I shall be grateful to you if you take time and could help me in finding problem. i am not able to get the code coverage for my soql query in test class....
My Apex Class
Public class Invoice_details {
Public List<VoiceWrapper> InvoiceList {get;set;}
Public kognoz1__Invoice2__c InvoiceInfo { get;set;}
Public static Date StartDate {get;set;}
Public static Date EndDate {get;set;}
Public static string querystr;
Public integer counter = 0;
Public integer list_size=100;
Static set<Id> ids = New set<Id>();
Public Invoice_details() {
InvoiceInfo = new kognoz1__Invoice2__c();
InvoiceList = New List<VoiceWrapper>();
for(kognoz1__Invoice2__c k : [SELECT id, kognoz1__Invoice_Date__c, kognoz1__Account__c, Downloaded__c, kognoz1__Opportunity__r.rechnung_per_email__c , kognoz1__Opportunity__c, (select id, name from Attachments) FROM kognoz1__Invoice2__c ORDER BY kognoz1__Invoice_Date__c DESC limit: List_size ])
{
InvoiceList.add(new VoiceWrapper(K));
}
}
Public void display() {
InvoiceList.clear();
Date startDate = InvoiceInfo.kognoz1__Invoice_Date__c;
Date endDate = InvoiceInfo.kognoz1__Invoice_Due_Date__c;
if(InvoiceInfo.kognoz1__Invoice_Date__c != null ) {
InvoiceList = New List<VoiceWrapper>();
for(kognoz1__Invoice2__c k : [SELECT Id, kognoz1__Invoice_Date__c, kognoz1__Account__c, Downloaded__c, kognoz1__Opportunity__r.rechnung_per_email__c, kognoz1__Opportunity__c, (select id, name from Attachments) FROM kognoz1__Invoice2__c WHERE kognoz1__Invoice_Date__c >=: startDate AND kognoz1__Invoice_Date__c <=: EndDate ORDER BY kognoz1__Invoice_Date__c DESC limit: list_size offset: counter ])
{
InvoiceList.add(new VoiceWrapper(K));
}
}
}
Public void emailLists() {
InvoiceList.clear();
Date startDate = InvoiceInfo.kognoz1__Invoice_Date__c;
Date endDate = InvoiceInfo.kognoz1__Invoice_Due_Date__c;
if(InvoiceInfo.kognoz1__Invoice_Date__c != null ) {
InvoiceList = New List<VoiceWrapper>();
for(kognoz1__Invoice2__c k : [SELECT Id, kognoz1__Invoice_Date__c, kognoz1__Account__c, Downloaded__c, kognoz1__Opportunity__r.rechnung_per_email__c, kognoz1__Opportunity__c, (select id, name from Attachments) FROM kognoz1__Invoice2__c WHERE kognoz1__Invoice_Date__c >=: startDate AND kognoz1__Invoice_Date__c <=: EndDate AND kognoz1__Opportunity__r.rechnung_per_email__c like '%' ORDER BY kognoz1__Invoice_Date__c DESC limit: list_size offset: counter ])
{
InvoiceList.add(new VoiceWrapper(K));
}
}
}
Public void emailListsStatus() {
InvoiceList.clear();
Date startDate = InvoiceInfo.kognoz1__Invoice_Date__c;
Date endDate = InvoiceInfo.kognoz1__Invoice_Due_Date__c;
if(InvoiceInfo.kognoz1__Invoice_Date__c != null ) {
InvoiceList = New List<VoiceWrapper>();
for(kognoz1__Invoice2__c k : [SELECT Id, kognoz1__Invoice_Date__c, kognoz1__Account__c, Downloaded__c, kognoz1__Opportunity__r.rechnung_per_email__c, kognoz1__Opportunity__c, (select id, name from Attachments) FROM kognoz1__Invoice2__c WHERE Downloaded__c=false AND kognoz1__Invoice_Date__c >=: startDate AND kognoz1__Invoice_Date__c <=: EndDate AND kognoz1__Opportunity__r.rechnung_per_email__c like '%' ORDER BY kognoz1__Invoice_Date__c DESC limit: list_size offset: counter ])
{
InvoiceList.add(new VoiceWrapper(K));
}
}
}
Public void emailListsStatustrue() {
InvoiceList.clear();
Date startDate = InvoiceInfo.kognoz1__Invoice_Date__c;
Date endDate = InvoiceInfo.kognoz1__Invoice_Due_Date__c;
if(InvoiceInfo.kognoz1__Invoice_Date__c != null ) {
InvoiceList = New List<VoiceWrapper>();
for(kognoz1__Invoice2__c k : [SELECT Id, kognoz1__Invoice_Date__c, kognoz1__Account__c, Downloaded__c, kognoz1__Opportunity__r.rechnung_per_email__c, kognoz1__Opportunity__c, (select id, name from Attachments) FROM kognoz1__Invoice2__c WHERE Downloaded__c=true AND kognoz1__Invoice_Date__c >=: startDate AND kognoz1__Invoice_Date__c <=: EndDate AND kognoz1__Opportunity__r.rechnung_per_email__c like '%' ORDER BY kognoz1__Invoice_Date__c DESC limit: list_size offset: counter ])
{
InvoiceList.add(new VoiceWrapper(K));
}
}
}
Public void PostLists() {
InvoiceList.clear();
Date startDate = InvoiceInfo.kognoz1__Invoice_Date__c;
Date endDate = InvoiceInfo.kognoz1__Invoice_Due_Date__c;
if(InvoiceInfo.kognoz1__Invoice_Date__c != null ) {
InvoiceList = New List<VoiceWrapper>();
for(kognoz1__Invoice2__c k : [SELECT Id, kognoz1__Invoice_Date__c, kognoz1__Account__c, Downloaded__c, kognoz1__Opportunity__r.rechnung_per_email__c, kognoz1__Opportunity__c, (select id, name from Attachments) FROM kognoz1__Invoice2__c WHERE kognoz1__Invoice_Date__c >=: startDate AND kognoz1__Invoice_Date__c <=: EndDate AND kognoz1__Opportunity__r.rechnung_per_email__c = null ORDER BY kognoz1__Invoice_Date__c DESC limit: list_size offset: counter ])
{
InvoiceList.add(new VoiceWrapper(K));
}
}
}
Public void PostListsStauts() {
InvoiceList.clear();
Date startDate = InvoiceInfo.kognoz1__Invoice_Date__c;
Date endDate = InvoiceInfo.kognoz1__Invoice_Due_Date__c;
if(InvoiceInfo.kognoz1__Invoice_Date__c != null ) {
InvoiceList = New List<VoiceWrapper>();
for(kognoz1__Invoice2__c k : [SELECT Id, kognoz1__Invoice_Date__c, kognoz1__Account__c, Downloaded__c, kognoz1__Opportunity__r.rechnung_per_email__c, kognoz1__Opportunity__c, (select id, name from Attachments) FROM kognoz1__Invoice2__c WHERE Downloaded__c=false AND kognoz1__Invoice_Date__c >=: startDate AND kognoz1__Invoice_Date__c <=: EndDate AND kognoz1__Opportunity__r.rechnung_per_email__c = null ORDER BY kognoz1__Invoice_Date__c DESC limit: list_size offset: counter ])
{
InvoiceList.add(new VoiceWrapper(K));
}
}
}
Public void PostListsStautstrue() {
InvoiceList.clear();
Date startDate = InvoiceInfo.kognoz1__Invoice_Date__c;
Date endDate = InvoiceInfo.kognoz1__Invoice_Due_Date__c;
if(InvoiceInfo.kognoz1__Invoice_Date__c != null ) {
InvoiceList = New List<VoiceWrapper>();
for(kognoz1__Invoice2__c k : [SELECT Id, kognoz1__Invoice_Date__c, kognoz1__Account__c, Downloaded__c, kognoz1__Opportunity__r.rechnung_per_email__c, kognoz1__Opportunity__c, (select id, name from Attachments) FROM kognoz1__Invoice2__c WHERE Downloaded__c=true AND kognoz1__Invoice_Date__c >=: startDate AND kognoz1__Invoice_Date__c <=: EndDate AND kognoz1__Opportunity__r.rechnung_per_email__c = null ORDER BY kognoz1__Invoice_Date__c DESC limit: list_size offset: counter ])
{
InvoiceList.add(new VoiceWrapper(K));
}
}
}
Public void displayDownload(){
InvoiceList.clear();
Date startDate = InvoiceInfo.kognoz1__Invoice_Date__c;
Date endDate = InvoiceInfo.kognoz1__Invoice_Due_Date__c;
if(InvoiceInfo.kognoz1__Invoice_Date__c != null ) {
InvoiceList = New List<VoiceWrapper>();
for(kognoz1__Invoice2__c k : [SELECT Id, kognoz1__Invoice_Date__c, kognoz1__Account__c, Downloaded__c, kognoz1__Opportunity__r.rechnung_per_email__c, kognoz1__Opportunity__c, (select id, name from Attachments) FROM kognoz1__Invoice2__c WHERE Downloaded__c=false AND kognoz1__Invoice_Date__c >=: startDate AND kognoz1__Invoice_Date__c <=: EndDate ORDER BY kognoz1__Invoice_Date__c DESC limit: list_size offset: counter ])
{
InvoiceList.add(new VoiceWrapper(K));
}
}
}
Public class VoiceWrapper {
Public kognoz1__Invoice2__c KI {get;set;}
Public boolean selected {get;set;}
Public VoiceWrapper (kognoz1__Invoice2__c K) {
KI = k;
selected = false;
system.debug(KI);
}
}
Public Pagereference Previous() {
counter = counter-list_size;
display();
return null;
}
Public pagereference Next() {
counter = counter + list_size;
display();
system.debug( InvoiceList);
return null;
}
Public boolean getDisablePrevious() {
if (counter<=0) {
return True;
}
else return false;
}
Public Boolean getDisableNext() {
if (counter > = InvoiceList.size()-list_size) {
return true;
}
else return false;
}
}
test class
@isTest(seeAllData=true)
public class Invoice_detailsTestClass {
public List<VoiceWrapper> InvoiceList {get;set;}
public kognoz1__Invoice2__c InvoiceInfo { get;set;}
public static Date StartDate {get;set;}
public static Date EndDate {get;set;}
public boolean selected{get;set;}
public static integer counter = 0;
Public static integer list_size=20;
Public integer total_size ;
static testMethod void Invoice_details() {
list<kognoz1__Invoice2__c> InvoiceList = [SELECT id, kognoz1__Invoice_Date__c, kognoz1__Account__c, kognoz1__Opportunity__r.rechnung_per_email__c, kognoz1__Opportunity__c, (select id, name from Attachments) FROM kognoz1__Invoice2__c];
Account testAcco = TestData.createAccounts(1, 'Publisher', 'Horst')[0];
insert testAcco;
Opportunity opp = TestData.createOpportunity();
opp.AccountId = testAcco.Id;
opp.StageName = 'New';
insert opp;
Test.StartTest();
ApexPages.currentPage().getParameters().put('id', String.valueOf(TestAcco.Id));
Invoice_details testAccPlan = new Invoice_details();
testAccplan.display();
testAccplan.Previous();
testAccplan.Next();
testAccplan.getDisablePrevious();
testAccplan.getDisableNext();
kognoz1__Invoice2__c K = new kognoz1__Invoice2__c();
Invoice_details.VoiceWrapper TV = new Invoice_details.VoiceWrapper( k);
Test.StopTest();
}
static testMethod void testdisplay() {
DAte StartDate = Date.newInstance(2016, 12, 9);
Date endDate = Date.newInstance(2016, 12, 9);
list<kognoz1__Invoice2__c> InvoiceList = [SELECT id, kognoz1__Invoice_Date__c, kognoz1__Account__c, kognoz1__Opportunity__r.rechnung_per_email__c, kognoz1__Opportunity__c, (select id, name from Attachments) FROM kognoz1__Invoice2__c WHERE kognoz1__Invoice_Date__c >=: startDate AND kognoz1__Invoice_Date__c <=: EndDate ORDER BY kognoz1__Invoice_Date__c DESC limit: list_size offset: counter];
Account testAcco = TestData.createAccounts(1, 'Publisher', 'Horst')[0];
insert testAcco;
Opportunity opp = TestData.createOpportunity();
opp.AccountId = testAcco.Id;
opp.StageName = 'New';
opp.rechnung_per_email__c = 'abv@gmail.com';
insert opp;
kognoz1__Invoice2__c KI = new kognoz1__Invoice2__c();
KI.kognoz1__Opportunity__c = opp.Id;
KI.kognoz1__Account__c = testAcco.Id;
KI.kognoz1__Invoice_Date__c = StartDate;
KI.kognoz1__Invoice_Due_Date__c = EndDate;
insert KI;
system.debug(KI);
Test.StartTest();
ApexPages.currentPage().getParameters().put('id', String.valueOf(TestAcco.Id));
Invoice_details AccPlan = new Invoice_details();
Accplan.display();
Accplan.Previous();
Accplan.Next();
Accplan.getDisablePrevious();
Accplan.getDisableNext();
kognoz1__Invoice2__c K = new kognoz1__Invoice2__c();
Invoice_details.VoiceWrapper TV = new Invoice_details.VoiceWrapper(k);
Test.StopTest();
}
static testMethod void testdisplayDownload(){
list<kognoz1__Invoice2__c> InvoiceList = [SELECT id, kognoz1__Invoice_Date__c, kognoz1__Account__c, kognoz1__Opportunity__r.rechnung_per_email__c, kognoz1__Opportunity__c, (select id, name from Attachments) FROM kognoz1__Invoice2__c];
Account testAcco = TestData.createAccounts(1, 'Publisher', 'Horst')[0];
insert testAcco;
Opportunity opp = TestData.createOpportunity();
opp.AccountId = testAcco.Id;
opp.StageName = 'New';
insert opp;
kognoz1__Invoice2__c ki = new kognoz1__Invoice2__c();
KI.kognoz1__Opportunity__c = opp.Id;
ki.kognoz1__Invoice_Date__c = Date.newInstance(2016, 12, 9);
ki.Downloaded__c=true;
insert KI;
Ki.Downloaded__c=false;
update KI;
Test.StartTest();
// ApexPages.currentPage().getParameters().put('id', String.valueOf(Ki.Id));
Invoice_details ID = new Invoice_details();
ID.displayDownload();
kognoz1__Invoice2__c K = new kognoz1__Invoice2__c();
// Invoice_details.VoiceWrapper TV = new Invoice_details.VoiceWrapper(k);
Test.StopTest();
}
static testMethod void testemailLists(){
list<kognoz1__Invoice2__c> InvoiceList = [SELECT id, kognoz1__Invoice_Date__c, kognoz1__Account__c, kognoz1__Opportunity__r.rechnung_per_email__c, kognoz1__Opportunity__c, (select id, name from Attachments) FROM kognoz1__Invoice2__c];
Account testAcco = TestData.createAccounts(1, 'Publisher', 'Horst')[0];
insert testAcco;
Opportunity opp = TestData.createOpportunity();
opp.AccountId = testAcco.Id;
opp.StageName = 'New';
insert opp;
kognoz1__Invoice2__c ki = new kognoz1__Invoice2__c();
KI.kognoz1__Opportunity__c = opp.Id;
ki.kognoz1__Invoice_Date__c = Date.newInstance(2016, 12, 9);
ki.Downloaded__c=true;
insert KI;
Ki.Downloaded__c=false;
update KI;
Test.StartTest();
// ApexPages.currentPage().getParameters().put('id', String.valueOf(Ki.Id));
Invoice_details ID = new Invoice_details();
kognoz1__Invoice2__c K = new kognoz1__Invoice2__c();
ID.emailLists();
Invoice_details.VoiceWrapper TV = new Invoice_details.VoiceWrapper(k);
Test.StopTest();
}
static testMethod void testPostLists(){
list<kognoz1__Invoice2__c> InvoiceList = [SELECT id, kognoz1__Invoice_Date__c, kognoz1__Account__c, kognoz1__Opportunity__r.rechnung_per_email__c, kognoz1__Opportunity__c, (select id, name from Attachments) FROM kognoz1__Invoice2__c];
Account testAcco = TestData.createAccounts(1, 'Publisher', 'Horst')[0];
insert testAcco;
Opportunity opp = TestData.createOpportunity();
opp.AccountId = testAcco.Id;
opp.StageName = 'New';
insert opp;
kognoz1__Invoice2__c ki = new kognoz1__Invoice2__c();
KI.kognoz1__Opportunity__c = opp.Id;
ki.kognoz1__Invoice_Date__c = Date.newInstance(2016, 12, 9);
ki.Downloaded__c=true;
insert KI;
Ki.Downloaded__c=false;
update KI;
Test.StartTest();
// ApexPages.currentPage().getParameters().put('id', String.valueOf(Ki.Id));
Invoice_details ID = new Invoice_details();
ID.PostLists();
Test.StopTest();
}
private class VoiceWrapper{
Public kognoz1__Invoice2__c KI {get;set;}
Public boolean selected {get;set;}
Public VoiceWrapper (kognoz1__Invoice2__c K) {
KI = k;
selected = false;
system.debug(KI);
}
}
}
for the constructor i got complete coverage but i am not getting soql coverage for the methods. I shall be grateful to you for your kind cooperation. Thanks :)
1) writting query in test method is not a good idea its a good pratic to creat the test data in test class it self
anyways if in you case if ok to write that query like that that try setting up test.run as admin
Profile p = [SELECT Id FROM Profile WHERE Name=<system admin>];
user u = [select id form user where ProfileId = P.id limit 1);
System.runAs(u) {
Note: - you have to modify this code its just for reference
sure i am Looking into it
Hi Nishant
1) why are you setting "ApexPages.currentPage().getParameters().put('id', String.valueOf(TestAcco.Id));" in you test class when you are not even getting the URL paramter anywhere in you main class
Anyways if you need to set the context of a VF page before you want to run your code please add the follwing line
PageReference pageRef = Page.<your VF page Name>; // Add your VF page Name here
pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
Test.setCurrentPage(pageRef);
2) can you please post a small screen shot of which line is covred and which line is not
3) can put a debug log as mentioned below and let me knwo what it is prinitng when you run the test
Public void display() {
InvoiceList.clear();
Date startDate = InvoiceInfo.kognoz1__Invoice_Date__c;
Date endDate = InvoiceInfo.kognoz1__Invoice_Due_Date__c;
system.debug('!^&%#%#%##!'+InvoiceInfo);
system.debug('!^&%#%#%##!'+InvoiceInfo.kognoz1__Invoice_Date__c );
system.debug('!^&%#%#%##!'+startDate);
system.debug('!^&%#%#%##!'+EndDate );
if(InvoiceInfo.kognoz1__Invoice_Date__c != null ) {
InvoiceList = New List<VoiceWrapper>();
for(kognoz1__Invoice2__c k : [SELECT Id, kognoz1__Invoice_Date__c, kognoz1__Account__c, Downloaded__c, kognoz1__Opportunity__r.rechnung_per_email__c, kognoz1__Opportunity__c, (select id, name from Attachments) FROM kognoz1__Invoice2__c WHERE kognoz1__Invoice_Date__c >=: startDate AND kognoz1__Invoice_Date__c <=: EndDate ORDER BY kognoz1__Invoice_Date__c DESC limit: list_size offset: counter ])
{
InvoiceList.add(new VoiceWrapper(K));
}
}
}
3)
if i could tell you in short, only these lines in everymethod soql query
InvoiceList = New List<VoiceWrapper>();
for(kognoz1__Invoice2__c k : [SELECT Id, kognoz1__Invoice_Date__c, kognoz1__Account__c, Downloaded__c, kognoz1__Opportunity__r.rechnung_per_email__c, kognoz1__Opportunity__c, (select id, name from Attachments) FROM kognoz1__Invoice2__c WHERE Downloaded__c=false AND kognoz1__Invoice_Date__c >=: startDate AND kognoz1__Invoice_Date__c <=: EndDate AND kognoz1__Opportunity__r.rechnung_per_email__c = null ORDER BY kognoz1__Invoice_Date__c DESC limit: list_size offset: counter ])
{
InvoiceList.add(new VoiceWrapper(K));
}
are not being covered
it is not pass that IF condtion (i.e. if(InvoiceInfo.kognoz1__Invoice_Date__c != null )
which means either "InvoiceInfo" is null or InvoiceInfo.kognoz1__Invoice_Date__c is definalty comeing null
i can see in constructer you mentiond that
Public Invoice_details() {
InvoiceInfo = new kognoz1__Invoice2__c();
// do this automatically populate kognoz1__Invoice_Date__c field ??
ok
i update test method and updates are highlited in bold try this if this work
it can fail if you query on "kognoz1__Invoice2__c" in test class do not return any data, for better solucation i will suggent that you insert record for kognoz1__Invoice2__c in test calss it self
static testMethod void Invoice_details()
{
list<kognoz1__Invoice2__c> InvoiceList = [SELECT id, kognoz1__Invoice_Date__c, kognoz1__Account__c, kognoz1__Opportunity__r.rechnung_per_email__c, kognoz1__Opportunity__c, (select id, name from Attachments) FROM kognoz1__Invoice2__c where kognoz1__Invoice_Date__c != null];
Account testAcco = TestData.createAccounts(1, 'Publisher', 'Horst')[0];
insert testAcco;
Opportunity opp = TestData.createOpportunity();
opp.AccountId = testAcco.Id;
opp.StageName = 'New';
insert opp;
Test.StartTest();
ApexPages.currentPage().getParameters().put('id', String.valueOf(TestAcco.Id));
Invoice_details testAccPlan = new Invoice_details();
system.debug("^!*&#!*&#^!*^#!'+InvoiceList[0]);
system.debug("^!*&#!*&#^!*^#!'+InvoiceList[0]);
system.debug("^!*&#!*&#^!*^#!'+InvoiceList[0]);
testAccPlan.InvoiceInfo = InvoiceList [0];
testAccPlan.StartDate = InvoiceList [0].kognoz1__Invoice_Date__c;
testAccPlan.EndDate = InvoiceList [0].kognoz1__Invoice_Date__c;
testAccplan.display();
testAccplan.Previous();
testAccplan.Next();
testAccplan.getDisablePrevious();
testAccplan.getDisableNext();
kognoz1__Invoice2__c K = new kognoz1__Invoice2__c();
Invoice_details.VoiceWrapper TV = new Invoice_details.VoiceWrapper( k);
Test.StopTest();
}
Hi the first method is working properly, just display() and other method is not working controller is giving 100% coverage but not giving coverage for methods, like void display(), void displaydownload() etc. only methods are not giving coverage,Illegal assignment from Invoice_detailsTestClass.VoiceWrapper to kognoz1__Invoice2__c
Variable does not exist: kognoz1__Invoice_Date__c
Static field cannot be referenced from a non static context: StartDate from the type Invoice_details
Static field cannot be referenced from a non static context: EndDate from the type Invoice_details
after implementing your methods.......
why StartDate and enddate is static can you make them non static please
Public static Date StartDate {get;set;}
Public static Date EndDate {get;set;}
2) also did you added this in you test calss
PageReference pageRef = Page.<your VF page Name>; // Add your VF page Name here
pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
Test.setCurrentPage(pageRef);
3) can you add share the new test calss and main class afte you made these changes
yes the value of InvoiceInfo.kognoz1__Invoice_Date__c value is Null, if i remove the if condition then it covers the upto the soql query but does not cover the {
InvoiceList.add(new VoiceWrapper(K));
}
However i can not remove if else condition, so how can i deal this InvoiceInfo.kognoz1__Invoice_Date__c value and after that how can i cover this Invoicelist.addd(new VoiceWrapper(k)); (VoiceWrapper is a wrapper class);
If you like to mark it solved go ahed
have a good day