+ Start a Discussion
NervosaNervosa 

Writing test class - still 0% of coverage

Greetings to everyone!

 

I've got a problem writing test for my Apex controller. Here is controller:

public class Fullfunctionality_2 {
 
   private List<Item__c> items;
   private String sortDirection = 'ASC';
   private String sortExp = 'name';
   public String NewItemName {get; set;}
   public Integer NewItemQuantity { get; set; }
   public Integer NewItemPrice { get; set; }
   public String NewItemType { get; set; }
   public Date NewItemReleaseDate { get; set; }
   public String IdToDel { get; set; }
   public string searchText {get;set;}
   public List<Item__c> searchResults {get;set;}

   public List<SelectOption> getTypes(){
       List<SelectOption> types = new List<SelectOption>();
       Schema.DescribeFieldResult fieldResult = Item__c.Item_Type__c.getDescribe();
       List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
       for( Schema.PicklistEntry f : ple)
           {
              types.add(new SelectOption(f.getLabel(), f.getValue()));
           }       
       return types;
    }
    
   public PageReference add(){

        Item__c NewItem = new Item__c(            
                Name = NewItemName,
                Item_Price__c = NewItemPrice,
                Items_Available__c = NewItemQuantity,
                Item_Type__c = NewItemType,
                Release_Date__c = NewItemReleaseDate);
                insert NewItem; 
                ViewData();
                PageReference curPage = ApexPages.currentPage(); 
                curPage.getParameters().put('success','true');
                curPage.getParameters().put('id',Apexpages.currentPage().getParameters().get('id'));
                curPage.setRedirect(true);
                NewItemName=null;
                NewItemQuantity=null;
                NewItemPrice = null;
                NewItemReleaseDate = null;
                return null; 
    }     
   public PageReference del() {

        Item__c ItemToDel = [SELECT id
                             FROM Item__c
                             WHERE id = :IdToDel];
        delete ItemToDel;   
               
        PageReference curPage = ApexPages.currentPage(); 
        curPage.getParameters().put('success','true');
        curPage.getParameters().put('id',Apexpages.currentPage().getParameters().get('id'));
        ViewData();
        curPage.setRedirect(false);       
        return curPage;
            
    }    

  public PageReference search() {
        String qry = 'select id, name, createddate, item_price__c from Item__c ' +
        'where name LIKE \'%'+searchText+'%\' order by name';
        searchResults = Database.query(qry);
        PageReference curPage = ApexPages.currentPage(); 
        curPage.getParameters().put('success','true');
        curPage.getParameters().put('id',Apexpages.currentPage().getParameters().get('id'));
        ViewData();
        curPage.setRedirect(true);       
        return null;        
  }    

   public String sortExpression
   {
     get
     {
        return sortExp;
     }
     set
     {
       if (value == sortExp)
         sortDirection = (sortDirection == 'ASC')? 'DESC' : 'ASC';
       else
         sortDirection = 'ASC';
       sortExp = value;
     }
   }

 public String getSortDirection()
 {
    if (sortExpression == null || sortExpression == '')
      return 'ASC';
    else
     return sortDirection;
 }

 public void setSortDirection(String value)
 {  
   sortDirection = value;
 }
  
   public List<Item__c> getItems() {
       return items;
   }

   public PageReference ViewData() {
   
       string sortFullExp = sortExpression  + ' ' + sortDirection;
       items = Database.query('Select id, Name, Item_Price__c, CreatedDate from Item__c order by ' + sortFullExp + ' limit 1000');
       return null;
   }
}

 I wrote a piece of test, it looks like this:

 

@isTest
private class TestFullFunctionality_2 {

static testMethod void testItemEnter() {

    Item__c NewItem = new Item__c(name = 'NEWITEM', Item_Price__c = 100, Item_Type__c = 'Solid', Items_Available__c = 10 );
    insert NewItem;
    Test.startTest();
    Test.stopTest();    
}
}

 ...but still the code coverage total is 0%. What do i do wrong?

 

Thanks in advance.

satyamsatyam

Hi,

 

For this you have to 

 

1) Insert a record of Item

2)Created a controller of your class

3)Pass the record to your controller

