You need to sign in to do that
Don't have an account?
Mayank Parnami
We could not find the class 'MaintenanceRequestHelperTest' using assertions in the unit tests. I have all the asserts in place with 100% code coverage, still unable to pass challenge 4, apex superbadge
Test class is as follows(getting passed with 100% coverage) :-
@isTest
public class MaintenanceRequestTest {
static List<Case> caseList1 = new List<Case>();
static List<Product2> prodList = new List<Product2>();
static List<Equipment_Maintenance_Item__c> wpList = new List<Equipment_Maintenance_Item__c>();
@testSetup
static void getData(){
caseList1= CreateData( 300,3,3,'Repair');
System.assertEquals(300, caseList1.size());
}
public static List<Case>
CreateData( Integer numOfcase, Integer numofProd, Integer numofVehicle,
String type){
List<Case> caseList = new List<Case>();
//Create Vehicle
Vehicle__c vc = new Vehicle__c();
vc.name='Test Vehicle';
upsert vc;
//Create Equipment
for(Integer i=0;i<numofProd;i++){
Product2 prod = new Product2();
prod.Name='Test Product'+i;
if(i!=0)
prod.Maintenance_Cycle__c=i;
prod.Replacement_Part__c=true;
prodList.add(prod);
}
system.debug('prodList.size()'+prodList.size());
system.assertEquals(prodList.size(), 3);
upsert prodlist;
//Create Case
for(Integer i=0;i< numOfcase;i++){
Case newCase = new Case();
newCase.Status='New';
newCase.Origin='web';
if( math.mod(i, 2) ==0)
newCase.Type='Routine Maintenance';
else
newCase.Type='Repair';
newCase.Subject='Routine Maintenance of Vehicle' +i;
newCase.Vehicle__c=vc.Id;
if(i<numofProd)
newCase.productid=prodList.get(i).ID;
else
newCase.productid=prodList.get(0).ID;
caseList.add(newCase);
}
system.debug('caseList.size() *'+caseList.size());
system.assertEquals(caseList.size(), 300);
upsert caseList;
//create work part
for(Integer i=0;i<numofProd;i++){
Equipment_Maintenance_Item__c wp = new Equipment_Maintenance_Item__c();
wp.Equipment__c =prodlist.get(i).Id ;
wp.Maintenance_Request__c=caseList.get(i).id;
wplist.add(wp) ;
}
system.debug('wplist.size() *'+wplist.size());
system.assertEquals(wplist.size(), 3);
upsert wplist;
system.debug('caseList *'+caseList);
return caseList;
}
public static testmethod void testMaintenanceHelper(){
Test.startTest();
getData();
for(Case cas: caseList1)
cas.Status ='Closed';
System.assertEquals(300, caseList1.size());
update caseList1;
List<Case> allCases = [SELECT ID,Date_Due__c FROM Case WHERE status = 'New'];
System.assertEquals(600, allCases.size() );
system.debug('allCases in debug'+allCases );
system.debug('allCases in debug'+allCases.size() );
System.debug('getData'+caseList1.size());
Integer i4 =[select count() from case where type='Repair'];
Integer i5 =[select count() from case where type='Routine Maintenance'];
Integer i6 =[select count() from case where type='Electrical'];
System.debug('Test2-'+i4+'--'+i5+'---'+i6);
System.assertEquals(300, i4);
System.assertEquals(600, i5);
System.assertEquals(0, i6);
System.assertEquals(300, caseList1.size());
Test.stopTest();
}
}
Main class:-
public without sharing class MaintenanceRequestHelper {
//System.debug('main ');
public static void updateWorkOrders(Map<Id,Case>applicableCases) {
// TODO: Complete the method to update workorders
System.debug('*******Inside MaintenanceRequestHelper Class*******');
Map<Id, Integer> mapProduct = new Map<Id, Integer>();
List<Case> newCaseList = new List<Case>();
List<Product2> listProduct = [select Id, Maintenance_Cycle__c from Product2];
for (Product2 p : listProduct) {
if (p != null) {
if(p.Maintenance_Cycle__c != null){
mapProduct.put(p.Id, Integer.valueOf(p.Maintenance_Cycle__c));
}
}
}
for(Case a: applicableCases.values()){
Case newCase = new Case();
newCase.Vehicle__c = a.Vehicle__c;
newCase.ProductId= a.ProductId;
newCase.Type = 'Routine Maintenance';
newCase.Subject = String.isBlank(a.Subject) ? 'Routine Maintenance Request' : a.Subject;
newCase.Date_Reported__c = Date.today();
newCase.Status = 'New';
newCase.Product__c = a.Product__c;
newCase.AccountId = a.AccountId;
newCase.ContactId = a.ContactId;
newCase.AssetId = a.AssetId;
newCase.Origin = a.Origin;
newCase.Reason = a.Reason;
newCase.Date_Due__c = (mapProduct.get(a.Id) != null) ? (Date.today().addDays(Integer.valueOf(mapProduct.get(a.Id)))) : (Date.today());
newCaseList.add(newCase);
}
if(newCaseList.size() > 0){
insert newCaseList;
}
}
}
@isTest
public class MaintenanceRequestTest {
static List<Case> caseList1 = new List<Case>();
static List<Product2> prodList = new List<Product2>();
static List<Equipment_Maintenance_Item__c> wpList = new List<Equipment_Maintenance_Item__c>();
@testSetup
static void getData(){
caseList1= CreateData( 300,3,3,'Repair');
System.assertEquals(300, caseList1.size());
}
public static List<Case>
CreateData( Integer numOfcase, Integer numofProd, Integer numofVehicle,
String type){
List<Case> caseList = new List<Case>();
//Create Vehicle
Vehicle__c vc = new Vehicle__c();
vc.name='Test Vehicle';
upsert vc;
//Create Equipment
for(Integer i=0;i<numofProd;i++){
Product2 prod = new Product2();
prod.Name='Test Product'+i;
if(i!=0)
prod.Maintenance_Cycle__c=i;
prod.Replacement_Part__c=true;
prodList.add(prod);
}
system.debug('prodList.size()'+prodList.size());
system.assertEquals(prodList.size(), 3);
upsert prodlist;
//Create Case
for(Integer i=0;i< numOfcase;i++){
Case newCase = new Case();
newCase.Status='New';
newCase.Origin='web';
if( math.mod(i, 2) ==0)
newCase.Type='Routine Maintenance';
else
newCase.Type='Repair';
newCase.Subject='Routine Maintenance of Vehicle' +i;
newCase.Vehicle__c=vc.Id;
if(i<numofProd)
newCase.productid=prodList.get(i).ID;
else
newCase.productid=prodList.get(0).ID;
caseList.add(newCase);
}
system.debug('caseList.size() *'+caseList.size());
system.assertEquals(caseList.size(), 300);
upsert caseList;
//create work part
for(Integer i=0;i<numofProd;i++){
Equipment_Maintenance_Item__c wp = new Equipment_Maintenance_Item__c();
wp.Equipment__c =prodlist.get(i).Id ;
wp.Maintenance_Request__c=caseList.get(i).id;
wplist.add(wp) ;
}
system.debug('wplist.size() *'+wplist.size());
system.assertEquals(wplist.size(), 3);
upsert wplist;
system.debug('caseList *'+caseList);
return caseList;
}
public static testmethod void testMaintenanceHelper(){
Test.startTest();
getData();
for(Case cas: caseList1)
cas.Status ='Closed';
System.assertEquals(300, caseList1.size());
update caseList1;
List<Case> allCases = [SELECT ID,Date_Due__c FROM Case WHERE status = 'New'];
System.assertEquals(600, allCases.size() );
system.debug('allCases in debug'+allCases );
system.debug('allCases in debug'+allCases.size() );
System.debug('getData'+caseList1.size());
Integer i4 =[select count() from case where type='Repair'];
Integer i5 =[select count() from case where type='Routine Maintenance'];
Integer i6 =[select count() from case where type='Electrical'];
System.debug('Test2-'+i4+'--'+i5+'---'+i6);
System.assertEquals(300, i4);
System.assertEquals(600, i5);
System.assertEquals(0, i6);
System.assertEquals(300, caseList1.size());
Test.stopTest();
}
}
Main class:-
public without sharing class MaintenanceRequestHelper {
//System.debug('main ');
public static void updateWorkOrders(Map<Id,Case>applicableCases) {
// TODO: Complete the method to update workorders
System.debug('*******Inside MaintenanceRequestHelper Class*******');
Map<Id, Integer> mapProduct = new Map<Id, Integer>();
List<Case> newCaseList = new List<Case>();
List<Product2> listProduct = [select Id, Maintenance_Cycle__c from Product2];
for (Product2 p : listProduct) {
if (p != null) {
if(p.Maintenance_Cycle__c != null){
mapProduct.put(p.Id, Integer.valueOf(p.Maintenance_Cycle__c));
}
}
}
for(Case a: applicableCases.values()){
Case newCase = new Case();
newCase.Vehicle__c = a.Vehicle__c;
newCase.ProductId= a.ProductId;
newCase.Type = 'Routine Maintenance';
newCase.Subject = String.isBlank(a.Subject) ? 'Routine Maintenance Request' : a.Subject;
newCase.Date_Reported__c = Date.today();
newCase.Status = 'New';
newCase.Product__c = a.Product__c;
newCase.AccountId = a.AccountId;
newCase.ContactId = a.ContactId;
newCase.AssetId = a.AssetId;
newCase.Origin = a.Origin;
newCase.Reason = a.Reason;
newCase.Date_Due__c = (mapProduct.get(a.Id) != null) ? (Date.today().addDays(Integer.valueOf(mapProduct.get(a.Id)))) : (Date.today());
newCaseList.add(newCase);
}
if(newCaseList.size() > 0){
insert newCaseList;
}
}
}
Trailhead Help (https://trailhead.salesforce.com/en/help?support=home)can provide assistance for situations where Trailhead does not appear to be functioning correctly. You can reach out to them if this is the case.
Thanks,