Apex Test classes and methods

Prakher Chaturvedi
3 min readMay 13, 2020

--

I have touched almost all the important topics of Apex Programming and will head to Apex unit testing which is one the most important topics. I will study what apex test classes and methods do and jump to do a hands-on on trailhead.
Testing is one of the important phases in building stable apps. Apex Test classes provide you the way to test your code so that it is stable and also complies with the 75% code coverage limitation set by salesforce.

What Apex Test Classes do?

  • used to verify a particular piece of code
  • ensures you apex class and triggers work as expected
  • helps in meeting code coverage requirements
  • helps in building high-quality apps
  • is defined with @isTest notation
  • checks the actual and expected outcome(System.assertsEquals(expected,actual))

Apex Test Methods:

A Test method should always be defined inside the test class
A Test method does not take any argument
Can either have @istest at the starting of method name or testMethod before return type (void in test method)

@isTest static void testName(){

} or
static testMethod void testName(){

}

Syntax of an Apex test Class:

An Apex test class is always annotated by @isTest at the starting of the class name declaration and syntax is like
@isTest private class MyTestClass {
@isTest static void myTest() {
// code_block
}
}

Writing codes in test class:

To write test classes for testing a class or trigger in apex, we should first understand what that apex class or trigger does. We should be able to understand every scenario which the code addresses. If we address each scenario in our test class, we will not only be able to test the apex class completely but also will maximize our code coverage.

As we are now having understanding of what apex class does, and how to write apex codes let us jump to a trailhead module named Apex Testing.

In the first section of this module, Get started with apex we have been provided a code for which we have to write a text class. the test class which we write should have 100% code coverage. As discussed, to maximize our code coverage we have to cover every scenario (if-else). Let us see what the code is.

Code for which we have to write a test class

So, let us try to understand this code to achieve 100% code coverage. The first method Date check method works on If Else. If datewithin30days method returns true it will return date 2 else it will run SetEndofMonthDate Method.

If we look at the DateWithin30Days method, it returns true in one case and false in two cases. so we will have 3 test methods in test class for this code.

This is how my Test class looks like. We need to understand why these three cases were taken, and when we take these 3 cases, we achieve 100% code coverage.

How Can You Contribute?

  • Connect with me on Twitter or Linkedin or Instagram.
  • Provide me continuous feedback and suggestions for moving ahead in this journey.
  • Share the series on Twitter and LinkedIn.

Posts In This Series :

Salesforce Development — Learning Apex Programming, VisualForce, Lightning Components
First Step toward Learning Apex Programming
Day 2 in Apex — Writing apex codes
Working with sObjects, SOQL, and SOSL
Understanding Salesforce Triggers and working with them
Apex Test classes and methods
Understanding VisualForce Concepts
Controllers in VisualForce

--

--

Prakher Chaturvedi

Looking for remote opportunities | Salesforce Developer | Lightning Web Components | APEX | 5x Certified | Trailhead Ranger | Content Creator (Medium)