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
KhaledZeroKhaledZero 

Unit test I cannot exceed 60%

Hi,
I want be at least 75 % of cover of the code of my class
My class to be tested :

    Public static void blockChange(FeedItem item){
     List<CollaborationGroup> MyGroup = [Select Name From CollaborationGroup where Id = :item.ParentId LIMIT 1];
            if(MyGroup.size() >0){
                if(MyGroup.get(0).Name.toUpperCase().contains('#INACTIF')){
                        item.addError('this is inactive group');
                }
            }    
    }
    Public static void blockChange(FeedItem item, FeedComment comment){
     List<CollaborationGroup> MyGroup = [Select Name From CollaborationGroup where Id = :item.ParentId LIMIT 1];
            if(MyGroup.size() >0){
                if(MyGroup.get(0).Name.toUpperCase().contains('#INACTIF')){
                        comment.addError('this is inactive group');
                }
            }    
    }
}

 My Test Class:

@isTest(SeeAllData=true)
public class InactiveGroups_ClassTest{
    static testMethod void myUnitTest() {
        Group gp= new Group();
        gp.Name ='#inactif';
     FeedItem item = new FeedItem();
  item.ParentId = gp.Id;       
        FeedComment comment = new FeedComment();
        Test.startTest();
        InactiveGroups_Class.blockChange(item);
        InactiveGroups_Class.blockChange(item, comment);
        Test.stopTest();   
    }
}

 Thx.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
KhaledZeroKhaledZero
static testMethod void myUnitTest() {        
        CollaborationGroup gp= new CollaborationGroup(Name = 'MyGroup To test', CollaborationType = 'Private');
        gp.Name ='#inactif';
        insert gp;
        FeedItem item = new FeedItem();
        item.ParentId = gp.Id;        
    FeedComment comment = new FeedComment();
        Test.startTest();
        InactiveGroups_Class.blockChange(item);
        InactiveGroups_Class.blockChange(item, comment);
        Test.stopTest();    
   }

 

All Answers

hitesh90hitesh90

can you post your full class code?

KhaledZeroKhaledZero
Public static void blockChange(FeedItem item){
     List<CollaborationGroup> MyGroup = [Select Name From CollaborationGroup where Id = :item.ParentId LIMIT 1];
            if(MyGroup.size() >0){
                if(MyGroup.get(0).Name.toUpperCase().contains('#INACTIF')){
                        item.addError('this is inactive group');
                }
            }    
    }
    Public static void blockChange(FeedItem item, FeedComment comment){
     List<CollaborationGroup> MyGroup = [Select Name From CollaborationGroup where Id = :item.ParentId LIMIT 1];
            if(MyGroup.size() >0){
                if(MyGroup.get(0).Name.toUpperCase().contains('#INACTIF')){
                        comment.addError('this is inactive group');
                }
            }    
    }
}

 

Abhi_TripathiAbhi_Tripathi

first of all remove that "void" and return something as i have done below, coz statci method can be called easily,

 

and after that simply call these methods and in only two lines your code coveerage will be 100%, like 

 

//insert your item record first

insert iten;

 

//call your static method here

className.blockChanges(item);

 

 

Public static string blockChange(FeedItem item){

hitesh90hitesh90

Hi,

 

Try to use below test class code.

 

Test class:

@isTest(SeeAllData=true)
public class InactiveGroups_ClassTest{
    static testMethod void myUnitTest() {
		Group gp= new Group();
        gp.Name ='#inactif';
        FeedItem item = new FeedItem();
		item.ParentId = gp.Id;       
        insert item;
		
		FeedComment comment = new FeedComment();
        Test.startTest();
        InactiveGroups_Class.blockChange(item);
        InactiveGroups_Class.blockChange(item, comment);
        Test.stopTest();   
    }
}

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

KhaledZeroKhaledZero

@hitesh90

it's now at 30%

:(

 

 

 

KhaledZeroKhaledZero

hye Abhi,

can you give me more explacation or code to teste please

 

thank you

hitesh90hitesh90

Try to use following code.

 

@isTest(SeeAllData=true)
public class InactiveGroups_ClassTest{
    static testMethod void myUnitTest() {
		Group gp= new Group();
        gp.Name ='#INACTIF';
        FeedItem item = new FeedItem();
		item.ParentId = gp.Id;       
		
		FeedComment comment = new FeedComment();
        Test.startTest();
        InactiveGroups_Class.blockChange(item);
        InactiveGroups_Class.blockChange(item, comment);
        Test.stopTest();   
    }
}

 

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

KhaledZeroKhaledZero

it's the same thig with my code !

the resulte 60%

KhaledZeroKhaledZero
static testMethod void myUnitTest() {        
        CollaborationGroup gp= new CollaborationGroup(Name = 'MyGroup To test', CollaborationType = 'Private');
        gp.Name ='#inactif';
        insert gp;
        FeedItem item = new FeedItem();
        item.ParentId = gp.Id;        
    FeedComment comment = new FeedComment();
        Test.startTest();
        InactiveGroups_Class.blockChange(item);
        InactiveGroups_Class.blockChange(item, comment);
        Test.stopTest();    
   }

 

This was selected as the best answer