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
pavan kumar 1156pavan kumar 1156 

Test is successfully runs but wont shows any test coverage ..!

Apex class
-------------
public class StringStack {

      private List<String> stack;

    public StringStack()
    {
         stack = new List<String>();

    }

    public void push(String s)
    { 
        stack.add(s); 
    }
    public String pop() 
    {
       
    return stack.remove( lastItemIndex );
    }
    public String peak() 
    {

        return stack.get( lastItemIndex );

    }

 
    public Boolean isEmpty() 
    { 
        return stack.isEmpty();
    }
     

    // Helper Property

    private Integer lastItemIndex
    {
        get 
        { 
            return stack.size() - 1;
            }
    }

}



Test clas
------------
@isTest
public class TestStringStack 
 {
public static testmethod void StringStackcheck()
{
    //Instantiate a StringStack.

    StringStack stack = new StringStack();
 // Set up some test data.

    String bottomString = 'Bottom String';
    String middleString = 'Middle String';
    String topString = 'Top String';

      // Call the push() method with multiple objects

    stack.push(bottomString);

    stack.push(middleString);
    stack.push(topString);

    // Verify that the 'top' object is the object we expected

    String peakValue = stack.peak();

    System.assertEquals(topString, peakValue);

    // Verify that the order of the objects is as we expected
    String popValue = stack.pop();
    System.assertEquals(topString, popValue);
    popValue = stack.pop();
    System.assertEquals(middleString, popValue);
    popValue = stack.pop();
    System.assertEquals(bottomString, popValue);
    System.assert(stack.isEmpty());

}
 }
Best Answer chosen by pavan kumar 1156
Sandeep YadavSandeep Yadav
Hello, Pawan your code is correct.
But you must use Test.startTest()  and  Test.stopTest() in your test Class
Update your Test Class with this--
@isTest
public class TestStringStack 
 {
public static testmethod void StringStackcheck()
{
    //Instantiate a StringStack.

    StringStack stack = new StringStack();
 // Set up some test data.

    String bottomString = 'Bottom String';
    String middleString = 'Middle String';
    String topString = 'Top String';

      // Call the push() method with multiple objects
    
    Test.startTest(); //startTesting
    stack.push(bottomString);
    stack.push(middleString);
    stack.push(topString);

    // Verify that the 'top' object is the object we expected

    String peakValue = stack.peak();
    Test.stopTest(); //stopTesting
    System.assertEquals(topString, peakValue);

    // Verify that the order of the objects is as we expected
    String popValue = stack.pop();
    System.assertEquals(topString, popValue);
    popValue = stack.pop();
    System.assertEquals(middleString, popValue);
    popValue = stack.pop();
    System.assertEquals(bottomString, popValue);
    System.assert(stack.isEmpty());

}
 }
Let me inform if you solve your problem and
If this helps you mark it best answer.
Thanks.
Sandeep
 

All Answers

Sandeep YadavSandeep Yadav
Hello, Pawan your code is correct.
But you must use Test.startTest()  and  Test.stopTest() in your test Class
Update your Test Class with this--
@isTest
public class TestStringStack 
 {
public static testmethod void StringStackcheck()
{
    //Instantiate a StringStack.

    StringStack stack = new StringStack();
 // Set up some test data.

    String bottomString = 'Bottom String';
    String middleString = 'Middle String';
    String topString = 'Top String';

      // Call the push() method with multiple objects
    
    Test.startTest(); //startTesting
    stack.push(bottomString);
    stack.push(middleString);
    stack.push(topString);

    // Verify that the 'top' object is the object we expected

    String peakValue = stack.peak();
    Test.stopTest(); //stopTesting
    System.assertEquals(topString, peakValue);

    // Verify that the order of the objects is as we expected
    String popValue = stack.pop();
    System.assertEquals(topString, popValue);
    popValue = stack.pop();
    System.assertEquals(middleString, popValue);
    popValue = stack.pop();
    System.assertEquals(bottomString, popValue);
    System.assert(stack.isEmpty());

}
 }
Let me inform if you solve your problem and
If this helps you mark it best answer.
Thanks.
Sandeep
 
This was selected as the best answer
pavan kumar 1156pavan kumar 1156
User-added image
thank you but small clarification When &Why we have to use those methods.!
Sandeep YadavSandeep Yadav
We use these methods to avoid Governer limit defined by salesforce.
We have multiple lines in our code so these determines when to start testing of our classes and triggers
for details go to this link--
https://success.salesforce.com/answers?id=90630000000hr9tAAA 
Thanks.