4)call the methods of your class

 

Please let me know if you are facing any issue.

 

Thanks

Satyam

NervosaNervosa

1) Insert a record of Item

Aren't I doing this by my testItemEnter() method?

 

2)Created a controller of your class

What is it - CONTROLLER OF CLASS ???

 

3)Pass the record to your controller

Isn't it passed by insert NewItem command?

satyamsatyam

Hi,

 

Do something like this:

 

// this need to be done at the start
static TestFullFunctionality_2 ext;

// You insert item
Item__c NewItem = new Item__c(name = 'NEWITEM', Item_Price__c = 100, Item_Type__c = 'Solid', Items_Available__c = 10 );
insert NewItem;

ApexPages.StandardController con1 = new ApexPages.StandardController(NewItem );
ext = new TestFullFunctionality_2(con1);
You need to call all the methods like this:

ext.add();
ext.delete();

 

Please mark it resolved if this is the answer of youe question.

 

Thanks:))

NervosaNervosa

Okay. I've done it like this -

@isTest
private class TestFullFunctionality_2 {
    
static testMethod void testAdd() {
    TestFullFunctionality_2 ext;
    Item__c NewItem = new Item__c(name = 'NEWITEM', Item_Price__c = 100, Item_Type__c = 'Solid', Items_Available__c = 10 );
    insert NewItem;
    ApexPages.StandardController con1 = new ApexPages.StandardController(NewItem);
    ext = new TestFullFunctionality_2(con1);

    Test.startTest();
    Test.stopTest();    
}
}

 but it can't even be saved, i see an error "Error: Compile Error: Constructor not defined: [TestFullFunctionality_2].<Constructor>(ApexPages.StandardController) at line 9 column 11"

Sorry for being too annoying...
satyamsatyam

Hi ,

 

try this

 

i got the problem

 

@isTest
private class TestFullFunctionality_2 {

static testMethod void testAdd() {
TestFullFunctionality_2 ext;
Item__c NewItem = new Item__c(name = 'NEWITEM', Item_Price__c = 100, Item_Type__c = 'Solid', Items_Available__c = 10 );
insert NewItem;

TestFullFunctionality_2 Con=new TestFullFunctionality_2();
Con.add();
Con.del();
Con.search();
Con.viewData();

 

//you can call all your methods and provide parameter to the methods if needed.

}
}

 

it should work :))

 

Thanks:))

NervosaNervosa

Hey, it almost helped me! I changed test class like this:

@isTest
private class TestFullFunctionality_2 {

String NewItemName = 'NEWITEM';
Integer NewItemPrice = 100;
String NewItemType = 'Solid';
Integer NewItemQuantity = 10;

static testMethod void testAdd() {
TestFullFunctionality_2 ext;

FullFunctionality_2 Con=new FullFunctionality_2();
Con.add();
Con.del();
Con.search();
Con.viewData();

}
}

 Now total coverage is 32% !!! But i get an error:

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Item_Price__c]: [Item_Price__c]

 I think i'm very close to solution =)

satyamsatyam

Hi,

 

I think you have to insert a item record which you removed from code and please make sure that need to insert all the mandatory field of that object otherwise it will throw error.

 

Item__c NewItem = new Item__c(name = 'NEWITEM', Item_Price__c = 100, Item_Type__c = 'Solid', Items_Available__c = 10 );
insert NewItem;

 

Please put this line in your code.

 

Thanks:))

NervosaNervosa

Okay. Now test class looks like this:

 

@isTest
private class TestFullFunctionality_2 {

String NewItemName = 'NEWITEM';
Integer NewItemPrice = 100;
String NewItemType = 'Solid';
Integer NewItemQuantity = 10;

static testMethod void testAdd() {
TestFullFunctionality_2 ext;
Item__c NewItem = new Item__c(name = 'NEWITEM', Item_Price__c = 100, Item_Type__c = 'Solid', Items_Available__c = 10 );
insert NewItem;
FullFunctionality_2 Con=new FullFunctionality_2();
Con.add();
Con.del();
Con.search();
Con.viewData();

}
}

 and still i get the same error:

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Item_Price__c]: [Item_Price__c]

 =(