[Salesforce] Apex Testing - Apex 유닛 테스트 시작하기



Apex 유닛 테스트 시작하기

https://trailhead.salesforce.com/ko/content/learn/modules/apex_testing/apex_testing_intro


Apex 유닛 테스트

사용 시 장점

  • Apex 클래스 및 트리거 테스트
  • Apex 클래스/트리거 업데이트될 때 회귀테스트 진행
  • 코드 커버리지 요구사항 충족


테스트 클래스 및 테스트 메서드 구문

  • 최상위 클래스에 @isTest 애노테이션 달아야함
@isTest
private class MyTestClass {
  @isTest static void myTest() {
    // code_block
  }
}


유닛 테스트 예제

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);
  }
}

  • 테스트 수행

  • 성공


  • 실패


코드 적용 범위 확장

  • 최소 커버리지인 75%보다 높게 목표를 잡아서 테스트케이스를 많게 하는 것을 권고

  • 코드 커버리지가 75%인 경우


  • 코드 커버리지 100%인 경우


테스트 도구 모음 생성 및 실행

  • Apex 테스트 클래스 모음


  • 생성


  • 실행 결과 확인




Hands-on

  • 코드 커버리지 100% 달성하는 테스트코드 작성하기
    public class VerifyDate {
      //method to handle potential checks against two dates
      public static Date CheckDates(Date date1, Date date2) {
          //if date2 is within the next 30 days of date1, use date2.  Otherwise use the end of the month
          if(DateWithin30Days(date1,date2)) {
              return date2;
          } else {
              return SetEndOfMonthDate(date1);
          }
      }
      //method to check if date2 is within the next 30 days of date1
      private static Boolean DateWithin30Days(Date date1, Date date2) {
          //check for date2 being in the past
          if( date2 < date1) { return false; }
          //check that date2 is within (>=) 30 days of date1
          Date date30Days = date1.addDays(30); //create a date 30 days away from date1
          if( date2 >= date30Days ) { return false; }
          else { return true; }
      }
      //method to return the end of the month of a given date
      private static Date SetEndOfMonthDate(Date date1) {
          Integer totalDays = Date.daysInMonth(date1.year(), date1.month());
          Date lastDay = Date.newInstance(date1.year(), date1.month(), totalDays);
          return lastDay;
      }
    }
    
@isTest
public class TestVerifyDate {
    
    @isTest
    static void testWithin30Days() {
        Date date1 = Date.newInstance(2026, 5, 4);
        Date date2 = Date.newInstance(2026, 6, 2);
        Date resultDate = VerifyDate.CheckDates(date1, date2);
        System.assertEquals(resultDate, date2);
    }
    
    @isTest
    static void testEndOfMonthDate() {
        Date date1 = Date.newInstance(2026, 5, 4);
        Date date2 = Date.newInstance(2026, 6, 3);
        Date resultDate = VerifyDate.CheckDates(date1, date2);
        
        Date endOfMonth = Date.newInstance(2026,5,31);
        System.assertEquals(resultDate, endOfMonth);
    }
    
}

다른 글