You need to sign in to do that
Don't have an account?

Unit test for inner/wrapper class
Hi all! I need to write test class for wrapper class coverage. There is my controller:
Thanx
public class StoreFrontController { List<DisplayMerchandise> products; public List<DisplayMerchandise> getProducts() { if(products == null) { products = new List<DisplayMerchandise>(); for(Merchandise__c item : [ SELECT Id, Name, Description__c, Price__c, Total_Inventory__c FROM Merchandise__c]) { products.add(new DisplayMerchandise(item)); } } return products; } // Inner class to hold online store details for item public class DisplayMerchandise { private Merchandise__c merchandise; public DisplayMerchandise(Merchandise__c item) { this.merchandise = item; } // Properties for use in the Visualforce view public String name { get { return merchandise.Name; } } public String description { get { return merchandise.Description__c; } } public Decimal price { get { return merchandise.Price__c; } } public Boolean inStock { get { return (0 < merchandise.Total_Inventory__c); } } public Integer qtyToBuy { get; set; } } }What Unit Test can I do for my inner class ?
Thanx
public class StoreFrontController {
List<DisplayMerchandise> products;
public List<DisplayMerchandise> getProducts() {
if(products == null) {
products = new List<DisplayMerchandise>();
for(Merchandise__c item : [
SELECT Id, Name, Description__c, Price__c, Total_Inventory__c
FROM Merchandise__c]) {
products.add(new DisplayMerchandise(item));
}
}
return products;
}
// Inner class to hold online store details for item
public class DisplayMerchandise {
@TestVisible private Merchandise__c merchandise;
public DisplayMerchandise(Merchandise__c item) {
this.merchandise = item;
}
// Properties for use in the Visualforce view
@TestVisible public String name {
get { return merchandise.Name; }
}
@TestVisible public String description {
get { return merchandise.Description__c; }
}
@TestVisible public Decimal price {
get { return merchandise.Price__c; }
}
@TestVisible public Boolean inStock {
get { return (0 < merchandise.Total_Inventory__c); }
}
public Integer qtyToBuy { get; set; }
}
}
Test Class :
@isTest(seeAllData=false)
public class StoreFrontControllerTest{
static testMethod void myUnitTest(){
Merchandise__c mar = new Merchandise__c();
mar.name = 'test';
mar.Description__c ='test1';
mar.Price__c= 200.00;
mar.Total_Inventory__c=200;
mar.Quantity__c = 10;
insert mar;
Test.startTest();
StoreFrontController ctlrs = new StoreFrontController();
ctlrs.getProducts();
String name = mar.Name;
StoreFrontController.DisplayMerchandise mars= new StoreFrontController.DisplayMerchandise(mar);
mars.merchandise = mar;
mars.qtyToBuy = 0;
mars.name = name;
mars.price = 200.00;
mars.inStock =true;
mars.description = 'test';
Test.stopTest();
}
}
All Answers
public class StoreFrontController {
List<DisplayMerchandise> products;
public List<DisplayMerchandise> getProducts() {
if(products == null) {
products = new List<DisplayMerchandise>();
for(Merchandise__c item : [
SELECT Id, Name, Description__c, Price__c, Total_Inventory__c
FROM Merchandise__c]) {
products.add(new DisplayMerchandise(item));
}
}
return products;
}
// Inner class to hold online store details for item
public class DisplayMerchandise {
@TestVisible private Merchandise__c merchandise;
public DisplayMerchandise(Merchandise__c item) {
this.merchandise = item;
}
// Properties for use in the Visualforce view
@TestVisible public String name {
get { return merchandise.Name; }
}
@TestVisible public String description {
get { return merchandise.Description__c; }
}
@TestVisible public Decimal price {
get { return merchandise.Price__c; }
}
@TestVisible public Boolean inStock {
get { return (0 < merchandise.Total_Inventory__c); }
}
public Integer qtyToBuy { get; set; }
}
}
Test Class :
@isTest(seeAllData=false)
public class StoreFrontControllerTest{
static testMethod void myUnitTest(){
Merchandise__c mar = new Merchandise__c();
mar.name = 'test';
mar.Description__c ='test1';
mar.Price__c= 200.00;
mar.Total_Inventory__c=200;
mar.Quantity__c = 10;
insert mar;
Test.startTest();
StoreFrontController ctlrs = new StoreFrontController();
ctlrs.getProducts();
String name = mar.Name;
StoreFrontController.DisplayMerchandise mars= new StoreFrontController.DisplayMerchandise(mar);
mars.merchandise = mar;
mars.qtyToBuy = 0;
mars.name = name;
mars.price = 200.00;
mars.inStock =true;
mars.description = 'test';
Test.stopTest();
}
}