Skip to content

Instantly share code, notes, and snippets.

@olgaloza
Last active April 16, 2018 18:12
Show Gist options
  • Save olgaloza/3837bc1cf814205d40d07aeea9e7d4e7 to your computer and use it in GitHub Desktop.
Save olgaloza/3837bc1cf814205d40d07aeea9e7d4e7 to your computer and use it in GitHub Desktop.
Final Project - Test Class
@isTest
private class StockItemHandler_Test {
@isTest static void testBeforeInsertFunctionality() {
try {
// Implement test code
// to test, try creating 2 items with same names.
Stock_Item__c item1 = new Stock_Item__c(Item_Name__c = 'Test Stock Item');
Stock_Item__c item2 = new Stock_Item__c(Item_Name__c = 'Test Stock Item');
/* FOR SOME REASON I COULDN'T GET THIS TEST VERSION TO WORK
List<Stock_Item__c> stockItemsList = new List<Stock_Item__c>();
//Loop through and add stock items:
for (Integer i = 0; i < 10; i++) {
Stock_Item__c si = new Stock_Item__c();
si.Item_Name__c = 'Test Stock Item';
si.Description__c = 'Lorem ipsum dolor sit amet ' + (i+1);
si.List_Price__c = (i+1)+(i+1);
si.Minimum_Stock_Level__c = 1;
si.Stock_on_Hand__c = (i+1)+(i+1);
stockItemsList.add(si);
} //Inserting all in one action:
insert stockItemsList;
List<Stock_Item__c> checkList = new List<Stock_Item__c>(
[SELECT Id, Item_Name__c FROM Stock_Item__c WHERE Item_Name__c = 'Test Stock Item']
);
System.assertEquals(1, checkList.size());
*/
// insert stockItemsList;
insert item1;
insert item2;
} catch(Exception error) {
System.assert(error.getMessage().contains('Duplicate name found'));
}
}
@isTest static void testBeforeDeleteFunctionality() {
// Implement test code
//To test, delete a stock item with inventory>0 and see if case was created
//First, create a test stock item with inventory>0
Stock_Item__c item = new Stock_Item__c(Item_Name__c = 'Test Stock Item', Stock_on_Hand__c = 10);
insert item;
//now try deleting the Item we just created
delete [SELECT Id FROM Stock_Item__c WHERE Id = :item.Id];
//check if a Case was created because deleting an item with inventory
Case checkCase = [SELECT Id FROM Case WHERE Subject LIKE '%Test Stock Item%'];
System.assertEquals(true, checkCase!=NULL);
}
@isTest static void testGetLowStockItems() {
// Implement test code
// To test, create test stock item with low inventory
Stock_Item__c item = new Stock_Item__c(Item_Name__c = 'Test Stock Item', Stock_on_Hand__c = 1, Minimum_Stock_Level__c =10);
insert item;
//I was COMPLETELY STUCK ON THE NEXT LINE, and had to get help from a developer at work
List<Stock_Item__c> lowStockItems = StockItemHandler.getLowStockItems();
System.assertEquals(1, lowStockItems.size());
}
@isTest static void packageUploadCoverage() {
Stock_Item__c item = new Stock_Item__c();
item.Item_Name__c = 'Test';
item.Description__c = 'Test';
item.Minimum_Stock_Level__c =3;
insert item;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment