CategoryTest Classes

Why can’t we use static data in test classes?

Consider we fetch a set of records from the system and used them to cover the test coverage and successfully achieved 90% test coverage. We can move our class to Production, the first time which is a fresh copy hence no data exists there. As soon as we deploy our code, it will fail as test class is querying data from the system used before and our current Production system has no data. That’s why...

A developer is trying to insert data in the system using a test class, but the data is not reflecting. He made every possible attempt. What could be the problem?

Test classes have its own virtual database which is created when the test class is executed and destroyed with its completion. This virtual database is not committed to the original database but kept private to be used for methods in test classes. That’s why DMLs may not affect original data.

When is @testsetup used?

Consider you have multiple test methods in a test class and you want to use a single set of data to them. So instead of writing the same line of code for generating data in every method, you can generate it commonly and make it available for every method to use using test setup. Data in this method can be used by any test method liberally. @testsetup static void SetMethod(){}

What is the importance of Test.starttest() and Test.StopTest() in test class?

You use a fresh new context of governor limits within these methods. Consider your test classes have 50 lines and your starttest is at line 30 and endtest is at line 40.

Then for the lines 30 to 40, a different set of governor limits will be
used freshly which have nothing to do with the governor limits used for lines
1-30 and 40-50. In short, you can double up the limits using them.

Recent Posts

Recent Comments