Apex code can only be written in a sandbox environment or developer edition, not in production.

Before deploying to production at least 75% of Apex code must be covered by tests, and all those tests must pass. In addition each trigger must have some coverage. Beyond this though testing should be done to test the common use cases in app, including positive and negative test cases, and bulk and single record processing.

This is the syntax of a test method.

@isTest static void testName() {
// code_block
}

Access modifiers are omitted in the syntax because the test framework is always able to access test methods.

Test methods must be defined in test classes, which are classes annotated with isTest:

@isTest
private class MyTestClass {
    @isTest static void myTest() {
        // code_block
    }
}

Notice the class does has an access modifier. If using this class for unit testing only, declare it as private.

I created the following class and test class. Notice that the test class contains test methods which use ‘System.assertEquals’. This takes two parameters: the expected value and the actual value(?).

public class TemperatureConverter {
// Takes a Fahrenheit temperature and returns the Celsius equivalent.
public static Decimal FahrenheitToCelsius(Decimal fh) {
Decimal cs = (fh - 32) * 5/9;
return cs.setScale(2);
}
}
@isTest
private class TemperatureConverterTest {
    @isTest static void testWarmTemp() {
        Decimal celsius = TemperatureConverter.FahrenheitToCelsius(70);
        System.assertEquals(21.11,celsius);
    }
    
    @isTest static void testFreezingPoint() {
        Decimal celsius = TemperatureConverter.FahrenheitToCelsius(32);
        System.assertEquals(0,celsius);
    }
    @isTest static void testBoilingPoint() {
        Decimal celsius = TemperatureConverter.FahrenheitToCelsius(212);        
        System.assertEquals(100,celsius,'Boiling point temperature is not expected.');
    } 
    
    @isTest static void testNegativeTemp() {
        Decimal celsius = TemperatureConverter.FahrenheitToCelsius(-10);
        System.assertEquals(-23.33,celsius);
    }     
}

I ran these tests via the tests tab and here’s what showed up in the debug log:

Screen Shot 2018-04-09 at 12.56.16 pm.png

Whenever you modify Apex code, rerun tests to refresh code coverage results.

It’s not possible to test for every data point but you can test for values in different ranges, positive/negative, boundary conditions, invalid parameter values etc. To test code behavior.

The higher the percentage of test coverage, the higher the likelihood that code is robust. Aim for 100%.

The equality operator (==) performs case-insensitive string comparisons, so there is no need to convert the string to lower case first. This means that passing in ‘ca’ or ‘Ca’ will satisfy the equality condition with the string literal ‘CA’.

Add test classes to a test suite to make them run together. Do this by going file -> new test suite, then adding test classes to the suite.

I used this code to pass a Trailhead challenge:

@isTest
private class TestVerifyDate {
    static testMethod void TestVerifyDate() {
      VerifyDate.CheckDates(System.today(),System.today().addDays(10));
       VerifyDate.CheckDates(System.today(),System.today().addDays(78));
    }
}

 

This tests how a class used to verify a date behaves when you pass it less than 30 days and more than 30 days.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s