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
bhanu_prakashbhanu_prakash 

soql 101 at test class

Hi,

Iam getting error : soql 101 error on test class
@isTest
private class LastCommentComponentTest {
    @isTest
    static void publicOnlyTest(){
        Case c = new Case(subject = 'apex test case');
        insert c;
        CaseComment comment1 = new CaseComment(isPublished = false, parentId = c.id, commentBody = 'private apex test comment');
        insert comment1;
        CaseComment comment2 = new CaseComment(isPublished = true, parentId = c.id, commentBody = 'public apex test comment');
        insert comment2;
        CaseComment comment3 = new CaseCOmment(isPublished = false, parentId = c.id, commentBody = 'private apex test comment #2');
        insert comment3;
        
        Test.startTest();
        LastCommentComponent controller = new LastCommentComponent();
        controller.caseId = c.id;
        controller.onlyPublic = true;
        CaseComment result = controller.getComment();
        Test.stopTest();
        
        System.assertEquals(result.id,comment2.id);
    }
    
    @isTest
    static void allTest(){
        Case c = new Case(subject = 'apex test case');
        insert c;
        CaseComment comment1 = new CaseComment(isPublished = false, parentId = c.id, commentBody = 'private apex test comment');
        insert comment1;
        CaseComment comment2 = new CaseComment(isPublished = true, parentId = c.id, commentBody = 'public apex test comment');
        insert comment2;
        CaseComment comment3 = new CaseComment(isPublished = false, parentId = c.id, commentBody = 'private apex test comment #2');
        insert comment3;
        
        Test.startTest();
        LastCommentComponent controller = new LastCommentComponent();
        controller.caseId = c.id;
        controller.onlyPublic = false;
        CaseComment result = controller.getComment();
        Test.stopTest();
        System.assertEquals(result.id,comment3.id,'Got :'+result+' instead of '+[SELECT id, createdDate, commentBody FROM CaseComment WHERE id = :comment3.id LIMIT 1]);
    }
    
    @isTest
    static void noCommentTest(){
        Case c = new Case(subject = 'apex test case');
        insert c;
        CaseComment comment1 = new CaseComment(isPublished = false, parentId = c.id, commentBody = 'private apex test comment');
        insert comment1;
        
        Test.startTest();
        LastCommentComponent controller = new LastCommentComponent();
        controller.caseId = c.id;
        controller.onlyPublic = true;
        CaseComment result = controller.getComment();
        Test.stopTest();
        
        System.assertEquals(null,result);
    }
}

Help me to fix these issue:)

Thanks for advance
Abdul KhatriAbdul Khatri
Waqar Hussain SFWaqar Hussain SF
You will be using SOQL in for loop either in the case trigger or LastCommentComponent controller. You will have to use best practise and will have to remove query from the loop.

See below links that will help you to identify the issue. 
https://help.salesforce.com/articleView?id=000181404&type=1
There is also a known issue with test classes due to process builder
https://success.salesforce.com/issues_view?id=a1p3A0000001C7cQAE