Testing: 12% Flashcards

1
Q

What annotation is used to define classes or individual methods that only contain code used for testing

A

@isTest

Unit test methods are static methods that are defined with the @isTest annotation or the testMethod keyword

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the two options for creating test data for a test class?

A

1- Create test data in your test method

2- Write utility test classes containing methods for test data creation that can be called by other tests

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why should you add @isTest annotation to a common utility class used by test classes?

A

Using this annotation is that the class won’t count against the 3MB organization code size limit. The public methods in this can only be called by test code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why is using Test.startTest and Test.stopTest statements a good testing practice?

A

You can perform prerequisite setup and test data creation prior to calling the Test.startTest and include verifications after Test.stopTest. The original set of governor limits are reverted to after the call to Test.stopTest

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When running test classes, why should you set the Test | Always Run Asynchronously flag?

A

If you don’t select Always Run Asynchronously, test runs that include tests from only one class run synchronously. Test runs that include more than one class run asynchronously even if you don’t select this option

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When would a test method be public versus private?

A

It’s a best practice to keep your test code private, but if you create test helper or utility classes that are used by separate test classes, they’ll need to be public.

The reason for private classes are that the Apex test framework can find and run your tests, but nothing else should be able to.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a good pattern to follow for writing test classes (3 easy steps :))

A

1- Perform required setup, including creation of test data
2- Perform the test execution, wrapped inside Test.startTest() and Test.endTest() test framework calls
3- Compare the results of the test execution with known data. (Compare actual behaviour to expected behaviour.)

It’s also best practice to test only one thing at a time, and to put each test into a separate method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Test-first development?

A

It is the practice of writing tests for a feature before you write the code to implement the feature.

Because the feature isn’t implemented yet, the tests will fail. Then you implement the feature and run the tests again. When they pass, you have some confidence that you’ve implemented the feature correctly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When testing controllers is it necessary for all page names to be in lowercase or is it case insensitive?

A

All page names must be in lowercase for all test methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which constructor should be used when testing extensions to standard list controllers?

A

ApexPages.StandardSetController(new ObjectName[])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When testing Controllers, how can controllers mimic navigating to another page?

A

Through the following code:

controller.save().getURL();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which method is used to populate test data via a static resource

A

Test.loadData

If the static resource name is myResource, then the code will look as follows:
List<sObject> ls = Test.loadData(Account.sObjectType, 'myResource');</sObject>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of defining a test class with @isTest(SeeAllData=true)

A

It ensures that all the test methods in the class have access to all the data in the organization

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the runAs Method do in Unit tests?

A

User record sharing is enforced.

It doesn’t enforce user permissions or field-level permissions, only record sharing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can you bypass the mixed DML error in test methods?

A

1) You can use the runAs method to perform mixed DML operations in your test by enclosing the DML operations within the runAs block. In this way, you bypass the mixed DML error that is otherwise returned when inserting or updating setup objects together with other sObjects.
2) Use @future to Bypass the Mixed DML error in a Test Method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you use the runAs method with managed packages?

A

There is an overload of the runAs method (runAs(System.Version)) that takes a package version as an argument. This method causes the code of a specific version of a managed package to be used.