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
Artem Trill 2Artem Trill 2 

Apex Unit Testing

Hi, 
I have a test class for Salesforce trigger.
I have 100% coverage without any test.startTest();  test.stopTest().
Should i write them to? 
If I have 100% coverage => my test class is good? 
So I don't need to write any positive, negative tests? 
If I have negative and postive tests, how can I check results?
Thank you. 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Artem,

Greetings to you!

startTest and stopTest methods are used to validate how close the code is to reaching governor limits. They are not required to be used but in some situations may be critical.

startTest:
The startTest method marks the point in your test code when your test actually begins. Each test method is allowed to call this method only once. All of the code before this method should be used to initialize variables, populate data structures, and so on, allowing you to set up everything you need to run your test. Any code that executes after the call to startTest and before stopTest is assigned a new set of governor limits.

The startTest method does not refresh the context of the test: it adds a context to your test. For example, if your class makes 98 SOQL queries before it calls startTest, and the first significant statement after startTest is a DML statement, the program can now make an additional 100 queries. Once stopTest is called, however, the program goes back into the original context, and can only make 2 additional SOQL queries before reaching the limit of 100.

stopTest:
The stopTest method marks the point in your test code when your test ends. Use this method in conjunction with the startTest method. Each test method is allowed to call this method only once. Any code that executes after the stopTestmethod is assigned the original limits that were in effect before startTest was called. All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously. 

Reference: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_start_stop_test.htm

https://salesforce.stackexchange.com/questions/80949/when-to-use-test-starttest

Positive behavior:
Test to verify that the expected behavior occurs through every expected permutation, that is, that the user filled out everything correctly and did not go past the limits. 
-> Verify that when passed valid inputs, called code completes without throwing an exception.

Negative behavior:
There are likely limits to your applications, such as not being able to add a future date, not being able to specify a negative amount, and so on. You must test for the negative case and verify that the error messages are correctly produced as well as for the positive, within the limits cases.
-> Verify that when passed invalid inputs, exceptions are properly handled.

Please refer to below links which might help you further:

Testing Example:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm?search_text=negative%20test

Write Positive Tests:
https://trailhead.salesforce.com/en/content/learn/modules/unit-testing-on-the-lightning-platform/positive-tests

Write Negative Tests:
https://trailhead.salesforce.com/en/content/learn/modules/unit-testing-on-the-lightning-platform/negative-tests

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
About MeAbout Me
Hi Artam, the main aim of writing  test classes is to make sure the Apex code written is working as expected in any scenario(positive,negative bulk) is the best practice of writing Test classes. Please follow the documentation.
'https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm'
I would suggest posting the code so developers here can check and post their thoughts. Hope this helps! hit cheers.
Ajay K DubediAjay K Dubedi
Hi Artem,

test.startTest(); test.stopTest() are the methods to in which Test.startTest() to reset the governor limits and Test.stopTest() to reset the governor limits and allow for any async jobs to finish. These two methods are not necessary but for best practice 
we use it.
Positive behaviour :
Test to verify that the expected behaviour occurs through every expected permutation, that is, that the user filled out everything correctly and did not go past the limits.
Negative behaviour
There are likely limits to your applications, such as not being able to add a future date, not being able to specify a negative amount, and so on. You must test for the negative case and verify that the error messages are correctly produced as well as for the positive, within the limits cases.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi