반응형
Apex Test Code 예제를 보며 System.assertEquals() , System.assert()를 사용하여 예외 케이스까지 테스트 해본다.
public class AccountService {
// Enum to represent Account Levels
public enum AccountLevel {
STANDARD, PREMIUM, VIP
}
// Method to update the Account Level based on revenue
public static void updateAccountLevel(Id accountId) {
Account account = [SELECT Id, AnnualRevenue FROM Account WHERE Id = :accountId LIMIT 1];
if (account.AnnualRevenue == null) {
throw new IllegalArgumentException('Annual Revenue cannot be null.');
}
if (account.AnnualRevenue < 50000) {
account.Level__c = AccountLevel.STANDARD.name();
} else if (account.AnnualRevenue < 100000) {
account.Level__c = AccountLevel.PREMIUM.name();
} else {
account.Level__c = AccountLevel.VIP.name();
}
update account;
}
// Method to calculate discount rate based on Account Level
public static Decimal calculateDiscountRate(Id accountId) {
Account account = [SELECT Id, Level__c FROM Account WHERE Id = :accountId LIMIT 1];
if (String.isEmpty(account.Level__c)) {
throw new IllegalArgumentException('Account Level is not set.');
}
switch on account.Level__c {
when 'STANDARD' {
return 0.05; // 5% discount
}
when 'PREMIUM' {
return 0.1; // 10% discount
}
when 'VIP' {
return 0.2; // 20% discount
}
when else {
throw new IllegalArgumentException('Invalid Account Level.');
}
}
}
}
@isTest
public class AccountServiceTest {
@testSetup
static void setupTestData() {
// Insert test accounts
List<Account> testAccounts = new List<Account>{
new Account(Name = 'Standard Account', AnnualRevenue = 40000),
new Account(Name = 'Premium Account', AnnualRevenue = 75000),
new Account(Name = 'VIP Account', AnnualRevenue = 150000),
new Account(Name = 'Null Revenue Account', AnnualRevenue = null)
};
insert testAccounts;
}
@isTest
static void testUpdateAccountLevel() {
// Retrieve test accounts
List<Account> accounts = [SELECT Id, AnnualRevenue, Level__c FROM Account];
// Update levels for all accounts
for (Account acc : accounts) {
if (acc.AnnualRevenue != null) {
AccountService.updateAccountLevel(acc.Id);
}
}
// Verify levels
Account standardAccount = [SELECT Level__c FROM Account WHERE Name = 'Standard Account'];
System.assertEquals('STANDARD', standardAccount.Level__c);
Account premiumAccount = [SELECT Level__c FROM Account WHERE Name = 'Premium Account'];
System.assertEquals('PREMIUM', premiumAccount.Level__c);
Account vipAccount = [SELECT Level__c FROM Account WHERE Name = 'VIP Account'];
System.assertEquals('VIP', vipAccount.Level__c);
}
@isTest
static void testCalculateDiscountRate() {
// Retrieve test accounts
Account vipAccount = [SELECT Id FROM Account WHERE Name = 'VIP Account'];
Account premiumAccount = [SELECT Id FROM Account WHERE Name = 'Premium Account'];
Account standardAccount = [SELECT Id FROM Account WHERE Name = 'Standard Account'];
// Update levels
AccountService.updateAccountLevel(vipAccount.Id);
AccountService.updateAccountLevel(premiumAccount.Id);
AccountService.updateAccountLevel(standardAccount.Id);
// Verify discount rates
Decimal vipDiscount = AccountService.calculateDiscountRate(vipAccount.Id);
System.assertEquals(0.2, vipDiscount);
Decimal premiumDiscount = AccountService.calculateDiscountRate(premiumAccount.Id);
System.assertEquals(0.1, premiumDiscount);
Decimal standardDiscount = AccountService.calculateDiscountRate(standardAccount.Id);
System.assertEquals(0.05, standardDiscount);
}
@isTest
static void testErrorHandling() {
// Retrieve the account with null revenue
Account nullRevenueAccount = [SELECT Id FROM Account WHERE Name = 'Null Revenue Account'];
// Test updateAccountLevel with null revenue
try {
AccountService.updateAccountLevel(nullRevenueAccount.Id);
System.assert(false, 'Expected an exception for null AnnualRevenue');
} catch (IllegalArgumentException e) {
System.assertEquals('Annual Revenue cannot be null.', e.getMessage());
}
// Test calculateDiscountRate with unset Level__c
Account newAccount = new Account(Name = 'No Level Account');
insert newAccount;
try {
AccountService.calculateDiscountRate(newAccount.Id);
System.assert(false, 'Expected an exception for unset Level__c');
} catch (IllegalArgumentException e) {
System.assertEquals('Account Level is not set.', e.getMessage());
}
}
}
name()
: Apex에서 Enum의 name() 메서드는 열거형(Enum) 값의 문자열 이름을 반환하는 메서드로, Enum의 값(상수)을 문자열로 변환할 때 사용된다.
System.assertEquals() , System.assert()
반응형