function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Travis - GHTravis - GH 

Unit test for inner/wrapper class

Hi all! I need to write test class for wrapper class coverage. There is my controller:
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
 
Best Answer chosen by Travis - GH
Sandhyarani S PSandhyarani S P
Controller : 

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

Sandhyarani S PSandhyarani S P
Controller : 

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();  
        }
    }
This was selected as the best answer
Travis - GHTravis - GH
Thank you man. That one works fine.