I'm new to Salesforce. Creating Test class for a trigger isn't as tough as creating test class for simple class that only has a dml and a query. The line "clInfo.GetAll();" is giving me 100% code coverage but I am not sure if this is the right way of testing this class. My test clss doesn't have the inserting of records, the query of these objects nor assertions but it gives 100% with just "clInfo.GetAll();". Is it the right way?
@RestResource(urlMapping='/info')
global with sharing class clInfo{
@HttpGet
global static Wrapper GetAll(){
Wrapper wrap = new Wrapper('', '', null, null);
String orgId = UserInfo.getOrganizationId(); // retrieve org ID
String orgName = UserInfo.getOrganizationName(); // retrieve org Name
datetime dt = System.now()-1; // use to filter results for System Overview query
// query for the Installed Packages and System Overview
List<Installed_Package__c> packageList = [SELECT Name, Version__c, License__c, Date_Installed__c, Date_Updated__c FROM Installed_Package__c];
List<System_Overview__c> sysOv = [SELECT Max_API__c, Requested_API__c, Usable_API__c, Total_Storage__c, Used_Storage__c, Remaining_Storage__c, Retrieve_DateTime__c FROM System_Overview__c where Retrieve_DateTime__c > :dt];
wrap.packages=packageList;
wrap.system_overview=sysOv;
wrap.orgId = orgId;
wrap.orgName = orgName;
return wrap;
}
global class Wrapper{
public String orgId{get;set;}
public String orgName{get;set;}
public List<Installed_Package__c> packages{get; set;}
public List<System_Overview__c> system_overview{get; set;}
}}
Here's my test class:
@isTest
global class GetSysOvClassTest {
@isTest static void testHere() {
clInfo.GetAll();
}}
I have updated my test class into this:
@isTest static void testHere() {
//created records here
//inserted the records here
datetime dt = System.now()-1;
List<Installed_Package__c> SFpackRetrieve = [SELECT ID FROM Installed_Package__c]; //query the records
List<System_Overview__c> SFsysOvRetrieve = [SELECT ID FROM System_Overview__c WHERE Retrieve_DateTime__c > :dt]; //query the records
clInfo.Wrapper testWrap = new clInfo.Wrapper('00D6F000002KtVEUA0 ', 'Org Client', SFpackRetrieve, SFsysOvRetrieve);
System.assertEquals(2, SFpackRetrieve.size());
System.assertEquals(1, SFsysOvRetrieve.size());
}}
I removed the "clInfo.GetAll();" line in the test class and now I have 42% code coverage. Will it be alright to add this line (clInfo.GetAll();) once again? My GetAll() method isn't covered. I add that line, it goes 100%.